'use client' import React, { useCallback } from 'react' import { useMount } from 'ahooks' import { useTranslation } from 'react-i18next' import { capitalize } from 'lodash-es' import copy from 'copy-to-clipboard' import { RiCloseLine } from '@remixicon/react' import Modal from '@/app/components/base/modal' import { BubbleX } from '@/app/components/base/icons/src/vender/line/others' import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor' import { Clipboard, ClipboardCheck, } from '@/app/components/base/icons/src/vender/line/files' import { useStore } from '@/app/components/workflow/store' import type { ConversationVariable, } from '@/app/components/workflow/types' import { ChatVarType } from '@/app/components/workflow/panel/chat-variable-panel/type' import { CodeLanguage } from '@/app/components/workflow/nodes/code/types' import useTimestamp from '@/hooks/use-timestamp' import { fetchCurrentValueOfConversationVariable } from '@/service/workflow' import cn from '@/utils/classnames' export type Props = { conversationID: string onHide: () => void } const ConversationVariableModal = ({ conversationID, onHide, }: Props) => { const { t } = useTranslation() const { formatTime } = useTimestamp() const varList = useStore(s => s.conversationVariables) as ConversationVariable[] const appID = useStore(s => s.appId) const [currentVar, setCurrentVar] = React.useState(varList[0]) const [latestValueMap, setLatestValueMap] = React.useState>({}) const [latestValueTimestampMap, setLatestValueTimestampMap] = React.useState>({}) const getChatVarLatestValues = useCallback(async () => { if (conversationID && varList.length > 0) { const res = await fetchCurrentValueOfConversationVariable({ url: `/apps/${appID}/conversation-variables`, params: { conversation_id: conversationID }, }) if (res.data.length > 0) { const valueMap = res.data.reduce((acc: any, cur) => { acc[cur.id] = cur.value return acc }, {}) setLatestValueMap(valueMap) const timestampMap = res.data.reduce((acc: any, cur) => { acc[cur.id] = cur.updated_at return acc }, {}) setLatestValueTimestampMap(timestampMap) } } }, [appID, conversationID, varList.length]) const [isCopied, setIsCopied] = React.useState(false) const handleCopy = useCallback(() => { copy(currentVar.value) setIsCopied(true) setTimeout(() => { setIsCopied(false) }, 2000) }, [currentVar.value]) useMount(() => { getChatVarLatestValues() }) return ( { }} className={cn('w-[920px] max-w-[920px] h-[640px] p-0')} >
{/* LEFT */}
{t('workflow.chatVariable.panelTitle')}
{varList.map(chatVar => (
setCurrentVar(chatVar)}>
{chatVar.name}
))}
{/* RIGHT */}
{currentVar.name}
{capitalize(currentVar.value_type)}
{t('workflow.chatVariable.storedContent').toLocaleUpperCase()}
{latestValueTimestampMap[currentVar.id] && (
{t('workflow.chatVariable.updatedAt')}{formatTime(latestValueTimestampMap[currentVar.id], t('appLog.dateTimeFormat') as string)}
)}
{currentVar.value_type !== ChatVarType.Number && currentVar.value_type !== ChatVarType.String && (
JSON
{!isCopied ? ( ) : ( ) }
)} {(currentVar.value_type === ChatVarType.Number || currentVar.value_type === ChatVarType.String) && (
{latestValueMap[currentVar.id] || ''}
)}
) } export default ConversationVariableModal