fix: delete var of webhook (#25038)

This commit is contained in:
非法操作 2025-09-03 14:49:56 +08:00 committed by GitHub
parent ff4a62d1e7
commit 7544b5ec9a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,7 +4,7 @@ import { useTranslation } from 'react-i18next'
import type { HttpMethod, ParameterType, WebhookHeader, WebhookParameter, WebhookTriggerNodeType } from './types' import type { HttpMethod, ParameterType, WebhookHeader, WebhookParameter, WebhookTriggerNodeType } from './types'
import { getArrayElementType, isArrayType } from './types' import { getArrayElementType, isArrayType } from './types'
import { useNodesReadOnly } from '@/app/components/workflow/hooks' import { useNodesReadOnly, useWorkflow } from '@/app/components/workflow/hooks'
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud' import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
import { useStore as useAppStore } from '@/app/components/app/store' import { useStore as useAppStore } from '@/app/components/app/store'
import { fetchWebhookUrl } from '@/service/apps' import { fetchWebhookUrl } from '@/service/apps'
@ -18,6 +18,7 @@ const useConfig = (id: string, payload: WebhookTriggerNodeType) => {
const { nodesReadOnly: readOnly } = useNodesReadOnly() const { nodesReadOnly: readOnly } = useNodesReadOnly()
const { inputs, setInputs } = useNodeCrud<WebhookTriggerNodeType>(id, payload) const { inputs, setInputs } = useNodeCrud<WebhookTriggerNodeType>(id, payload)
const appId = useAppStore.getState().appDetail?.id const appId = useAppStore.getState().appDetail?.id
const { isVarUsedInNodes, removeUsedVarInNodes } = useWorkflow()
const handleMethodChange = useCallback((method: HttpMethod) => { const handleMethodChange = useCallback((method: HttpMethod) => {
setInputs(produce(inputs, (draft) => { setInputs(produce(inputs, (draft) => {
@ -64,6 +65,7 @@ const useConfig = (id: string, payload: WebhookTriggerNodeType) => {
const syncVariablesInDraft = useCallback(( const syncVariablesInDraft = useCallback((
draft: WebhookTriggerNodeType, draft: WebhookTriggerNodeType,
newData: (WebhookParameter | WebhookHeader)[], newData: (WebhookParameter | WebhookHeader)[],
sourceType: 'param' | 'header' | 'body',
) => { ) => {
if (!draft.variables) if (!draft.variables)
draft.variables = [] draft.variables = []
@ -78,6 +80,25 @@ const useConfig = (id: string, payload: WebhookTriggerNodeType) => {
return false return false
} }
// Create set of new variable names for this source
const newVarNames = new Set(newData.map(item => item.name))
// Find variables from current source that will be deleted and clean up references
draft.variables
.filter(v => v.label === sourceType && !newVarNames.has(v.variable))
.forEach((v) => {
// Clean up references if variable is used in other nodes
if (isVarUsedInNodes([id, v.variable]))
removeUsedVarInNodes([id, v.variable])
})
// Remove variables that no longer exist in newData for this specific source type
draft.variables = draft.variables.filter((v) => {
// Keep variables from other sources
if (v.label !== sourceType) return true
return newVarNames.has(v.variable)
})
// Add or update variables // Add or update variables
newData.forEach((item) => { newData.forEach((item) => {
const varName = item.name const varName = item.name
@ -85,11 +106,11 @@ const useConfig = (id: string, payload: WebhookTriggerNodeType) => {
const inputVarType = 'type' in item const inputVarType = 'type' in item
? toInputVarType(item.type) ? toInputVarType(item.type)
: InputVarType.textInput // Headers default to text : InputVarType.textInput
const newVar: InputVar = { const newVar: InputVar = {
type: inputVarType, type: inputVarType,
label: varName, label: sourceType, // Use sourceType as label to identify source
variable: varName, variable: varName,
required: item.required, required: item.required,
} }
@ -101,26 +122,26 @@ const useConfig = (id: string, payload: WebhookTriggerNodeType) => {
}) })
return true return true
}, [toInputVarType, t]) }, [toInputVarType, t, id, isVarUsedInNodes, removeUsedVarInNodes])
const handleParamsChange = useCallback((params: WebhookParameter[]) => { const handleParamsChange = useCallback((params: WebhookParameter[]) => {
setInputs(produce(inputs, (draft) => { setInputs(produce(inputs, (draft) => {
draft.params = params draft.params = params
syncVariablesInDraft(draft, params) syncVariablesInDraft(draft, params, 'param')
})) }))
}, [inputs, setInputs, syncVariablesInDraft]) }, [inputs, setInputs, syncVariablesInDraft])
const handleHeadersChange = useCallback((headers: WebhookHeader[]) => { const handleHeadersChange = useCallback((headers: WebhookHeader[]) => {
setInputs(produce(inputs, (draft) => { setInputs(produce(inputs, (draft) => {
draft.headers = headers draft.headers = headers
syncVariablesInDraft(draft, headers) syncVariablesInDraft(draft, headers, 'header')
})) }))
}, [inputs, setInputs, syncVariablesInDraft]) }, [inputs, setInputs, syncVariablesInDraft])
const handleBodyChange = useCallback((body: WebhookParameter[]) => { const handleBodyChange = useCallback((body: WebhookParameter[]) => {
setInputs(produce(inputs, (draft) => { setInputs(produce(inputs, (draft) => {
draft.body = body draft.body = body
syncVariablesInDraft(draft, body) syncVariablesInDraft(draft, body, 'body')
})) }))
}, [inputs, setInputs, syncVariablesInDraft]) }, [inputs, setInputs, syncVariablesInDraft])