import type { FC } from 'react' import type { CommonNodeType } from '@/app/components/workflow/types' import { cn } from '@langgenius/dify-ui/cn' import { Tooltip, TooltipContent, TooltipTrigger } from '@langgenius/dify-ui/tooltip' import { RiLoader2Line, RiStopCircleFill } from '@remixicon/react' import { useMemo } from 'react' import { useTranslation } from 'react-i18next' import { useNodes } from 'reactflow' import { NodeRunningStatus, WorkflowRunningStatus } from '@/app/components/workflow/types' import { EVENT_WORKFLOW_STOP } from '@/app/components/workflow/variable-inspect/types' import { useEventEmitterContextContext } from '@/context/event-emitter' import useCurrentVars from '../hooks/use-inspect-vars-crud' import { useNodesReadOnly } from '../hooks/use-workflow' import { useStore } from '../store' const VariableInspectTrigger: FC = () => { const { t } = useTranslation() const { eventEmitter } = useEventEmitterContextContext() const showVariableInspectPanel = useStore((s) => s.showVariableInspectPanel) const setShowVariableInspectPanel = useStore((s) => s.setShowVariableInspectPanel) const environmentVariables = useStore((s) => s.environmentVariables) const setCurrentFocusNodeId = useStore((s) => s.setCurrentFocusNodeId) const { conversationVars, systemVars, nodesWithInspectVars, deleteAllInspectorVars } = useCurrentVars() const currentVars = useMemo(() => { const allVars = [ ...environmentVariables, ...conversationVars, ...systemVars, ...nodesWithInspectVars, ] return allVars }, [environmentVariables, conversationVars, systemVars, nodesWithInspectVars]) const { nodesReadOnly, getNodesReadOnly } = useNodesReadOnly() const workflowRunningData = useStore((s) => s.workflowRunningData) const nodes = useNodes() const isStepRunning = useMemo( () => nodes.some((node) => node.data._singleRunningStatus === NodeRunningStatus.Running), [nodes], ) const isPreviewRunning = useMemo(() => { if (!workflowRunningData) return false return workflowRunningData.result.status === WorkflowRunningStatus.Running }, [workflowRunningData]) const isRunning = useMemo( () => isPreviewRunning || isStepRunning, [isPreviewRunning, isStepRunning], ) const handleStop = () => { eventEmitter?.emit({ type: EVENT_WORKFLOW_STOP, } as any) } const handleClearAll = () => { deleteAllInspectorVars() setCurrentFocusNodeId('') } if (showVariableInspectPanel) return null return (
{!isRunning && !currentVars.length && (
{ if (getNodesReadOnly()) return setShowVariableInspectPanel(true) }} > {t(($) => $['debug.variableInspect.trigger.normal'], { ns: 'workflow' })}
)} {!isRunning && currentVars.length > 0 && ( <>
{ if (getNodesReadOnly()) return setShowVariableInspectPanel(true) }} > {t(($) => $['debug.variableInspect.trigger.cached'], { ns: 'workflow' })}
{ if (getNodesReadOnly()) return handleClearAll() }} > {t(($) => $['debug.variableInspect.trigger.clear'], { ns: 'workflow' })}
)} {isRunning && ( <>
setShowVariableInspectPanel(true)} > {t(($) => $['debug.variableInspect.trigger.running'], { ns: 'workflow' })}
{isPreviewRunning && (
} /> {t(($) => $['debug.variableInspect.trigger.stop'], { ns: 'workflow' })} )} )} ) } export default VariableInspectTrigger