import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form'; import { Input } from '@/components/ui/input'; import { RAGFlowSelect, RAGFlowSelectOptionType } from '@/components/ui/select'; import { Switch } from '@/components/ui/switch'; import { IModalProps } from '@/interfaces/common'; import { zodResolver } from '@hookform/resolvers/zod'; import { isEmpty } from 'lodash'; import { useEffect, useMemo } from 'react'; import { useForm, useWatch } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import { z } from 'zod'; import { BeginQueryType, BeginQueryTypeIconMap } from '../../constant'; import { BeginQuery } from '../../interface'; import { BeginDynamicOptions } from './begin-dynamic-options'; type ModalFormProps = { initialValue: BeginQuery; otherThanCurrentQuery: BeginQuery[]; submit(values: any): void; }; const FormId = 'BeginParameterForm'; function ParameterForm({ initialValue, otherThanCurrentQuery, submit, }: ModalFormProps) { const FormSchema = z.object({ type: z.string(), key: z .string() .trim() .min(1) .refine( (value) => !value || !otherThanCurrentQuery.some((x) => x.key === value), { message: 'The key cannot be repeated!' }, ), optional: z.boolean(), name: z.string().trim().min(1), options: z .array(z.object({ value: z.string().or(z.boolean()).or(z.number()) })) .optional(), }); const form = useForm>({ resolver: zodResolver(FormSchema), mode: 'onChange', defaultValues: { type: BeginQueryType.Line, optional: false, key: '', name: '', options: [], }, }); const options = useMemo(() => { return Object.values(BeginQueryType).reduce( (pre, cur) => { const Icon = BeginQueryTypeIconMap[cur]; return [ ...pre, { label: (
{cur}
), value: cur, }, ]; }, [], ); }, []); const type = useWatch({ control: form.control, name: 'type', }); useEffect(() => { if (!isEmpty(initialValue)) { form.reset({ ...initialValue, options: initialValue.options?.map((x) => ({ value: x })), }); } }, [form, initialValue]); function onSubmit(data: z.infer) { const values = { ...data, options: data.options?.map((x) => x.value) }; console.log('🚀 ~ onSubmit ~ values:', values); submit(values); } return (
( Type )} /> ( Key )} /> ( Name )} /> ( Optional )} /> {type === BeginQueryType.Options && ( )} ); } export function ParameterDialog({ initialValue, hideModal, otherThanCurrentQuery, submit, }: ModalFormProps & IModalProps) { const { t } = useTranslation(); return ( {t('flow.variableSettings')} ); }