mirror of
https://github.com/langgenius/dify.git
synced 2026-06-24 04:51:11 +08:00
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: fatelei <fatelei@gmail.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: 盐粒 Yanli <yanli@dify.ai> Co-authored-by: Charles Yao <chongbinyao33@gmail.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> Co-authored-by: yunlu.wen <yunlu.wen@dify.ai> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: Jingyi <jingyi.qi@dify.ai> Co-authored-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: hjlarry <hjlarry@163.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com> Co-authored-by: gigglewang <gigglewang@dify.ai> Co-authored-by: chariri <w@chariri.moe> Co-authored-by: Evan <2869018789@qq.com> Co-authored-by: zyssyz123 <916125788@qq.com>
111 lines
3.5 KiB
TypeScript
111 lines
3.5 KiB
TypeScript
import type { Node } from '@/app/components/workflow/types'
|
|
import { useCallback, useMemo } from 'react'
|
|
import { useEdges } from 'reactflow'
|
|
import { CollectionType } from '@/app/components/tools/types'
|
|
import {
|
|
useNodeMetaData,
|
|
useNodesInteractions,
|
|
useNodesReadOnly,
|
|
} from '@/app/components/workflow/hooks'
|
|
import { useHooksStore } from '@/app/components/workflow/hooks-store'
|
|
import { useWorkflowStore } from '@/app/components/workflow/store'
|
|
import { BlockEnum, NodeRunningStatus } from '@/app/components/workflow/types'
|
|
import { canRunBySingle } from '@/app/components/workflow/utils'
|
|
import { useAllWorkflowTools } from '@/service/use-tools'
|
|
import { canFindTool } from '@/utils'
|
|
|
|
type UseNodeActionsMenuModelParams = {
|
|
id: string
|
|
data: Node['data']
|
|
onClose: () => void
|
|
showHelpLink?: boolean
|
|
}
|
|
|
|
export function useNodeActionsMenuModel({
|
|
id,
|
|
data,
|
|
onClose,
|
|
showHelpLink = true,
|
|
}: UseNodeActionsMenuModelParams) {
|
|
const edges = useEdges()
|
|
const {
|
|
handleNodeDelete,
|
|
handleNodesDuplicate,
|
|
handleNodeSelect,
|
|
handleNodesCopy,
|
|
} = useNodesInteractions()
|
|
const workflowStore = useWorkflowStore()
|
|
const { nodesReadOnly } = useNodesReadOnly()
|
|
const canRunWorkflow = useHooksStore(s => s.accessControl.canRun)
|
|
const nodeMetaData = useNodeMetaData({ id, data } as Node)
|
|
const { data: workflowTools } = useAllWorkflowTools()
|
|
|
|
const isChildNode = !!(data.isInIteration || data.isInLoop)
|
|
const canRun = canRunWorkflow && !nodesReadOnly && canRunBySingle(data.type, isChildNode)
|
|
const isSingleRunning = data._singleRunningStatus === NodeRunningStatus.Running
|
|
const canChangeBlock = !nodeMetaData.isTypeFixed && !nodeMetaData.isUndeletable && !nodesReadOnly
|
|
const sourceHandle = useMemo(() => {
|
|
return edges.find(edge => edge.target === id)?.sourceHandle || 'source'
|
|
}, [edges, id])
|
|
|
|
const workflowAppHref = useMemo(() => {
|
|
const isWorkflowTool = data.type === BlockEnum.Tool && data.provider_type === CollectionType.workflow
|
|
if (!isWorkflowTool || !workflowTools || !data.provider_id)
|
|
return undefined
|
|
|
|
const workflowTool = workflowTools.find(item => canFindTool(item.id, data.provider_id))
|
|
if (!workflowTool?.workflow_app_id)
|
|
return undefined
|
|
|
|
return `/app/${workflowTool.workflow_app_id}/workflow`
|
|
}, [data.provider_id, data.provider_type, data.type, workflowTools])
|
|
|
|
const handleRun = useCallback(() => {
|
|
const store = workflowStore.getState()
|
|
store.setInitShowLastRunTab(true)
|
|
store.setPendingSingleRun({
|
|
nodeId: id,
|
|
action: isSingleRunning ? 'stop' : 'run',
|
|
})
|
|
handleNodeSelect(id)
|
|
onClose()
|
|
}, [handleNodeSelect, id, isSingleRunning, onClose, workflowStore])
|
|
|
|
const handleCopy = useCallback(() => {
|
|
onClose()
|
|
handleNodesCopy(id)
|
|
}, [handleNodesCopy, id, onClose])
|
|
|
|
const handleDuplicate = useCallback(() => {
|
|
onClose()
|
|
handleNodesDuplicate(id)
|
|
}, [handleNodesDuplicate, id, onClose])
|
|
|
|
const handleDelete = useCallback(() => {
|
|
onClose()
|
|
handleNodeDelete(id)
|
|
}, [handleNodeDelete, id, onClose])
|
|
|
|
return {
|
|
about: {
|
|
author: nodeMetaData.author,
|
|
description: nodeMetaData.description,
|
|
},
|
|
canChangeBlock,
|
|
canRun,
|
|
data,
|
|
handleCopy,
|
|
handleDelete,
|
|
handleDuplicate,
|
|
handleRun,
|
|
helpLinkUri: showHelpLink ? nodeMetaData.helpLinkUri : undefined,
|
|
id,
|
|
isSingleton: nodeMetaData.isSingleton,
|
|
isSingleRunning,
|
|
isUndeletable: nodeMetaData.isUndeletable,
|
|
nodesReadOnly,
|
|
sourceHandle,
|
|
workflowAppHref,
|
|
}
|
|
}
|