Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

6.9 KB
Raw Blame
import { cn } from '@langgenius/dify-ui/cn'
import {
  ContextMenuContent,
  ContextMenuGroup,
  ContextMenuItem,
  ContextMenuSeparator,
} from '@langgenius/dify-ui/context-menu'
import { useAtomValue } from 'jotai'
import { useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import {
  editorResourceKindAtom,
  isChatflowAtom,
} from '@/app/components/workflow/editor-resource/state'
import { useHooksStore } from './hooks-store'
import { useDSL } from './hooks/use-DSL'
import { useNodesInteractions } from './hooks/use-nodes-interactions'
import { useWorkflowMoveMode } from './hooks/use-workflow-panel-interactions'
import { useWorkflowStartRun } from './hooks/use-workflow-start-run'
import { TEST_RUN_MENU_HOTKEY } from './hotkeys'
import AddBlock from './operator/add-block'
import { useOperator } from './operator/hooks'
import { handleWorkflowMenuKeyDown } from './shortcuts/handle-workflow-menu-key-down'
import { ShortcutKbd } from './shortcuts/shortcut-kbd'
import { useStore } from './store'
import { WorkflowRunningStatus } from './types'

export function PanelContextmenu({ onClose }: { onClose: () => void }) {
  const { t } = useTranslation(['app', 'workflow', 'workflowComments'])
  const isPanelContextMenu = useStore((s) => s.contextMenuTarget?.type === 'panel')
  const clipboardElements = useStore((s) => s.clipboardElements)
  const setShowImportDSLModal = useStore((s) => s.setShowImportDSLModal)
  const pendingComment = useStore((s) => s.pendingComment)
  const setCommentPlacing = useStore((s) => s.setCommentPlacing)
  const setCommentQuickAdd = useStore((s) => s.setCommentQuickAdd)
  const workflowRunningData = useStore((s) => s.workflowRunningData)
  const historyWorkflowData = useStore((s) => s.historyWorkflowData)
  const isRestoring = useStore((s) => s.isRestoring)
  const { handleNodesPaste } = useNodesInteractions()
  const { handleStartWorkflowRun, handleWorkflowStartRunInChatflow } = useWorkflowStartRun()
  const { handleAddNote } = useOperator()
  const { isCommentModeAvailable } = useWorkflowMoveMode()
  const { exportCheck } = useDSL()
  const accessControl = useHooksStore((s) => s.accessControl)
  const resourceKind = useAtomValue(editorResourceKindAtom)
  const isChatflow = useAtomValue(isChatflowAtom)
  const workflowOperationReadOnly = !!(
    workflowRunningData?.result.status === WorkflowRunningStatus.Running ||
    workflowRunningData?.result.status === WorkflowRunningStatus.Paused ||
    historyWorkflowData ||
    isRestoring
  )
  const canEditWorkflow = accessControl.canEdit && !workflowOperationReadOnly
  const shouldHideImportApp = resourceKind === 'snippet'

  const addBlockTrigger = (
    <ContextMenuItem
      nativeButton
      closeOnClick={false}
      render={<button type="button" />}
      className={cn(
        'w-[calc(100%-8px)]',
        'justify-between gap-4 border-0 bg-transparent px-3 text-left text-text-secondary',
      )}
    >
      {t(($) => $['common.addBlock'], { ns: 'workflow' })}
    </ContextMenuItem>
  )

  const handleRunAction = useCallback(() => {
    if (isChatflow) handleWorkflowStartRunInChatflow()
    else handleStartWorkflowRun()

    onClose()
  }, [isChatflow, handleWorkflowStartRunInChatflow, handleStartWorkflowRun, onClose])

  if (!isPanelContextMenu) return null

  function pasteNodes() {
    if (!clipboardElements.length) return
    handleNodesPaste()
    onClose()
  }

  return (
    <ContextMenuContent
      className="w-50 rounded-lg"
      onKeyDown={(event) =>
        handleWorkflowMenuKeyDown(event, [
          ['workflow.paste', canEditWorkflow && clipboardElements.length ? pasteNodes : undefined],
        ])
      }
    >
      <ContextMenuGroup>
        {canEditWorkflow && (
          <AddBlock
            renderTrigger={addBlockTrigger}
            // The menu row owns the focus treatment, so the trigger adds no ring.
            triggerClassName="focus-visible:ring-0"
            onClose={onClose}
            isolateKeyboardEvents
            sideOffset={-36}
            alignOffset={-4}
          />
        )}
        {canEditWorkflow && (
          <ContextMenuItem
            className="justify-between gap-4 px-3 text-text-secondary"
            onClick={(e) => {
              e.stopPropagation()
              handleAddNote()
              onClose()
            }}
          >
            {t(($) => $['nodes.note.addNote'], { ns: 'workflow' })}
          </ContextMenuItem>
        )}
        {!workflowOperationReadOnly && isCommentModeAvailable && (
          <ContextMenuItem
            disabled={!!pendingComment}
            className={cn(
              'justify-between gap-4 px-3 text-text-secondary',
              pendingComment && 'cursor-not-allowed opacity-50',
            )}
            onClick={(e) => {
              e.stopPropagation()
              if (pendingComment) return
              setCommentQuickAdd(true)
              setCommentPlacing(true)
              onClose()
            }}
          >
            {t(($) => $['comments.actions.addComment'], { ns: 'workflowComments' })}
          </ContextMenuItem>
        )}
        {accessControl.canRun && (
          <ContextMenuItem
            className="justify-between gap-4 px-3 text-text-secondary"
            onClick={handleRunAction}
          >
            {isChatflow
              ? t(($) => $['common.debugAndPreview'], { ns: 'workflow' })
              : t(($) => $['common.run'], { ns: 'workflow' })}
            {!isChatflow && <ShortcutKbd hotkey={TEST_RUN_MENU_HOTKEY} />}
          </ContextMenuItem>
        )}
      </ContextMenuGroup>
      {canEditWorkflow && (
        <>
          <ContextMenuSeparator />
          <ContextMenuGroup>
            <ContextMenuItem
              disabled={!clipboardElements.length}
              className={cn(
                'justify-between gap-4 px-3 text-text-secondary',
                !clipboardElements.length && 'cursor-not-allowed opacity-50',
              )}
              onClick={pasteNodes}
            >
              {t(($) => $['common.pasteHere'], { ns: 'workflow' })}
              <ShortcutKbd shortcut="workflow.paste" />
            </ContextMenuItem>
          </ContextMenuGroup>
        </>
      )}
      {accessControl.canImportExportDSL && (
        <>
          <ContextMenuSeparator />
          <ContextMenuGroup>
            <ContextMenuItem
              className="justify-between gap-4 px-3 text-text-secondary"
              onClick={() => exportCheck?.()}
            >
              {t(($) => $.exportApp, { ns: 'app' })}
            </ContextMenuItem>
            {!shouldHideImportApp && (
              <ContextMenuItem
                className="justify-between gap-4 px-3 text-text-secondary"
                onClick={() => setShowImportDSLModal(true)}
              >
                {t(($) => $.importApp, { ns: 'app' })}
              </ContextMenuItem>
            )}
          </ContextMenuGroup>
        </>
      )}
    </ContextMenuContent>
  )
}