use-config.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { useCallback, useMemo } from 'react'
  2. import produce from 'immer'
  3. import { useStoreApi } from 'reactflow'
  4. import type { ValueSelector, Var } from '../../types'
  5. import { VarType } from '../../types'
  6. import { type DocExtractorNodeType } from './types'
  7. import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
  8. import {
  9. useIsChatMode,
  10. useNodesReadOnly,
  11. useWorkflow,
  12. useWorkflowVariables,
  13. } from '@/app/components/workflow/hooks'
  14. const useConfig = (id: string, payload: DocExtractorNodeType) => {
  15. const { nodesReadOnly: readOnly } = useNodesReadOnly()
  16. const { inputs, setInputs } = useNodeCrud<DocExtractorNodeType>(id, payload)
  17. const filterVar = useCallback((varPayload: Var) => {
  18. return varPayload.type === VarType.file || varPayload.type === VarType.arrayFile
  19. }, [])
  20. const isChatMode = useIsChatMode()
  21. const store = useStoreApi()
  22. const { getBeforeNodesInSameBranch } = useWorkflow()
  23. const {
  24. getNodes,
  25. } = store.getState()
  26. const currentNode = getNodes().find(n => n.id === id)
  27. const isInIteration = payload.isInIteration
  28. const iterationNode = isInIteration ? getNodes().find(n => n.id === currentNode!.parentId) : null
  29. const availableNodes = useMemo(() => {
  30. return getBeforeNodesInSameBranch(id)
  31. }, [getBeforeNodesInSameBranch, id])
  32. const { getCurrentVariableType } = useWorkflowVariables()
  33. const getType = useCallback((variable?: ValueSelector) => {
  34. const varType = getCurrentVariableType({
  35. parentNode: iterationNode,
  36. valueSelector: variable || [],
  37. availableNodes,
  38. isChatMode,
  39. isConstant: false,
  40. })
  41. return varType
  42. }, [getCurrentVariableType, availableNodes, isChatMode, iterationNode])
  43. const handleVarChanges = useCallback((variable: ValueSelector | string) => {
  44. const newInputs = produce(inputs, (draft) => {
  45. draft.variable_selector = variable as ValueSelector
  46. draft.is_array_file = getType(draft.variable_selector) === VarType.arrayFile
  47. })
  48. setInputs(newInputs)
  49. }, [getType, inputs, setInputs])
  50. return {
  51. readOnly,
  52. inputs,
  53. filterVar,
  54. handleVarChanges,
  55. }
  56. }
  57. export default useConfig