You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

use-config.ts 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { useCallback, useEffect, useRef } from 'react'
  2. import produce from 'immer'
  3. import useVarList from '../_base/hooks/use-var-list'
  4. import type { Var, Variable } from '../../types'
  5. import { VarType } from '../../types'
  6. import { useStore } from '../../store'
  7. import type { TemplateTransformNodeType } from './types'
  8. import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
  9. import {
  10. useNodesReadOnly,
  11. } from '@/app/components/workflow/hooks'
  12. import useAvailableVarList from '@/app/components/workflow/nodes/_base/hooks/use-available-var-list'
  13. const useConfig = (id: string, payload: TemplateTransformNodeType) => {
  14. const { nodesReadOnly: readOnly } = useNodesReadOnly()
  15. const defaultConfig = useStore(s => s.nodesDefaultConfigs)[payload.type]
  16. const { inputs, setInputs: doSetInputs } = useNodeCrud<TemplateTransformNodeType>(id, payload)
  17. const inputsRef = useRef(inputs)
  18. const setInputs = useCallback((newPayload: TemplateTransformNodeType) => {
  19. doSetInputs(newPayload)
  20. inputsRef.current = newPayload
  21. }, [doSetInputs])
  22. const { availableVars } = useAvailableVarList(id, {
  23. onlyLeafNodeVar: false,
  24. filterVar: () => true,
  25. })
  26. const { handleAddVariable: handleAddEmptyVariable } = useVarList<TemplateTransformNodeType>({
  27. inputs,
  28. setInputs,
  29. })
  30. const handleVarListChange = useCallback((newList: Variable[]) => {
  31. const newInputs = produce(inputsRef.current, (draft: any) => {
  32. draft.variables = newList
  33. })
  34. setInputs(newInputs)
  35. }, [setInputs])
  36. const handleAddVariable = useCallback((payload: Variable) => {
  37. const newInputs = produce(inputsRef.current, (draft: any) => {
  38. draft.variables.push(payload)
  39. })
  40. setInputs(newInputs)
  41. }, [setInputs])
  42. // rename var in code
  43. const handleVarNameChange = useCallback((oldName: string, newName: string) => {
  44. const newInputs = produce(inputsRef.current, (draft: any) => {
  45. draft.template = draft.template.replaceAll(`{{ ${oldName} }}`, `{{ ${newName} }}`)
  46. })
  47. setInputs(newInputs)
  48. }, [setInputs])
  49. useEffect(() => {
  50. if (inputs.template)
  51. return
  52. const isReady = defaultConfig && Object.keys(defaultConfig).length > 0
  53. if (isReady) {
  54. setInputs({
  55. ...inputs,
  56. ...defaultConfig,
  57. })
  58. }
  59. // eslint-disable-next-line react-hooks/exhaustive-deps
  60. }, [defaultConfig])
  61. const handleCodeChange = useCallback((template: string) => {
  62. const newInputs = produce(inputsRef.current, (draft: any) => {
  63. draft.template = template
  64. })
  65. setInputs(newInputs)
  66. }, [setInputs])
  67. const filterVar = useCallback((varPayload: Var) => {
  68. return [VarType.string, VarType.number, VarType.object, VarType.array, VarType.arrayNumber, VarType.arrayString, VarType.arrayObject].includes(varPayload.type)
  69. }, [])
  70. return {
  71. readOnly,
  72. inputs,
  73. availableVars,
  74. handleVarListChange,
  75. handleVarNameChange,
  76. handleAddVariable,
  77. handleAddEmptyVariable,
  78. handleCodeChange,
  79. filterVar,
  80. }
  81. }
  82. export default useConfig