import { memo, useCallback, useEffect, useRef, useState, } from 'react' import { useKeyPress } from 'ahooks' import { RiCloseLine, RiEqualizer2Line } from '@remixicon/react' import { useTranslation } from 'react-i18next' import { useNodes } from 'reactflow' import { useEdgesInteractions, useNodesInteractions, useWorkflowInteractions, } from '../../hooks' import { BlockEnum } from '../../types' import type { StartNodeType } from '../../nodes/start/types' import ChatWrapper from './chat-wrapper' import cn from '@/utils/classnames' import { RefreshCcw01 } from '@/app/components/base/icons/src/vender/line/arrows' import { BubbleX } from '@/app/components/base/icons/src/vender/line/others' import Tooltip from '@/app/components/base/tooltip' import ActionButton, { ActionButtonState } from '@/app/components/base/action-button' import { useStore } from '@/app/components/workflow/store' export type ChatWrapperRefType = { handleRestart: () => void } const DebugAndPreview = () => { const { t } = useTranslation() const chatRef = useRef({ handleRestart: () => { } }) const { handleCancelDebugAndPreviewPanel } = useWorkflowInteractions() const { handleNodeCancelRunningStatus } = useNodesInteractions() const { handleEdgeCancelRunningStatus } = useEdgesInteractions() const varList = useStore(s => s.conversationVariables) const [expanded, setExpanded] = useState(true) const nodes = useNodes() const startNode = nodes.find(node => node.data.type === BlockEnum.Start) const variables = startNode?.data.variables || [] const [showConversationVariableModal, setShowConversationVariableModal] = useState(false) const handleRestartChat = () => { handleNodeCancelRunningStatus() handleEdgeCancelRunningStatus() chatRef.current.handleRestart() } useKeyPress('shift.r', () => { handleRestartChat() }, { exactMatch: true, }) const [panelWidth, setPanelWidth] = useState(420) const [isResizing, setIsResizing] = useState(false) const startResizing = useCallback((e: React.MouseEvent) => { e.preventDefault() setIsResizing(true) }, []) const stopResizing = useCallback(() => { setIsResizing(false) }, []) const resize = useCallback((e: MouseEvent) => { if (isResizing) { const newWidth = window.innerWidth - e.clientX if (newWidth > 420 && newWidth < 1024) setPanelWidth(newWidth) } }, [isResizing]) useEffect(() => { window.addEventListener('mousemove', resize) window.addEventListener('mouseup', stopResizing) return () => { window.removeEventListener('mousemove', resize) window.removeEventListener('mouseup', stopResizing) } }, [resize, stopResizing]) return (
{t('workflow.common.debugAndPreview').toLocaleUpperCase()}
handleRestartChat()}> {varList.length > 0 && ( setShowConversationVariableModal(true)}> )} {variables.length > 0 && (
setExpanded(!expanded)}> {expanded &&
}
)}
setShowConversationVariableModal(false)} showInputsFieldsPanel={expanded} onHide={() => setExpanded(false)} />
) } export default memo(DebugAndPreview)