mirror of
https://github.com/langgenius/dify.git
synced 2026-03-27 09:09:54 +08:00
Frontend: - Migrate deprecated imports: modal→dialog, toast→ui/toast, tooltip→tooltip-plus, portal-to-follow-elem→portal-to-follow-elem-plus, select→ui/select, confirm→alert-dialog - Replace next/* with @/next/* wrapper modules - Convert TypeScript enums to const objects (erasable-syntax-only) - Replace all `any` types with `unknown` or specific types in workflow types - Fix unused vars, react-hooks-extra, react-refresh/only-export-components - Extract InteractionMode to separate module, tool-block commands to commands.ts Backend: - Fix pyrefly errors: type narrowing, null guards, getattr patterns - Remove unused TYPE_CHECKING imports in LLM node - Add ignore_imports entries to .importlinter for dify_graph boundary violations Made-with: Cursor
95 lines
3.0 KiB
TypeScript
95 lines
3.0 KiB
TypeScript
import type { Var, Variable } from '../../types'
|
|
import type { TemplateTransformNodeType } from './types'
|
|
import { produce } from 'immer'
|
|
import { useCallback, useEffect, useRef } from 'react'
|
|
import {
|
|
useNodesReadOnly,
|
|
} from '@/app/components/workflow/hooks'
|
|
import useAvailableVarList from '@/app/components/workflow/nodes/_base/hooks/use-available-var-list'
|
|
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
|
|
import { useStore } from '../../store'
|
|
import { VarType } from '../../types'
|
|
import useVarList from '../_base/hooks/use-var-list'
|
|
|
|
const useConfig = (id: string, payload: TemplateTransformNodeType) => {
|
|
const { nodesReadOnly: readOnly } = useNodesReadOnly()
|
|
const defaultConfig = useStore(s => s.nodesDefaultConfigs)?.[payload.type]
|
|
|
|
const { inputs, setInputs: doSetInputs } = useNodeCrud<TemplateTransformNodeType>(id, payload)
|
|
const inputsRef = useRef(inputs)
|
|
const setInputs = useCallback((newPayload: TemplateTransformNodeType) => {
|
|
doSetInputs(newPayload)
|
|
inputsRef.current = newPayload
|
|
}, [doSetInputs])
|
|
|
|
const { availableVars } = useAvailableVarList(id, {
|
|
onlyLeafNodeVar: false,
|
|
filterVar: () => true,
|
|
})
|
|
|
|
const { handleAddVariable: handleAddEmptyVariable } = useVarList<TemplateTransformNodeType>({
|
|
inputs,
|
|
setInputs,
|
|
})
|
|
|
|
const handleVarListChange = useCallback((newList: Variable[]) => {
|
|
const newInputs = produce(inputsRef.current, (draft: any) => {
|
|
draft.variables = newList
|
|
})
|
|
setInputs(newInputs)
|
|
}, [setInputs])
|
|
|
|
const handleAddVariable = useCallback((payload: Variable) => {
|
|
const newInputs = produce(inputsRef.current, (draft: any) => {
|
|
draft.variables.push(payload)
|
|
})
|
|
setInputs(newInputs)
|
|
}, [setInputs])
|
|
|
|
// rename var in code
|
|
const handleVarNameChange = useCallback((oldName: string, newName: string) => {
|
|
const newInputs = produce(inputsRef.current, (draft: any) => {
|
|
draft.template = draft.template.replaceAll(`{{ ${oldName} }}`, `{{ ${newName} }}`)
|
|
})
|
|
setInputs(newInputs)
|
|
}, [setInputs])
|
|
|
|
useEffect(() => {
|
|
if (inputs.template)
|
|
return
|
|
|
|
const isReady = defaultConfig && Object.keys(defaultConfig).length > 0
|
|
if (isReady) {
|
|
setInputs({
|
|
...inputs,
|
|
...defaultConfig,
|
|
})
|
|
}
|
|
}, [defaultConfig])
|
|
|
|
const handleCodeChange = useCallback((template: string) => {
|
|
const newInputs = produce(inputsRef.current, (draft: any) => {
|
|
draft.template = template
|
|
})
|
|
setInputs(newInputs)
|
|
}, [setInputs])
|
|
|
|
const filterVar = useCallback((varPayload: Var) => {
|
|
return ([VarType.string, VarType.number, VarType.boolean, VarType.object, VarType.array, VarType.arrayNumber, VarType.arrayString, VarType.arrayBoolean, VarType.arrayObject] as VarType[]).includes(varPayload.type)
|
|
}, [])
|
|
|
|
return {
|
|
readOnly,
|
|
inputs,
|
|
availableVars,
|
|
handleVarListChange,
|
|
handleVarNameChange,
|
|
handleAddVariable,
|
|
handleAddEmptyVariable,
|
|
handleCodeChange,
|
|
filterVar,
|
|
}
|
|
}
|
|
|
|
export default useConfig
|