import { useRef } from 'react' import { useTranslation } from 'react-i18next' import clsx from 'clsx'; import type { ReactShapeConfig } from '@antv/x6-react-shape'; import { Flex } from 'antd'; import NodeTools from './NodeTools' import { useVariableList } from '../Properties/hooks/useVariableList' const caculateIsSet = (item: any, type: string) => { switch (type) { case 'categories': return typeof item?.class_name === 'string' && item?.class_name !== '' case 'cases': { if (!item.left) return false if (['not_empty', 'empty'].includes(item.operator)) return true return !!item.left && (!!item.right || typeof item.right === 'boolean' || typeof item.right === 'number') } } } const ConditionNode: ReactShapeConfig['component'] = ({ node }) => { const data = node?.getData() || {}; const { t } = useTranslation() const graphRef = useRef(node?.model?.graph) const variableList = useVariableList(node ?? null, graphRef, data.chatVariables ?? []) const getLocaleField = (field: string, filedType: string) => { const key = filedType === 'boolean' ? `workflow.config.if-else..boolean.${field}` : filedType === 'number' ? `workflow.config.if-else.num.${field}` : `workflow.config.if-else.${field}` const value = t(key) return value !== key ? value : t(`workflow.config.if-else.num.${field}`) }; const labelRender = (value: string) => { const filterOption = variableList.find(vo => `{{${vo.value}}}` === value) if (filterOption) { return ( {`{x}`} {filterOption.label} ) } return null } return (
{data.name ?? t(`workflow.${data.type}`)}
{data.type === 'question-classifier' && {data.config?.categories?.defaultValue.map((item: any, index: number) => (
{t('workflow.config.question-classifier.class_name')} {index + 1} {caculateIsSet(item, 'categories') ? t(`workflow.config.${data.type}.set`) : t(`workflow.config.${data.type}.unset`)}
))}
} {data.type === 'if-else' && {data.config?.cases?.defaultValue.map((item: any, index: number) => (
0 ? '' : 'rb:mb-1'}> 0 ? "space-between" : 'end'} className="rb:mb-1"> {item.expressions.length > 0 && CASE{index + 1}} {index === 0 ? 'IF' : `ELIF`} {item.expressions.length > 0 && {item.expressions.map((expression: any, eIndex: number) => (
{item.expressions.length > 1 && eIndex > 0 &&
{item.logical_operator?.toLocaleUpperCase()}
} {caculateIsSet(expression, 'cases') ? <> {labelRender(expression.left)} {getLocaleField(expression.operator, typeof expression.right)} {!['not_empty', 'empty'].includes(expression.operator) && {typeof expression.right === 'boolean' ? String(expression.right).charAt(0).toUpperCase() + String(expression.right).slice(1) : expression.right}} : t(`workflow.config.${data.type}.unset`) }
))}
}
))} ELSE
}
); }; export default ConditionNode;