import { forwardRef, useImperativeHandle, useMemo } from "react"; import { useCreation } from "ahooks"; import { Form, type FormInstance } from "antd"; import { useAntdForm } from "@/hooks"; import { NOTIFY_CHANNELS, type NotifyChannelsSettingsContent } from "@/domain/settings"; import NotifyChannelEditFormBarkFields from "./NotifyChannelEditFormBarkFields"; import NotifyChannelEditFormDingTalkFields from "./NotifyChannelEditFormDingTalkFields"; import NotifyChannelEditFormEmailFields from "./NotifyChannelEditFormEmailFields"; import NotifyChannelEditFormLarkFields from "./NotifyChannelEditFormLarkFields"; import NotifyChannelEditFormServerChanFields from "./NotifyChannelEditFormServerChanFields"; import NotifyChannelEditFormTelegramFields from "./NotifyChannelEditFormTelegramFields"; import NotifyChannelEditFormWebhookFields from "./NotifyChannelEditFormWebhookFields"; import NotifyChannelEditFormWeComFields from "./NotifyChannelEditFormWeComFields"; type NotifyChannelEditFormFieldValues = NotifyChannelsSettingsContent[keyof NotifyChannelsSettingsContent]; export type NotifyChannelEditFormProps = { className?: string; style?: React.CSSProperties; channel: string; disabled?: boolean; initialValues?: NotifyChannelEditFormFieldValues; onValuesChange?: (values: NotifyChannelEditFormFieldValues) => void; }; export type NotifyChannelEditFormInstance = { getFieldsValue: () => ReturnType["getFieldsValue"]>; resetFields: FormInstance["resetFields"]; validateFields: FormInstance["validateFields"]; }; const NotifyChannelEditForm = forwardRef( ({ className, style, channel, disabled, initialValues, onValuesChange }, ref) => { const { form: formInst, formProps } = useAntdForm({ initialValues: initialValues, }); const formName = useCreation(() => `notifyChannelEditForm_${Math.random().toString(36).substring(2, 10)}${new Date().getTime()}`, []); const formFieldsComponent = useMemo(() => { /* 注意:如果追加新的子组件,请保持以 ASCII 排序。 NOTICE: If you add new child component, please keep ASCII order. */ switch (channel) { case NOTIFY_CHANNELS.BARK: return ; case NOTIFY_CHANNELS.DINGTALK: return ; case NOTIFY_CHANNELS.EMAIL: return ; case NOTIFY_CHANNELS.LARK: return ; case NOTIFY_CHANNELS.SERVERCHAN: return ; case NOTIFY_CHANNELS.TELEGRAM: return ; case NOTIFY_CHANNELS.WEBHOOK: return ; case NOTIFY_CHANNELS.WECOM: return ; } }, [channel]); const handleFormChange = (_: unknown, values: NotifyChannelEditFormFieldValues) => { onValuesChange?.(values); }; useImperativeHandle(ref, () => { return { getFieldsValue: () => { return formInst.getFieldsValue(true); }, resetFields: (fields) => { return formInst.resetFields(fields); }, validateFields: (nameList, config) => { return formInst.validateFields(nameList, config); }, } as NotifyChannelEditFormInstance; }); return (
{formFieldsComponent}
); } ); export default NotifyChannelEditForm;