import { WorkflowNode, WorkflowNodeConfig } from "@/domain/workflow"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "../ui/form"; import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger } from "../ui/select"; import { Input } from "../ui/input"; import { useWorkflowStore, WorkflowState } from "@/providers/workflow"; import { useShallow } from "zustand/shallow"; import { usePanel } from "./PanelProvider"; import { useTranslation } from "react-i18next"; import { Button } from "../ui/button"; import { useNotifyContext } from "@/providers/notify"; import { useEffect, useState } from "react"; import { NotifyChannels, channels as supportedChannels } from "@/domain/settings"; import { SelectValue } from "@radix-ui/react-select"; import { Textarea } from "../ui/textarea"; import { RefreshCw, Settings } from "lucide-react"; type NotifyFormProps = { data: WorkflowNode; }; const selectState = (state: WorkflowState) => ({ updateNode: state.updateNode, }); type ChannelName = { name: string; label: string; }; const NotifyForm = ({ data }: NotifyFormProps) => { const { updateNode } = useWorkflowStore(useShallow(selectState)); const { hidePanel } = usePanel(); const { t } = useTranslation(); const { config: notifyConfig, initChannels } = useNotifyContext(); const [chanels, setChanels] = useState([]); useEffect(() => { setChanels(getChannels()); }, [notifyConfig]); const getChannels = () => { const rs: ChannelName[] = []; if (!notifyConfig.content) { return rs; } const chanels = notifyConfig.content as NotifyChannels; for (const channel of supportedChannels) { if (chanels[channel.name] && chanels[channel.name].enabled) { rs.push(channel); } } return rs; }; const formSchema = z.object({ channel: z.string(), title: z.string().min(1), content: z.string().min(1), }); let config: WorkflowNodeConfig = { channel: "", title: "", content: "", }; if (data) config = data.config ?? config; const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { channel: config.channel as string, title: config.title as string, content: config.content as string, }, }); const onSubmit = (config: z.infer) => { updateNode({ ...data, config }); hidePanel(); }; return ( <>
{ e.stopPropagation(); form.handleSubmit(onSubmit)(e); }} className="space-y-8" > (
推送渠道
initChannels()} />
设置推送渠道
)} /> ( 标题 )} /> ( 内容