import type { ToastPayload } from './variable-modal.helpers' import type { ConversationVariable } from '@/app/components/workflow/types' import { RiCloseLine } from '@remixicon/react' import * as React from 'react' import { useTranslation } from 'react-i18next' import Button from '@/app/components/base/button' import { toast } from '@/app/components/base/ui/toast' import { ChatVarType } from '@/app/components/workflow/panel/chat-variable-panel/type' import { useWorkflowStore } from '@/app/components/workflow/store' import { cn } from '@/utils/classnames' import { replaceSpaceWithUnderscoreInVarNameInput } from '@/utils/var' import { useVariableModalState } from './use-variable-modal-state' import { getEditorToggleLabelKey, typeList, validateVariableName, } from './variable-modal.helpers' import { DescriptionSection, NameSection, TypeSection, ValueSection, } from './variable-modal.sections' export type ModalPropsType = { chatVar?: ConversationVariable onClose: () => void onSave: (chatVar: ConversationVariable) => void } const ChatVariableModal = ({ chatVar, onClose, onSave, }: ModalPropsType) => { const { t } = useTranslation() const workflowStore = useWorkflowStore() const notify = React.useCallback(({ children, message, type = 'info' }: ToastPayload) => { toast[type](message, children ? { description: children } : undefined) }, []) const { description, editInJSON, editorContent, editorMinHeight, handleEditorChange, handleEditorValueChange, handleSave, handleStringOrNumberChange, handleTypeChange, handleVarNameChange, name, objectValue, placeholder, setDescription, setObjectValue, setValue, type, value, } = useVariableModalState({ chatVar, conversationVariables: workflowStore.getState().conversationVariables, notify, onClose, onSave, t, }) const handleNameChange = (e: React.ChangeEvent) => { replaceSpaceWithUnderscoreInVarNameInput(e.target) if (e.target.value && !validateVariableName({ name: e.target.value, notify, t })) return handleVarNameChange(e) } return (
{!chatVar ? t('chatVariable.modal.title', { ns: 'workflow' }) : t('chatVariable.modal.editTitle', { ns: 'workflow' })}
validateVariableName({ name: nextName, notify, t })} onChange={handleNameChange} placeholder={t('chatVariable.modal.namePlaceholder', { ns: 'workflow' }) || ''} title={t('chatVariable.modal.name', { ns: 'workflow' })} />
) } export default ChatVariableModal