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

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. }
  10. const useIsVarFileAttribute = ({
  11. nodeId,
  12. isInIteration,
  13. }: Params) => {
  14. const isChatMode = useIsChatMode()
  15. const store = useStoreApi()
  16. const { getBeforeNodesInSameBranch } = useWorkflow()
  17. const {
  18. getNodes,
  19. } = store.getState()
  20. const currentNode = getNodes().find(n => n.id === nodeId)
  21. const iterationNode = isInIteration ? getNodes().find(n => n.id === currentNode!.parentId) : null
  22. const availableNodes = useMemo(() => {
  23. return getBeforeNodesInSameBranch(nodeId)
  24. }, [getBeforeNodesInSameBranch, nodeId])
  25. const { getCurrentVariableType } = useWorkflowVariables()
  26. const getIsVarFileAttribute = (variable: ValueSelector) => {
  27. if (variable.length !== 3)
  28. return false
  29. const parentVariable = variable.slice(0, 2)
  30. const varType = getCurrentVariableType({
  31. parentNode: iterationNode,
  32. valueSelector: parentVariable,
  33. availableNodes,
  34. isChatMode,
  35. isConstant: false,
  36. })
  37. return varType === VarType.file
  38. }
  39. return {
  40. getIsVarFileAttribute,
  41. }
  42. }
  43. export default useIsVarFileAttribute