From 581228be741e432e4834148b908f87a86ffc89fd Mon Sep 17 00:00:00 2001 From: Joel Date: Fri, 16 Aug 2024 10:14:43 +0800 Subject: [PATCH] feat: abstract condition logic to components --- .../if-else/components/condition-wrap.tsx | 147 ++++++++++++++++++ .../workflow/nodes/if-else/panel.tsx | 112 ++----------- .../workflow/nodes/if-else/use-config.ts | 4 +- 3 files changed, 166 insertions(+), 97 deletions(-) create mode 100644 web/app/components/workflow/nodes/if-else/components/condition-wrap.tsx diff --git a/web/app/components/workflow/nodes/if-else/components/condition-wrap.tsx b/web/app/components/workflow/nodes/if-else/components/condition-wrap.tsx new file mode 100644 index 0000000000..a535b549e0 --- /dev/null +++ b/web/app/components/workflow/nodes/if-else/components/condition-wrap.tsx @@ -0,0 +1,147 @@ +'use client' +import type { FC } from 'react' +import React, { useCallback, useState } from 'react' +import { useTranslation } from 'react-i18next' +import { ReactSortable } from 'react-sortablejs' +import { + RiDeleteBinLine, + RiDraggable, +} from '@remixicon/react' +import type { CaseItem, HandleAddCondition, HandleRemoveCondition, HandleUpdateCondition, HandleUpdateConditionLogicalOperator } from '../types' +import type { Node, NodeOutPutVar, Var } from '../../../types' +import { VarType } from '../../../types' +import { useGetAvailableVars } from '../../variable-assigner/hooks' +import ConditionList from './condition-list' +import ConditionAdd from './condition-add' +import cn from '@/utils/classnames' +import Button from '@/app/components/base/button' + +type Props = { + nodeId: string + cases: CaseItem[] + readOnly: boolean + handleSortCase?: (sortedCases: (CaseItem & { id: string })[]) => void + handleRemoveCase: (caseId: string) => void + handleAddCondition: HandleAddCondition + handleUpdateCondition: HandleUpdateCondition + handleRemoveCondition: HandleRemoveCondition + handleUpdateConditionLogicalOperator: HandleUpdateConditionLogicalOperator + nodesOutputVars: NodeOutPutVar[] + availableNodes: Node[] + varsIsVarFileAttribute: Record + filterVar: (varPayload: Var) => boolean +} + +const ConditionWrap: FC = ({ + nodeId: id, + cases, + readOnly, + handleSortCase, + handleUpdateCondition, + handleRemoveCondition, + handleUpdateConditionLogicalOperator, + nodesOutputVars, + availableNodes, + varsIsVarFileAttribute, + filterVar, + handleAddCondition, + handleRemoveCase, +}) => { + const { t } = useTranslation() + + const getAvailableVars = useGetAvailableVars() + + const [willDeleteCaseId, setWillDeleteCaseId] = useState('') + const casesLength = cases.length + + const filterNumberVar = useCallback((varPayload: Var) => { + return varPayload.type === VarType.number + }, []) + + return ( + ({ ...caseItem, id: caseItem.case_id }))} + setList={handleSortCase} + handle='.handle' + ghostClass='bg-components-panel-bg' + animation={150} + > + { + cases.map((item, index) => ( +
+
+ 1 && 'group-hover:block', + )} /> +
+ { + index === 0 ? 'IF' : 'ELIF' + } + { + casesLength > 1 && ( +
CASE {index + 1}
+ ) + } +
+ { + !!item.conditions.length && ( +
+ +
+ ) + } +
+ + { + ((index === 0 && casesLength > 1) || (index > 0)) && ( + + ) + } +
+
+
+
+ )) + } +
+ ) +} +export default React.memo(ConditionWrap) diff --git a/web/app/components/workflow/nodes/if-else/panel.tsx b/web/app/components/workflow/nodes/if-else/panel.tsx index 111219118d..ba30f9be0d 100644 --- a/web/app/components/workflow/nodes/if-else/panel.tsx +++ b/web/app/components/workflow/nodes/if-else/panel.tsx @@ -1,24 +1,18 @@ import type { FC } from 'react' import { memo, - useState, } from 'react' import { useTranslation } from 'react-i18next' -import { ReactSortable } from 'react-sortablejs' import { RiAddLine, - RiDeleteBinLine, - RiDraggable, } from '@remixicon/react' import useConfig from './use-config' -import ConditionAdd from './components/condition-add' -import ConditionList from './components/condition-list' import type { IfElseNodeType } from './types' +import ConditionWrap from './components/condition-wrap' import Button from '@/app/components/base/button' import type { NodePanelProps } from '@/app/components/workflow/types' import Field from '@/app/components/workflow/nodes/_base/components/field' -import { useGetAvailableVars } from '@/app/components/workflow/nodes/variable-assigner/hooks' -import cn from '@/utils/classnames' + const i18nPrefix = 'workflow.nodes.ifElse' const Panel: FC> = ({ @@ -26,12 +20,10 @@ const Panel: FC> = ({ data, }) => { const { t } = useTranslation() - const getAvailableVars = useGetAvailableVars() const { readOnly, inputs, filterVar, - filterNumberVar, handleAddCase, handleRemoveCase, handleSortCase, @@ -43,95 +35,25 @@ const Panel: FC> = ({ availableNodes, varsIsVarFileAttribute, } = useConfig(id, data) - const [willDeleteCaseId, setWillDeleteCaseId] = useState('') const cases = inputs.cases || [] - const casesLength = cases.length return (
- ({ ...caseItem, id: caseItem.case_id }))} - setList={handleSortCase} - handle='.handle' - ghostClass='bg-components-panel-bg' - animation={150} - > - { - cases.map((item, index) => ( -
-
- 1 && 'group-hover:block', - )} /> -
- { - index === 0 ? 'IF' : 'ELIF' - } - { - casesLength > 1 && ( -
CASE {index + 1}
- ) - } -
- { - !!item.conditions.length && ( -
- -
- ) - } -
- - { - ((index === 0 && casesLength > 1) || (index > 0)) && ( - - ) - } -
-
-
-
- )) - } -
+