'use client' import React, { useCallback } from 'react' import type { ChangeEvent, FC } from 'react' import { useTranslation } from 'react-i18next' import produce from 'immer' import { useBoolean } from 'ahooks' import { RiDeleteBinLine, } from '@remixicon/react' import type { VarGroupItem as VarGroupItemType } from '../types' import VarReferencePicker from '../../_base/components/variable/var-reference-picker' import VarList from '../components/var-list' import Field from '@/app/components/workflow/nodes/_base/components/field' import { VarType } from '@/app/components/workflow/types' import type { NodeOutPutVar, ValueSelector, Var } from '@/app/components/workflow/types' import { VarType as VarKindType } from '@/app/components/workflow/nodes/tool/types' import { Folder } from '@/app/components/base/icons/src/vender/line/files' import { checkKeys, replaceSpaceWithUnderscoreInVarNameInput } from '@/utils/var' import Toast from '@/app/components/base/toast' const i18nPrefix = 'workflow.nodes.variableAssigner' type Payload = VarGroupItemType & { group_name?: string } type Props = { readOnly: boolean nodeId: string payload: Payload onChange: (newPayload: Payload) => void groupEnabled: boolean onGroupNameChange?: (value: string) => void canRemove?: boolean onRemove?: () => void availableVars: NodeOutPutVar[] } const VarGroupItem: FC = ({ readOnly, nodeId, payload, onChange, groupEnabled, onGroupNameChange, canRemove, onRemove, availableVars, }) => { const { t } = useTranslation() const handleAddVariable = useCallback((value: ValueSelector | string, _varKindType: VarKindType, varInfo?: Var) => { const chosenVariables = payload.variables if (chosenVariables.some(item => item.join('.') === (value as ValueSelector).join('.'))) return const newPayload = produce(payload, (draft: Payload) => { draft.variables.push(value as ValueSelector) if (varInfo && varInfo.type !== VarType.any) draft.output_type = varInfo.type }) onChange(newPayload) }, [onChange, payload]) const handleListChange = useCallback((newList: ValueSelector[], changedItem?: ValueSelector) => { if (changedItem) { const chosenVariables = payload.variables if (chosenVariables.some(item => item.join('.') === (changedItem as ValueSelector).join('.'))) return } const newPayload = produce(payload, (draft) => { draft.variables = newList if (newList.length === 0) draft.output_type = VarType.any }) onChange(newPayload) }, [onChange, payload]) const filterVar = useCallback((varPayload: Var) => { if (payload.output_type === VarType.any) return true return varPayload.type === payload.output_type }, [payload.output_type]) const [isEditGroupName, { setTrue: setEditGroupName, setFalse: setNotEditGroupName, }] = useBoolean(false) const handleGroupNameChange = useCallback((e: ChangeEvent) => { replaceSpaceWithUnderscoreInVarNameInput(e.target) const value = e.target.value const { isValid, errorKey, errorMessageKey } = checkKeys([value], false) if (!isValid) { Toast.notify({ type: 'error', message: t(`appDebug.varKeyError.${errorMessageKey}`, { key: errorKey }), }) return } onGroupNameChange?.(value) }, [onGroupNameChange, t]) return (
{(!isEditGroupName) ? (
{payload.group_name}
) : ( )}
{canRemove && (
)} : t(`${i18nPrefix}.title`)!} operations={
{payload.variables.length > 0 && (
{payload.output_type}
)} { !readOnly ? : undefined }
} >
) } export default React.memo(VarGroupItem)