import { ConfirmDeleteDialog } from '@/components/confirm-delete-dialog'; import { DynamicForm, DynamicFormRef, FormFieldConfig, } from '@/components/dynamic-form'; import { Button } from '@/components/ui/button'; import { Modal } from '@/components/ui/modal/modal'; import { Sheet, SheetContent, SheetHeader, SheetTitle, } from '@/components/ui/sheet'; import { useSetModalState } from '@/hooks/common-hooks'; import { useFetchAgent } from '@/hooks/use-agent-request'; import { GobalVariableType } from '@/interfaces/database/agent'; import { cn } from '@/lib/utils'; import { t } from 'i18next'; import { Trash2 } from 'lucide-react'; import { useEffect, useRef, useState } from 'react'; import { FieldValues } from 'react-hook-form'; import { useSaveGraph } from '../hooks/use-save-graph'; import { GobalFormFields, GobalVariableFormDefaultValues, TypeMaps, TypesWithArray, } from './contant'; export type IGobalParamModalProps = { data: any; hideModal: (open: boolean) => void; }; export const GobalParamSheet = (props: IGobalParamModalProps) => { const { hideModal } = props; const { data, refetch } = useFetchAgent(); const [fields, setFields] = useState(GobalFormFields); const { visible, showModal, hideModal: hideAddModal } = useSetModalState(); const [defaultValues, setDefaultValues] = useState( GobalVariableFormDefaultValues, ); const formRef = useRef(null); const handleFieldUpdate = ( fieldName: string, updatedField: Partial, ) => { setFields((prevFields) => prevFields.map((field) => field.name === fieldName ? { ...field, ...updatedField } : field, ), ); }; useEffect(() => { const typefileld = fields.find((item) => item.name === 'type'); if (typefileld) { typefileld.onChange = (value) => { // setWatchType(value); handleFieldUpdate('value', { type: TypeMaps[value as keyof typeof TypeMaps], }); const values = formRef.current?.getValues(); setTimeout(() => { switch (value) { case TypesWithArray.Boolean: setDefaultValues({ ...values, value: false }); break; case TypesWithArray.Number: setDefaultValues({ ...values, value: 0 }); break; default: setDefaultValues({ ...values, value: '' }); } }, 0); }; } }, [fields]); const { saveGraph, loading } = useSaveGraph(); const handleSubmit = async (value: FieldValues) => { const param = { ...(data.dsl?.variables || {}), [value.name]: value, } as Record; const res = await saveGraph(undefined, { gobalVariables: param, }); if (res.code === 0) { refetch(); } hideAddModal(); }; const handleDeleteGobalVariable = async (key: string) => { const param = { ...(data.dsl?.variables || {}), } as Record; delete param[key]; const res = await saveGraph(undefined, { gobalVariables: param, }); console.log('delete gobal variable-->', res); if (res.code === 0) { refetch(); } }; const handleEditGobalVariable = (item: FieldValues) => { setDefaultValues(item); showModal(); }; return ( <> e.preventDefault()} > {t('flow.gobalVariable')}
{data?.dsl?.variables && Object.keys(data.dsl.variables).map((key) => { const item = data.dsl.variables[key]; return (
{ handleEditGobalVariable(item); }} >
{item.name} {item.type}
{item.value}
handleDeleteGobalVariable(key)} >
); })}
{ console.log(data); }} defaultValues={defaultValues} onFieldUpdate={handleFieldUpdate} >
{ hideAddModal?.(); }} /> { handleSubmit(values); // console.log(values); // console.log(nodes, edges); // handleOk(values); }} />
); };