import {forwardRef, useImperativeHandle, useMemo} from "react"; import {Form, type FormInstance} from "antd"; import {NOTIFY_CHANNELS, type NotifyChannelsSettingsContent} from "@/domain/settings"; import {useAntdForm} from "@/hooks"; import NotifyChannelEditFormBarkFields from "./NotifyChannelEditFormBarkFields"; import NotifyChannelEditFormDingTalkFields from "./NotifyChannelEditFormDingTalkFields"; import NotifyChannelEditFormEmailFields from "./NotifyChannelEditFormEmailFields"; import NotifyChannelEditFormGotifyFields from "./NotifyChannelEditFormGotifyFields.tsx"; import NotifyChannelEditFormLarkFields from "./NotifyChannelEditFormLarkFields"; import NotifyChannelEditFormPushPlusFields from "./NotifyChannelEditFormPushPlusFields"; import NotifyChannelEditFormServerChanFields from "./NotifyChannelEditFormServerChanFields"; import NotifyChannelEditFormTelegramFields from "./NotifyChannelEditFormTelegramFields"; import NotifyChannelEditFormWebhookFields from "./NotifyChannelEditFormWebhookFields"; import NotifyChannelEditFormWeComFields from "./NotifyChannelEditFormWeComFields"; import NotifyChannelEditFormMattermostFields from "@/components/notification/NotifyChannelEditFormMattermostFields.tsx"; 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, name: "notifyChannelEditForm", }); const formFieldsEl = 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.GOTIFY: return ; case NOTIFY_CHANNELS.LARK: return ; case NOTIFY_CHANNELS.MATTERMOST: return ; case NOTIFY_CHANNELS.PUSHPLUS: 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 (
{formFieldsEl}
); } ); export default NotifyChannelEditForm;