WordBankForm.tsx 3.49 KB
import { useEffect, useRef, useState, useCallback } from 'react';
import { Modal, Form, Input, Select } from 'antd';
import { wordBankService } from '../../services/wordBankService';

const { TextArea } = Input;

const CATEGORY_OPTIONS = [
  { label: '个人身份信息(PII)', value: 'PII' },
  { label: '政治敏感', value: 'politics' },
  { label: '暴力内容', value: 'violence' },
  { label: '广告推广', value: 'ad' },
  { label: '自定义', value: 'custom' },
];

interface WordItem {
  id: string;
  category: string;
  word: string;
  replacement: string;
  enabled: boolean;
  created_at: string;
  updated_at: string;
}

interface Props {
  open: boolean;
  word: WordItem | null;
  onClose: () => void;
}

export default function WordBankForm({ open, word, onClose }: Props) {
  const [form] = Form.useForm();
  const isEdit = !!word;
  const [submitting, setSubmitting] = useState(false);
  const [dirty, setDirty] = useState(false);
  const firstFieldRef = useRef<HTMLInputElement>(null);
  const isInitialMount = useRef(true);

  useEffect(() => {
    if (open) {
      setDirty(false);
      isInitialMount.current = true;
      if (word) {
        form.setFieldsValue(word);
      } else {
        form.resetFields();
        form.setFieldValue('category', 'custom');
      }
      setTimeout(() => firstFieldRef.current?.focus(), 300);
    }
  }, [open, word, form]);

  const onFieldsChange = useCallback(() => {
    if (isInitialMount.current) {
      isInitialMount.current = false;
      return;
    }
    if (!dirty && form.isFieldsTouched()) {
      setDirty(true);
    }
  }, [dirty, form]);

  const handleClose = useCallback(() => {
    if (dirty) {
      Modal.confirm({
        title: '未保存的更改',
        content: '当前表单有未保存的修改,确定要关闭吗?',
        okText: '关闭',
        cancelText: '继续编辑',
        onOk: onClose,
      });
    } else {
      onClose();
    }
  }, [dirty, onClose]);

  const handleOk = async () => {
    try {
      setSubmitting(true);
      const values = await form.validateFields();
      if (isEdit) {
        await wordBankService.update(word!.id, values);
      } else {
        await wordBankService.create(values);
      }
      onClose();
    } catch {
      // validation errors shown inline
    } finally {
      setSubmitting(false);
    }
  };

  return (
    <Modal
      title={isEdit ? '编辑敏感词' : '新增敏感词'}
      open={open}
      onOk={handleOk}
      onCancel={handleClose}
      okText="保存"
      cancelText="取消"
      destroyOnHidden
      width={520}
      confirmLoading={submitting}
    >
      <Form
        form={form}
        layout="vertical"
        style={{ marginTop: 8 }}
        onFieldsChange={onFieldsChange}
      >
        <Form.Item
          name="category"
          label="分类"
          rules={[{ required: true, message: '请选择分类' }]}
        >
          <Select
            placeholder="选择分类"
            options={CATEGORY_OPTIONS}
          />
        </Form.Item>
        <Form.Item
          name="word"
          label="敏感词"
          rules={[{ required: true, message: '请输入敏感词' }]}
        >
          <Input placeholder="例如:身份证号、银行卡号" ref={firstFieldRef} />
        </Form.Item>
        <Form.Item
          name="replacement"
          label="替换文本"
          extra="留空则默认替换为 ***"
        >
          <Input placeholder="例如:[已脱敏]" />
        </Form.Item>
      </Form>
    </Modal>
  );
}