mirror of
https://github.com/langgenius/dify.git
synced 2025-11-16 03:13:19 +00:00
Signed-off-by: lyzno1 <yuanyouhuilyz@gmail.com> Co-authored-by: Stream <Stream_2@qq.com> Co-authored-by: lyzno1 <92089059+lyzno1@users.noreply.github.com> Co-authored-by: zhsama <torvalds@linux.do> Co-authored-by: Harry <xh001x@hotmail.com> Co-authored-by: lyzno1 <yuanyouhuilyz@gmail.com> Co-authored-by: yessenia <yessenia.contact@gmail.com> Co-authored-by: hjlarry <hjlarry@163.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: WTW0313 <twwu@dify.ai> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
120 lines
3.4 KiB
TypeScript
120 lines
3.4 KiB
TypeScript
import {
|
|
memo,
|
|
} from 'react'
|
|
import { produce } from 'immer'
|
|
import {
|
|
useReactFlow,
|
|
useStoreApi,
|
|
useViewport,
|
|
} from 'reactflow'
|
|
import { useEventListener } from 'ahooks'
|
|
import {
|
|
useStore,
|
|
useWorkflowStore,
|
|
} from './store'
|
|
import { WorkflowHistoryEvent, useAutoGenerateWebhookUrl, useNodesInteractions, useNodesSyncDraft, useWorkflowHistory } from './hooks'
|
|
import { CUSTOM_NODE } from './constants'
|
|
import { getIterationStartNode, getLoopStartNode } from './utils'
|
|
import CustomNode from './nodes'
|
|
import CustomNoteNode from './note-node'
|
|
import { CUSTOM_NOTE_NODE } from './note-node/constants'
|
|
import { BlockEnum } from './types'
|
|
|
|
const CandidateNode = () => {
|
|
const store = useStoreApi()
|
|
const reactflow = useReactFlow()
|
|
const workflowStore = useWorkflowStore()
|
|
const candidateNode = useStore(s => s.candidateNode)
|
|
const mousePosition = useStore(s => s.mousePosition)
|
|
const { zoom } = useViewport()
|
|
const { handleNodeSelect } = useNodesInteractions()
|
|
const { saveStateToHistory } = useWorkflowHistory()
|
|
const { handleSyncWorkflowDraft } = useNodesSyncDraft()
|
|
const autoGenerateWebhookUrl = useAutoGenerateWebhookUrl()
|
|
|
|
useEventListener('click', (e) => {
|
|
const { candidateNode, mousePosition } = workflowStore.getState()
|
|
|
|
if (candidateNode) {
|
|
e.preventDefault()
|
|
const {
|
|
getNodes,
|
|
setNodes,
|
|
} = store.getState()
|
|
const { screenToFlowPosition } = reactflow
|
|
const nodes = getNodes()
|
|
const { x, y } = screenToFlowPosition({ x: mousePosition.pageX, y: mousePosition.pageY })
|
|
const newNodes = produce(nodes, (draft) => {
|
|
draft.push({
|
|
...candidateNode,
|
|
data: {
|
|
...candidateNode.data,
|
|
_isCandidate: false,
|
|
},
|
|
position: {
|
|
x,
|
|
y,
|
|
},
|
|
})
|
|
if (candidateNode.data.type === BlockEnum.Iteration)
|
|
draft.push(getIterationStartNode(candidateNode.id))
|
|
|
|
if (candidateNode.data.type === BlockEnum.Loop)
|
|
draft.push(getLoopStartNode(candidateNode.id))
|
|
})
|
|
setNodes(newNodes)
|
|
if (candidateNode.type === CUSTOM_NOTE_NODE)
|
|
saveStateToHistory(WorkflowHistoryEvent.NoteAdd, { nodeId: candidateNode.id })
|
|
else
|
|
saveStateToHistory(WorkflowHistoryEvent.NodeAdd, { nodeId: candidateNode.id })
|
|
|
|
workflowStore.setState({ candidateNode: undefined })
|
|
|
|
if (candidateNode.type === CUSTOM_NOTE_NODE)
|
|
handleNodeSelect(candidateNode.id)
|
|
|
|
if (candidateNode.data.type === BlockEnum.TriggerWebhook) {
|
|
handleSyncWorkflowDraft(true, true, {
|
|
onSuccess: () => autoGenerateWebhookUrl(candidateNode.id),
|
|
})
|
|
}
|
|
}
|
|
})
|
|
|
|
useEventListener('contextmenu', (e) => {
|
|
const { candidateNode } = workflowStore.getState()
|
|
if (candidateNode) {
|
|
e.preventDefault()
|
|
workflowStore.setState({ candidateNode: undefined })
|
|
}
|
|
})
|
|
|
|
if (!candidateNode)
|
|
return null
|
|
|
|
return (
|
|
<div
|
|
className='absolute z-10'
|
|
style={{
|
|
left: mousePosition.elementX,
|
|
top: mousePosition.elementY,
|
|
transform: `scale(${zoom})`,
|
|
transformOrigin: '0 0',
|
|
}}
|
|
>
|
|
{
|
|
candidateNode.type === CUSTOM_NODE && (
|
|
<CustomNode {...candidateNode as any} />
|
|
)
|
|
}
|
|
{
|
|
candidateNode.type === CUSTOM_NOTE_NODE && (
|
|
<CustomNoteNode {...candidateNode as any} />
|
|
)
|
|
}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default memo(CandidateNode)
|