use-config.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { useCallback } from 'react'
  2. import produce from 'immer'
  3. import useVarList from '../_base/hooks/use-var-list'
  4. import type { Var } from '../../types'
  5. import { VarType } from '../../types'
  6. import type { AnswerNodeType } from './types'
  7. import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
  8. import {
  9. useNodesReadOnly,
  10. } from '@/app/components/workflow/hooks'
  11. const useConfig = (id: string, payload: AnswerNodeType) => {
  12. const { nodesReadOnly: readOnly } = useNodesReadOnly()
  13. const { inputs, setInputs } = useNodeCrud<AnswerNodeType>(id, payload)
  14. // variables
  15. const { handleVarListChange, handleAddVariable } = useVarList<AnswerNodeType>({
  16. inputs,
  17. setInputs,
  18. })
  19. const handleAnswerChange = useCallback((value: string) => {
  20. const newInputs = produce(inputs, (draft) => {
  21. draft.answer = value
  22. })
  23. setInputs(newInputs)
  24. }, [inputs, setInputs])
  25. const filterVar = useCallback((varPayload: Var) => {
  26. return varPayload.type !== VarType.arrayObject
  27. }, [])
  28. return {
  29. readOnly,
  30. inputs,
  31. handleVarListChange,
  32. handleAddVariable,
  33. handleAnswerChange,
  34. filterVar,
  35. }
  36. }
  37. export default useConfig