File size: 5,072 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
import { useCallback, useEffect } from 'react'
import produce from 'immer'
import { useBoolean } from 'ahooks'
import useVarList from '../_base/hooks/use-var-list'
import { VarType } from '../../types'
import type { Var } from '../../types'
import { useStore } from '../../store'
import type { Authorization, Body, HttpNodeType, Method, Timeout } from './types'
import useKeyValueList from './hooks/use-key-value-list'
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run'
import {
useNodesReadOnly,
} from '@/app/components/workflow/hooks'
const useConfig = (id: string, payload: HttpNodeType) => {
const { nodesReadOnly: readOnly } = useNodesReadOnly()
const defaultConfig = useStore(s => s.nodesDefaultConfigs)[payload.type]
const { inputs, setInputs } = useNodeCrud<HttpNodeType>(id, payload)
const { handleVarListChange, handleAddVariable } = useVarList<HttpNodeType>({
inputs,
setInputs,
})
useEffect(() => {
const isReady = defaultConfig && Object.keys(defaultConfig).length > 0
if (isReady) {
setInputs({
...defaultConfig,
...inputs,
})
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [defaultConfig])
const handleMethodChange = useCallback((method: Method) => {
const newInputs = produce(inputs, (draft: HttpNodeType) => {
draft.method = method
})
setInputs(newInputs)
}, [inputs, setInputs])
const handleUrlChange = useCallback((url: string) => {
const newInputs = produce(inputs, (draft: HttpNodeType) => {
draft.url = url
})
setInputs(newInputs)
}, [inputs, setInputs])
const handleFieldChange = useCallback((field: string) => {
return (value: string) => {
const newInputs = produce(inputs, (draft: HttpNodeType) => {
(draft as any)[field] = value
})
setInputs(newInputs)
}
}, [inputs, setInputs])
const {
list: headers,
setList: setHeaders,
addItem: addHeader,
isKeyValueEdit: isHeaderKeyValueEdit,
toggleIsKeyValueEdit: toggleIsHeaderKeyValueEdit,
} = useKeyValueList(inputs.headers, handleFieldChange('headers'))
const {
list: params,
setList: setParams,
addItem: addParam,
isKeyValueEdit: isParamKeyValueEdit,
toggleIsKeyValueEdit: toggleIsParamKeyValueEdit,
} = useKeyValueList(inputs.params, handleFieldChange('params'))
const setBody = useCallback((data: Body) => {
const newInputs = produce(inputs, (draft: HttpNodeType) => {
draft.body = data
})
setInputs(newInputs)
}, [inputs, setInputs])
// authorization
const [isShowAuthorization, {
setTrue: showAuthorization,
setFalse: hideAuthorization,
}] = useBoolean(false)
const setAuthorization = useCallback((authorization: Authorization) => {
const newInputs = produce(inputs, (draft: HttpNodeType) => {
draft.authorization = authorization
})
setInputs(newInputs)
}, [inputs, setInputs])
const setTimeout = useCallback((timeout: Timeout) => {
const newInputs = produce(inputs, (draft: HttpNodeType) => {
draft.timeout = timeout
})
setInputs(newInputs)
}, [inputs, setInputs])
const filterVar = useCallback((varPayload: Var) => {
return [VarType.string, VarType.number].includes(varPayload.type)
}, [])
// single run
const {
isShowSingleRun,
hideSingleRun,
getInputVars,
runningStatus,
handleRun,
handleStop,
runInputData,
setRunInputData,
runResult,
} = useOneStepRun<HttpNodeType>({
id,
data: inputs,
defaultRunInputData: {},
})
const varInputs = getInputVars([
inputs.url,
inputs.headers,
inputs.params,
inputs.body.data,
])
const inputVarValues = (() => {
const vars: Record<string, any> = {}
Object.keys(runInputData)
.forEach((key) => {
vars[key] = runInputData[key]
})
return vars
})()
const setInputVarValues = useCallback((newPayload: Record<string, any>) => {
setRunInputData(newPayload)
}, [setRunInputData])
return {
readOnly,
inputs,
handleVarListChange,
handleAddVariable,
filterVar,
handleMethodChange,
handleUrlChange,
// headers
headers,
setHeaders,
addHeader,
isHeaderKeyValueEdit,
toggleIsHeaderKeyValueEdit,
// params
params,
setParams,
addParam,
isParamKeyValueEdit,
toggleIsParamKeyValueEdit,
// body
setBody,
// authorization
isShowAuthorization,
showAuthorization,
hideAuthorization,
setAuthorization,
setTimeout,
// single run
isShowSingleRun,
hideSingleRun,
runningStatus,
handleRun,
handleStop,
varInputs,
inputVarValues,
setInputVarValues,
runResult,
}
}
export default useConfig
|