File size: 2,830 Bytes
4304c6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { useCallback, useEffect, useState } from 'react'
import produce from 'immer'
import useVarList from './components/var-list/use-var-list'
import type { VariableAssignerNodeType } from './types'
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
import type { ValueSelector, Var } from '@/app/components/workflow/types'
import { BlockEnum, VarType } from '@/app/components/workflow/types'
import {
  useNodesReadOnly,
  useWorkflow,
} from '@/app/components/workflow/hooks'

const useConfig = (id: string, payload: VariableAssignerNodeType) => {
  const { nodesReadOnly: readOnly } = useNodesReadOnly()
  const { inputs, setInputs } = useNodeCrud<VariableAssignerNodeType>(id, payload)
  const { getBeforeNodeById } = useWorkflow()
  const beforeNodes = getBeforeNodeById(id)

  useEffect(() => {
    if (beforeNodes.length !== 1 || inputs.variables.length > 0)
      return
    const beforeNode = beforeNodes[0]
    if (beforeNode.data.type === BlockEnum.KnowledgeRetrieval) {
      const newInputs = produce(inputs, (draft: VariableAssignerNodeType) => {
        draft.output_type = VarType.array
        draft.variables[0] = [beforeNode.id, 'result']
      })
      setInputs(newInputs)
    }
  // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [beforeNodes, inputs.variables])

  const handleOutputTypeChange = useCallback((outputType: string) => {
    const newInputs = produce(inputs, (draft: VariableAssignerNodeType) => {
      draft.output_type = outputType as VarType
    })
    setInputs(newInputs)
  }, [inputs, setInputs])

  const { handleVarListChange, handleAddVariable } = useVarList({
    id,
    inputs,
    setInputs,
  })

  const { variables } = inputs
  const [currVarIndex, setCurrVarIndex] = useState(-1)
  const currVar = variables[currVarIndex]
  const handleOnVarOpen = useCallback((index: number) => {
    setCurrVarIndex(index)
  }, [])
  const filterVar = useCallback((varPayload: Var, valueSelector: ValueSelector) => {
    const type = varPayload.type
    if ((inputs.output_type !== VarType.array && type !== inputs.output_type) || (
      inputs.output_type === VarType.array && ![VarType.array, VarType.arrayString, VarType.arrayNumber, VarType.arrayObject].includes(type)
    ))
      return false

    // can not choose the same node
    if (!currVar)
      return true

    const selectNodeId = valueSelector[0]

    if (selectNodeId !== currVar[0] && variables.find(v => v[0] === selectNodeId))
      return false

    return true
  }, [currVar, inputs.output_type, variables])
  return {
    readOnly,
    inputs,
    handleOutputTypeChange,
    handleVarListChange,
    handleAddVariable,
    handleOnVarOpen,
    filterVar,
  }
}

export default useConfig