import { ConfirmDeleteDialog } from '@/components/confirm-delete-dialog'; import { FormFieldConfig } from '@/components/dynamic-form'; import { BlockButton, Button } from '@/components/ui/button'; import { Sheet, SheetContent, SheetHeader, SheetTitle, } from '@/components/ui/sheet'; import { useSetModalState } from '@/hooks/common-hooks'; import { useFetchAgent } from '@/hooks/use-agent-request'; import { GlobalVariableType } from '@/interfaces/database/agent'; import { cn } from '@/lib/utils'; import { t } from 'i18next'; import { Trash2 } from 'lucide-react'; import { useState } from 'react'; import { FieldValues } from 'react-hook-form'; import { useSaveGraph } from '../hooks/use-save-graph'; import { AddVariableModal } from './component/add-variable-modal'; import { GlobalFormFields, GlobalVariableFormDefaultValues, TypeMaps, TypesWithArray, } from './constant'; import { useObjectFields } from './hooks/use-object-fields'; export type IGlobalParamModalProps = { data: any; hideModal: (open: boolean) => void; }; export const GlobalParamSheet = (props: IGlobalParamModalProps) => { const { hideModal } = props; const { data, refetch } = useFetchAgent(); const { visible, showModal, hideModal: hideAddModal } = useSetModalState(); const [fields, setFields] = useState(GlobalFormFields); const [defaultValues, setDefaultValues] = useState( GlobalVariableFormDefaultValues, ); const { handleCustomValidate, handleCustomSchema, handleRender } = useObjectFields(); const { saveGraph } = useSaveGraph(); const handleDeleteGlobalVariable = async (key: string) => { const param = { ...(data.dsl?.variables || {}), } as Record; delete param[key]; const res = await saveGraph(undefined, { globalVariables: param, }); if (res.code === 0) { refetch(); } }; const handleEditGlobalVariable = (item: FieldValues) => { const newFields = fields.map((field) => { let newField = field; newField.render = undefined; newField.schema = undefined; newField.customValidate = undefined; if (newField.name === 'value') { newField = { ...newField, type: TypeMaps[item.type as keyof typeof TypeMaps], render: handleRender(item.type), customValidate: handleCustomValidate(item.type), schema: handleCustomSchema(item.type), }; } return newField; }); setFields(newFields); setDefaultValues(item); showModal(); }; return ( <> e.preventDefault()} > {t('flow.conversationVariable')}
{ setFields(GlobalFormFields); setDefaultValues(GlobalVariableFormDefaultValues); showModal(); }} > {t('flow.add')}
{data?.dsl?.variables && Object.keys(data.dsl.variables).map((key) => { const item = data.dsl.variables[key]; return (
{ handleEditGlobalVariable(item); }} >
{item.name} {item.type}
{![ TypesWithArray.Object, TypesWithArray.ArrayObject, TypesWithArray.ArrayString, TypesWithArray.ArrayNumber, TypesWithArray.ArrayBoolean, ].includes(item.type as TypesWithArray) && (
{item.value}
)}
handleDeleteGlobalVariable(key)} >
); })}
); };