use-workflow.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. import {
  2. useCallback,
  3. useEffect,
  4. useMemo,
  5. useState,
  6. } from 'react'
  7. import dayjs from 'dayjs'
  8. import { uniqBy } from 'lodash-es'
  9. import { useTranslation } from 'react-i18next'
  10. import { useContext } from 'use-context-selector'
  11. import {
  12. getIncomers,
  13. getOutgoers,
  14. useStoreApi,
  15. } from 'reactflow'
  16. import type {
  17. Connection,
  18. } from 'reactflow'
  19. import type {
  20. Edge,
  21. Node,
  22. ValueSelector,
  23. } from '../types'
  24. import {
  25. BlockEnum,
  26. WorkflowRunningStatus,
  27. } from '../types'
  28. import {
  29. useStore,
  30. useWorkflowStore,
  31. } from '../store'
  32. import {
  33. getParallelInfo,
  34. } from '../utils'
  35. import {
  36. PARALLEL_DEPTH_LIMIT,
  37. PARALLEL_LIMIT,
  38. SUPPORT_OUTPUT_VARS_NODE,
  39. } from '../constants'
  40. import { CUSTOM_NOTE_NODE } from '../note-node/constants'
  41. import { findUsedVarNodes, getNodeOutputVars, updateNodeVars } from '../nodes/_base/components/variable/utils'
  42. import { useNodesExtraData } from './use-nodes-data'
  43. import { useWorkflowTemplate } from './use-workflow-template'
  44. import { useStore as useAppStore } from '@/app/components/app/store'
  45. import {
  46. fetchNodesDefaultConfigs,
  47. fetchPublishedWorkflow,
  48. fetchWorkflowDraft,
  49. syncWorkflowDraft,
  50. } from '@/service/workflow'
  51. import type { FetchWorkflowDraftResponse } from '@/types/workflow'
  52. import {
  53. fetchAllBuiltInTools,
  54. fetchAllCustomTools,
  55. fetchAllWorkflowTools,
  56. } from '@/service/tools'
  57. import I18n from '@/context/i18n'
  58. import { CollectionType } from '@/app/components/tools/types'
  59. import { CUSTOM_ITERATION_START_NODE } from '@/app/components/workflow/nodes/iteration-start/constants'
  60. import { CUSTOM_LOOP_START_NODE } from '@/app/components/workflow/nodes/loop-start/constants'
  61. import { useWorkflowConfig } from '@/service/use-workflow'
  62. import { canFindTool } from '@/utils'
  63. import { useAppDetailContext } from '@/app/(commonLayout)/app/(appDetailLayout)/[appId]/layout-main'
  64. export const useIsChatMode = () => {
  65. const appDetail = useAppStore(s => s.appDetail)
  66. return appDetail?.mode === 'advanced-chat'
  67. }
  68. export const useWorkflow = () => {
  69. const { t } = useTranslation()
  70. const { locale } = useContext(I18n)
  71. const store = useStoreApi()
  72. const workflowStore = useWorkflowStore()
  73. const appId = useStore(s => s.appId)
  74. const nodesExtraData = useNodesExtraData()
  75. const { data: workflowConfig } = useWorkflowConfig(appId)
  76. const setPanelWidth = useCallback((width: number) => {
  77. localStorage.setItem('workflow-node-panel-width', `${width}`)
  78. workflowStore.setState({ panelWidth: width })
  79. }, [workflowStore])
  80. const getTreeLeafNodes = useCallback((nodeId: string) => {
  81. const {
  82. getNodes,
  83. edges,
  84. } = store.getState()
  85. const nodes = getNodes()
  86. let startNode = nodes.find(node => node.data.type === BlockEnum.Start)
  87. const currentNode = nodes.find(node => node.id === nodeId)
  88. if (currentNode?.parentId)
  89. startNode = nodes.find(node => node.parentId === currentNode.parentId && (node.type === CUSTOM_ITERATION_START_NODE || node.type === CUSTOM_LOOP_START_NODE))
  90. if (!startNode)
  91. return []
  92. const list: Node[] = []
  93. const preOrder = (root: Node, callback: (node: Node) => void) => {
  94. if (root.id === nodeId)
  95. return
  96. const outgoers = getOutgoers(root, nodes, edges)
  97. if (outgoers.length) {
  98. outgoers.forEach((outgoer) => {
  99. preOrder(outgoer, callback)
  100. })
  101. }
  102. else {
  103. if (root.id !== nodeId)
  104. callback(root)
  105. }
  106. }
  107. preOrder(startNode, (node) => {
  108. list.push(node)
  109. })
  110. const incomers = getIncomers({ id: nodeId } as Node, nodes, edges)
  111. list.push(...incomers)
  112. return uniqBy(list, 'id').filter((item) => {
  113. return SUPPORT_OUTPUT_VARS_NODE.includes(item.data.type)
  114. })
  115. }, [store])
  116. const getBeforeNodesInSameBranch = useCallback((nodeId: string, newNodes?: Node[], newEdges?: Edge[]) => {
  117. const {
  118. getNodes,
  119. edges,
  120. } = store.getState()
  121. const nodes = newNodes || getNodes()
  122. const currentNode = nodes.find(node => node.id === nodeId)
  123. const list: Node[] = []
  124. if (!currentNode)
  125. return list
  126. if (currentNode.parentId) {
  127. const parentNode = nodes.find(node => node.id === currentNode.parentId)
  128. if (parentNode) {
  129. const parentList = getBeforeNodesInSameBranch(parentNode.id)
  130. list.push(...parentList)
  131. }
  132. }
  133. const traverse = (root: Node, callback: (node: Node) => void) => {
  134. if (root) {
  135. const incomers = getIncomers(root, nodes, newEdges || edges)
  136. if (incomers.length) {
  137. incomers.forEach((node) => {
  138. if (!list.find(n => node.id === n.id)) {
  139. callback(node)
  140. traverse(node, callback)
  141. }
  142. })
  143. }
  144. }
  145. }
  146. traverse(currentNode, (node) => {
  147. list.push(node)
  148. })
  149. const length = list.length
  150. if (length) {
  151. return uniqBy(list, 'id').reverse().filter((item) => {
  152. return SUPPORT_OUTPUT_VARS_NODE.includes(item.data.type)
  153. })
  154. }
  155. return []
  156. }, [store])
  157. const getBeforeNodesInSameBranchIncludeParent = useCallback((nodeId: string, newNodes?: Node[], newEdges?: Edge[]) => {
  158. const nodes = getBeforeNodesInSameBranch(nodeId, newNodes, newEdges)
  159. const {
  160. getNodes,
  161. } = store.getState()
  162. const allNodes = getNodes()
  163. const node = allNodes.find(n => n.id === nodeId)
  164. const parentNodeId = node?.parentId
  165. const parentNode = allNodes.find(n => n.id === parentNodeId)
  166. if (parentNode)
  167. nodes.push(parentNode)
  168. return nodes
  169. }, [getBeforeNodesInSameBranch, store])
  170. const getAfterNodesInSameBranch = useCallback((nodeId: string) => {
  171. const {
  172. getNodes,
  173. edges,
  174. } = store.getState()
  175. const nodes = getNodes()
  176. const currentNode = nodes.find(node => node.id === nodeId)!
  177. if (!currentNode)
  178. return []
  179. const list: Node[] = [currentNode]
  180. const traverse = (root: Node, callback: (node: Node) => void) => {
  181. if (root) {
  182. const outgoers = getOutgoers(root, nodes, edges)
  183. if (outgoers.length) {
  184. outgoers.forEach((node) => {
  185. callback(node)
  186. traverse(node, callback)
  187. })
  188. }
  189. }
  190. }
  191. traverse(currentNode, (node) => {
  192. list.push(node)
  193. })
  194. return uniqBy(list, 'id')
  195. }, [store])
  196. const getBeforeNodeById = useCallback((nodeId: string) => {
  197. const {
  198. getNodes,
  199. edges,
  200. } = store.getState()
  201. const nodes = getNodes()
  202. const node = nodes.find(node => node.id === nodeId)!
  203. return getIncomers(node, nodes, edges)
  204. }, [store])
  205. const getIterationNodeChildren = useCallback((nodeId: string) => {
  206. const {
  207. getNodes,
  208. } = store.getState()
  209. const nodes = getNodes()
  210. return nodes.filter(node => node.parentId === nodeId)
  211. }, [store])
  212. const getLoopNodeChildren = useCallback((nodeId: string) => {
  213. const {
  214. getNodes,
  215. } = store.getState()
  216. const nodes = getNodes()
  217. return nodes.filter(node => node.parentId === nodeId)
  218. }, [store])
  219. const isFromStartNode = useCallback((nodeId: string) => {
  220. const { getNodes } = store.getState()
  221. const nodes = getNodes()
  222. const currentNode = nodes.find(node => node.id === nodeId)
  223. if (!currentNode)
  224. return false
  225. if (currentNode.data.type === BlockEnum.Start)
  226. return true
  227. const checkPreviousNodes = (node: Node) => {
  228. const previousNodes = getBeforeNodeById(node.id)
  229. for (const prevNode of previousNodes) {
  230. if (prevNode.data.type === BlockEnum.Start)
  231. return true
  232. if (checkPreviousNodes(prevNode))
  233. return true
  234. }
  235. return false
  236. }
  237. return checkPreviousNodes(currentNode)
  238. }, [store, getBeforeNodeById])
  239. const handleOutVarRenameChange = useCallback((nodeId: string, oldValeSelector: ValueSelector, newVarSelector: ValueSelector) => {
  240. const { getNodes, setNodes } = store.getState()
  241. const afterNodes = getAfterNodesInSameBranch(nodeId)
  242. const effectNodes = findUsedVarNodes(oldValeSelector, afterNodes)
  243. if (effectNodes.length > 0) {
  244. const newNodes = getNodes().map((node) => {
  245. if (effectNodes.find(n => n.id === node.id))
  246. return updateNodeVars(node, oldValeSelector, newVarSelector)
  247. return node
  248. })
  249. setNodes(newNodes)
  250. }
  251. // eslint-disable-next-line react-hooks/exhaustive-deps
  252. }, [store])
  253. const isVarUsedInNodes = useCallback((varSelector: ValueSelector) => {
  254. const nodeId = varSelector[0]
  255. const afterNodes = getAfterNodesInSameBranch(nodeId)
  256. const effectNodes = findUsedVarNodes(varSelector, afterNodes)
  257. return effectNodes.length > 0
  258. }, [getAfterNodesInSameBranch])
  259. const removeUsedVarInNodes = useCallback((varSelector: ValueSelector) => {
  260. const nodeId = varSelector[0]
  261. const { getNodes, setNodes } = store.getState()
  262. const afterNodes = getAfterNodesInSameBranch(nodeId)
  263. const effectNodes = findUsedVarNodes(varSelector, afterNodes)
  264. if (effectNodes.length > 0) {
  265. const newNodes = getNodes().map((node) => {
  266. if (effectNodes.find(n => n.id === node.id))
  267. return updateNodeVars(node, varSelector, [])
  268. return node
  269. })
  270. setNodes(newNodes)
  271. }
  272. }, [getAfterNodesInSameBranch, store])
  273. const isNodeVarsUsedInNodes = useCallback((node: Node, isChatMode: boolean) => {
  274. const outputVars = getNodeOutputVars(node, isChatMode)
  275. const isUsed = outputVars.some((varSelector) => {
  276. return isVarUsedInNodes(varSelector)
  277. })
  278. return isUsed
  279. }, [isVarUsedInNodes])
  280. const checkParallelLimit = useCallback((nodeId: string, nodeHandle = 'source') => {
  281. const {
  282. edges,
  283. } = store.getState()
  284. const connectedEdges = edges.filter(edge => edge.source === nodeId && edge.sourceHandle === nodeHandle)
  285. if (connectedEdges.length > PARALLEL_LIMIT - 1) {
  286. const { setShowTips } = workflowStore.getState()
  287. setShowTips(t('workflow.common.parallelTip.limit', { num: PARALLEL_LIMIT }))
  288. return false
  289. }
  290. return true
  291. }, [store, workflowStore, t])
  292. const checkNestedParallelLimit = useCallback((nodes: Node[], edges: Edge[], parentNodeId?: string) => {
  293. const {
  294. parallelList,
  295. hasAbnormalEdges,
  296. } = getParallelInfo(nodes, edges, parentNodeId)
  297. if (hasAbnormalEdges)
  298. return false
  299. for (let i = 0; i < parallelList.length; i++) {
  300. const parallel = parallelList[i]
  301. if (parallel.depth > (workflowConfig?.parallel_depth_limit || PARALLEL_DEPTH_LIMIT)) {
  302. const { setShowTips } = workflowStore.getState()
  303. setShowTips(t('workflow.common.parallelTip.depthLimit', { num: (workflowConfig?.parallel_depth_limit || PARALLEL_DEPTH_LIMIT) }))
  304. return false
  305. }
  306. }
  307. return true
  308. }, [t, workflowStore, workflowConfig?.parallel_depth_limit])
  309. const isValidConnection = useCallback(({ source, sourceHandle, target }: Connection) => {
  310. const {
  311. edges,
  312. getNodes,
  313. } = store.getState()
  314. const nodes = getNodes()
  315. const sourceNode: Node = nodes.find(node => node.id === source)!
  316. const targetNode: Node = nodes.find(node => node.id === target)!
  317. if (!checkParallelLimit(source!, sourceHandle || 'source'))
  318. return false
  319. if (sourceNode.type === CUSTOM_NOTE_NODE || targetNode.type === CUSTOM_NOTE_NODE)
  320. return false
  321. if (sourceNode.parentId !== targetNode.parentId)
  322. return false
  323. if (sourceNode && targetNode) {
  324. const sourceNodeAvailableNextNodes = nodesExtraData[sourceNode.data.type].availableNextNodes
  325. const targetNodeAvailablePrevNodes = [...nodesExtraData[targetNode.data.type].availablePrevNodes, BlockEnum.Start]
  326. if (!sourceNodeAvailableNextNodes.includes(targetNode.data.type))
  327. return false
  328. if (!targetNodeAvailablePrevNodes.includes(sourceNode.data.type))
  329. return false
  330. }
  331. const hasCycle = (node: Node, visited = new Set()) => {
  332. if (visited.has(node.id))
  333. return false
  334. visited.add(node.id)
  335. for (const outgoer of getOutgoers(node, nodes, edges)) {
  336. if (outgoer.id === source)
  337. return true
  338. if (hasCycle(outgoer, visited))
  339. return true
  340. }
  341. }
  342. return !hasCycle(targetNode)
  343. }, [store, nodesExtraData, checkParallelLimit])
  344. const formatTimeFromNow = useCallback((time: number) => {
  345. return dayjs(time).locale(locale === 'zh-Hans' ? 'zh-cn' : locale).fromNow()
  346. }, [locale])
  347. const getNode = useCallback((nodeId?: string) => {
  348. const { getNodes } = store.getState()
  349. const nodes = getNodes()
  350. return nodes.find(node => node.id === nodeId) || nodes.find(node => node.data.type === BlockEnum.Start)
  351. }, [store])
  352. return {
  353. setPanelWidth,
  354. getTreeLeafNodes,
  355. getBeforeNodesInSameBranch,
  356. getBeforeNodesInSameBranchIncludeParent,
  357. getAfterNodesInSameBranch,
  358. handleOutVarRenameChange,
  359. isVarUsedInNodes,
  360. removeUsedVarInNodes,
  361. isNodeVarsUsedInNodes,
  362. checkParallelLimit,
  363. checkNestedParallelLimit,
  364. isValidConnection,
  365. isFromStartNode,
  366. formatTimeFromNow,
  367. getNode,
  368. getBeforeNodeById,
  369. getIterationNodeChildren,
  370. getLoopNodeChildren,
  371. }
  372. }
  373. export const useFetchToolsData = () => {
  374. const workflowStore = useWorkflowStore()
  375. const handleFetchAllTools = useCallback(async (type: string) => {
  376. if (type === 'builtin') {
  377. const buildInTools = await fetchAllBuiltInTools()
  378. workflowStore.setState({
  379. buildInTools: buildInTools || [],
  380. })
  381. }
  382. if (type === 'custom') {
  383. const customTools = await fetchAllCustomTools()
  384. workflowStore.setState({
  385. customTools: customTools || [],
  386. })
  387. }
  388. if (type === 'workflow') {
  389. const workflowTools = await fetchAllWorkflowTools()
  390. workflowStore.setState({
  391. workflowTools: workflowTools || [],
  392. })
  393. }
  394. }, [workflowStore])
  395. return {
  396. handleFetchAllTools,
  397. }
  398. }
  399. export const useWorkflowInit = () => {
  400. const { detail, isCreate, isEdit, isOperation } = useAppDetailContext()
  401. const workflowStore = useWorkflowStore()
  402. const {
  403. nodes: nodesTemplate,
  404. edges: edgesTemplate,
  405. } = useWorkflowTemplate()
  406. const { handleFetchAllTools } = useFetchToolsData()
  407. const appDetail = useAppStore(state => state.appDetail)!
  408. const setSyncWorkflowDraftHash = useStore(s => s.setSyncWorkflowDraftHash)
  409. const [data, setData] = useState<FetchWorkflowDraftResponse>()
  410. const [isLoading, setIsLoading] = useState(true)
  411. useEffect(() => {
  412. workflowStore.setState({ appId: appDetail.id })
  413. }, [appDetail.id, workflowStore])
  414. const handleGetInitialWorkflowData = useCallback(async () => {
  415. try {
  416. // 流程初始化
  417. const res = await fetchWorkflowDraft(`/apps/${appDetail.id}/workflows/draft`)
  418. setData(res)
  419. workflowStore.setState({
  420. envSecrets: (res.environment_variables || []).filter(env => env.value_type === 'secret').reduce((acc, env) => {
  421. acc[env.id] = env.value
  422. return acc
  423. }, {} as Record<string, string>),
  424. environmentVariables: res.environment_variables?.map(env => env.value_type === 'secret' ? { ...env, value: '[__HIDDEN__]' } : env) || [],
  425. conversationVariables: res.conversation_variables || [],
  426. })
  427. setSyncWorkflowDraftHash(res.hash)
  428. setIsLoading(false)
  429. }
  430. catch (error: any) {
  431. if (error && error.json && !error.bodyUsed && appDetail) {
  432. error.json().then((err: any) => {
  433. if (err.code === 'draft_workflow_not_exist') {
  434. workflowStore.setState({ notInitialWorkflow: true })
  435. syncWorkflowDraft({
  436. url: `/apps/${appDetail.id}/workflows/draft`,
  437. params: {
  438. graph: {
  439. nodes: nodesTemplate,
  440. edges: edgesTemplate,
  441. },
  442. features: {
  443. retriever_resource: { enabled: true },
  444. },
  445. environment_variables: [],
  446. conversation_variables: [],
  447. },
  448. }).then((res) => {
  449. workflowStore.getState().setDraftUpdatedAt(res.updated_at)
  450. handleGetInitialWorkflowData()
  451. })
  452. }
  453. })
  454. }
  455. }
  456. }, [appDetail, nodesTemplate, edgesTemplate, workflowStore, setSyncWorkflowDraftHash])
  457. useEffect(() => {
  458. handleGetInitialWorkflowData()
  459. // eslint-disable-next-line react-hooks/exhaustive-deps
  460. }, [])
  461. const handleFetchPreloadData = useCallback(async () => {
  462. try {
  463. const nodesDefaultConfigsData = await fetchNodesDefaultConfigs(`/apps/${appDetail?.id}/workflows/default-workflow-block-configs`)
  464. const publishedWorkflow = await fetchPublishedWorkflow(`/apps/${appDetail?.id}/workflows/publish`)
  465. workflowStore.setState({
  466. nodesDefaultConfigs: nodesDefaultConfigsData.reduce((acc, block) => {
  467. if (!acc[block.type])
  468. acc[block.type] = { ...block.config }
  469. return acc
  470. }, {} as Record<string, any>),
  471. })
  472. workflowStore.getState().setPublishedAt(publishedWorkflow?.created_at)
  473. }
  474. catch (e) {
  475. console.error(e)
  476. }
  477. }, [workflowStore, appDetail])
  478. useEffect(() => {
  479. handleFetchPreloadData()
  480. handleFetchAllTools('builtin')
  481. handleFetchAllTools('custom')
  482. handleFetchAllTools('workflow')
  483. }, [handleFetchPreloadData, handleFetchAllTools])
  484. useEffect(() => {
  485. if (data) {
  486. workflowStore.getState().setDraftUpdatedAt(data.updated_at)
  487. workflowStore.getState().setToolPublished(data.tool_published)
  488. }
  489. }, [data, workflowStore])
  490. return {
  491. data,
  492. isLoading,
  493. }
  494. }
  495. export const useWorkflowReadOnly = () => {
  496. const workflowStore = useWorkflowStore()
  497. const workflowRunningData = useStore(s => s.workflowRunningData)
  498. const getWorkflowReadOnly = useCallback(() => {
  499. return workflowStore.getState().workflowRunningData?.result.status === WorkflowRunningStatus.Running
  500. }, [workflowStore])
  501. return {
  502. workflowReadOnly: workflowRunningData?.result.status === WorkflowRunningStatus.Running,
  503. getWorkflowReadOnly,
  504. }
  505. }
  506. export const useNodesReadOnly = () => {
  507. const workflowStore = useWorkflowStore()
  508. const workflowRunningData = useStore(s => s.workflowRunningData)
  509. const historyWorkflowData = useStore(s => s.historyWorkflowData)
  510. const isRestoring = useStore(s => s.isRestoring)
  511. const getNodesReadOnly = useCallback(() => {
  512. const {
  513. workflowRunningData,
  514. historyWorkflowData,
  515. isRestoring,
  516. } = workflowStore.getState()
  517. return workflowRunningData?.result.status === WorkflowRunningStatus.Running || historyWorkflowData || isRestoring
  518. }, [workflowStore])
  519. return {
  520. nodesReadOnly: !!(workflowRunningData?.result.status === WorkflowRunningStatus.Running || historyWorkflowData || isRestoring),
  521. getNodesReadOnly,
  522. }
  523. }
  524. export const useToolIcon = (data: Node['data']) => {
  525. const buildInTools = useStore(s => s.buildInTools)
  526. const customTools = useStore(s => s.customTools)
  527. const workflowTools = useStore(s => s.workflowTools)
  528. const toolIcon = useMemo(() => {
  529. if (data.type === BlockEnum.Tool) {
  530. let targetTools = buildInTools
  531. if (data.provider_type === CollectionType.builtIn)
  532. targetTools = buildInTools
  533. else if (data.provider_type === CollectionType.custom)
  534. targetTools = customTools
  535. else
  536. targetTools = workflowTools
  537. return targetTools.find(toolWithProvider => canFindTool(toolWithProvider.id, data.provider_id))?.icon
  538. }
  539. }, [data, buildInTools, customTools, workflowTools])
  540. return toolIcon
  541. }
  542. export const useIsNodeInIteration = (iterationId: string) => {
  543. const store = useStoreApi()
  544. const isNodeInIteration = useCallback((nodeId: string) => {
  545. const {
  546. getNodes,
  547. } = store.getState()
  548. const nodes = getNodes()
  549. const node = nodes.find(node => node.id === nodeId)
  550. if (!node)
  551. return false
  552. if (node.parentId === iterationId)
  553. return true
  554. return false
  555. }, [iterationId, store])
  556. return {
  557. isNodeInIteration,
  558. }
  559. }
  560. export const useIsNodeInLoop = (loopId: string) => {
  561. const store = useStoreApi()
  562. const isNodeInLoop = useCallback((nodeId: string) => {
  563. const {
  564. getNodes,
  565. } = store.getState()
  566. const nodes = getNodes()
  567. const node = nodes.find(node => node.id === nodeId)
  568. if (!node)
  569. return false
  570. if (node.parentId === loopId)
  571. return true
  572. return false
  573. }, [loopId, store])
  574. return {
  575. isNodeInLoop,
  576. }
  577. }