use-is-var-file-attribute.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { useStoreApi } from 'reactflow'
  2. import { useMemo } from 'react'
  3. import { useIsChatMode, useWorkflow, useWorkflowVariables } from '../../hooks'
  4. import type { ValueSelector } from '../../types'
  5. import { VarType } from '../../types'
  6. type Params = {
  7. nodeId: string
  8. isInIteration: boolean
  9. isInLoop: boolean
  10. }
  11. const useIsVarFileAttribute = ({
  12. nodeId,
  13. isInIteration,
  14. isInLoop,
  15. }: Params) => {
  16. const isChatMode = useIsChatMode()
  17. const store = useStoreApi()
  18. const { getBeforeNodesInSameBranch } = useWorkflow()
  19. const {
  20. getNodes,
  21. } = store.getState()
  22. const currentNode = getNodes().find(n => n.id === nodeId)
  23. const iterationNode = isInIteration ? getNodes().find(n => n.id === currentNode!.parentId) : null
  24. const loopNode = isInLoop ? getNodes().find(n => n.id === currentNode!.parentId) : null
  25. const availableNodes = useMemo(() => {
  26. return getBeforeNodesInSameBranch(nodeId)
  27. }, [getBeforeNodesInSameBranch, nodeId])
  28. const { getCurrentVariableType } = useWorkflowVariables()
  29. const getIsVarFileAttribute = (variable: ValueSelector) => {
  30. if (variable.length !== 3)
  31. return false
  32. const parentVariable = variable.slice(0, 2)
  33. const varType = getCurrentVariableType({
  34. parentNode: isInIteration ? iterationNode : loopNode,
  35. valueSelector: parentVariable,
  36. availableNodes,
  37. isChatMode,
  38. isConstant: false,
  39. })
  40. return varType === VarType.file
  41. }
  42. return {
  43. getIsVarFileAttribute,
  44. }
  45. }
  46. export default useIsVarFileAttribute