diff --git a/web/src/pages/agent/form/switch-form/index.tsx b/web/src/pages/agent/form/switch-form/index.tsx index e8d6af692..6d4394c25 100644 --- a/web/src/pages/agent/form/switch-form/index.tsx +++ b/web/src/pages/agent/form/switch-form/index.tsx @@ -16,7 +16,7 @@ import { ISwitchForm } from '@/interfaces/database/flow'; import { cn } from '@/lib/utils'; import { zodResolver } from '@hookform/resolvers/zod'; import { X } from 'lucide-react'; -import { useMemo } from 'react'; +import { useCallback, useMemo } from 'react'; import { useFieldArray, useForm, useFormContext } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import { z } from 'zod'; @@ -26,10 +26,7 @@ import { SwitchOperatorOptions, } from '../../constant'; import { useBuildFormSelectOptions } from '../../form-hooks'; -import { - useBuildComponentIdAndBeginOptions, - useBuildVariableOptions, -} from '../../hooks/use-get-begin-query'; +import { useBuildComponentIdAndBeginOptions } from '../../hooks/use-get-begin-query'; import { IOperatorForm } from '../../interface'; import { useValues } from './use-values'; @@ -38,9 +35,39 @@ const ItemKey = 'items'; type ConditionCardsProps = { name: string; + removeParent(index: number): void; + parentIndex: number; + parentLength: number; } & IOperatorForm; -function ConditionCards({ name: parentName, node }: ConditionCardsProps) { +function useBuildSwitchOperatorOptions() { + const { t } = useTranslation(); + + const switchOperatorOptions = useMemo(() => { + return SwitchOperatorOptions.map((x) => ({ + value: x.value, + icon: ( + ', + })} + > + ), + label: t(`flow.switchOperatorOptions.${x.label}`), + })); + }, [t]); + + return switchOperatorOptions; +} + +function ConditionCards({ + name: parentName, + node, + parentIndex, + removeParent, + parentLength, +}: ConditionCardsProps) { const form = useFormContext(); const { t } = useTranslation(); @@ -49,18 +76,7 @@ function ConditionCards({ name: parentName, node }: ConditionCardsProps) { node?.parentId, ); - const switchOperatorOptions = useMemo(() => { - return SwitchOperatorOptions.map((x) => ({ - value: x.value, - icon: ( - ' })} - > - ), - label: t(`flow.switchOperatorOptions.${x.label}`), - })); - }, [t]); + const switchOperatorOptions = useBuildSwitchOperatorOptions(); const name = `${parentName}.${ItemKey}`; @@ -69,12 +85,30 @@ function ConditionCards({ name: parentName, node }: ConditionCardsProps) { control: form.control, }); + const handleRemove = useCallback( + (index: number) => () => { + remove(index); + if (parentIndex !== 0 && index === 0 && parentLength === 1) { + removeParent(parentIndex); + } + }, + [parentIndex, parentLength, remove, removeParent], + ); + return (
{fields.map((field, index) => { return (
- +
-
@@ -150,6 +184,7 @@ function ConditionCards({ name: parentName, node }: ConditionCardsProps) { const SwitchForm = ({ node }: IOperatorForm) => { const { t } = useTranslation(); const values = useValues(); + const switchOperatorOptions = useBuildSwitchOperatorOptions(); const FormSchema = z.object({ conditions: z.array( @@ -199,8 +234,6 @@ const SwitchForm = ({ node }: IOperatorForm) => { })); }, [t]); - const componentIdOptions = useBuildVariableOptions(node?.id, node?.parentId); - return (
{ {fields.map((field, index) => { return ( -
IF
-
- ( - - - - - - - )} - /> +
{index === 0 ? 'IF' : 'ELSEIF'}
+
+
+
+ ( + + + + + + + )} + /> +
+
@@ -239,7 +279,14 @@ const SwitchForm = ({ node }: IOperatorForm) => { })} - append({ logical_operator: SwitchLogicOperatorOptions[0] }) + append({ + logical_operator: SwitchLogicOperatorOptions[0], + [ItemKey]: [ + { + operator: switchOperatorOptions[0].value, + }, + ], + }) } > add