演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
55 lines
2.0 KiB
TypeScript
55 lines
2.0 KiB
TypeScript
import { useQueryClient } from '@tanstack/react-query'
|
|
import { produce } from 'immer'
|
|
import { useAtomValue } from 'jotai'
|
|
import { useCallback } from 'react'
|
|
import { editorAppIdAtom } from '@/app/components/workflow/editor-resource/state'
|
|
import { BlockEnum } from '@/app/components/workflow/types'
|
|
import { consoleQuery } from '@/service/console'
|
|
import { useCollaborativeWorkflow } from './use-collaborative-workflow'
|
|
|
|
export const useAutoGenerateWebhookUrl = () => {
|
|
const appId = useAtomValue(editorAppIdAtom)
|
|
const queryClient = useQueryClient()
|
|
const collaborativeWorkflow = useCollaborativeWorkflow()
|
|
|
|
return useCallback(
|
|
async (nodeId: string) => {
|
|
if (!appId) return
|
|
|
|
const { nodes } = collaborativeWorkflow.getState()
|
|
const node = nodes.find((n) => n.id === nodeId)
|
|
if (!node || node.data.type !== BlockEnum.TriggerWebhook) return
|
|
|
|
if (node.data.webhook_url && node.data.webhook_url.length > 0) return
|
|
|
|
try {
|
|
const response = await queryClient.query(
|
|
consoleQuery.apps.byAppId.workflows.triggers.webhook.get.queryOptions({
|
|
input: { params: { app_id: appId }, query: { node_id: nodeId } },
|
|
staleTime: 0,
|
|
retry: false,
|
|
}),
|
|
)
|
|
const { nodes: latestNodes, setNodes } = collaborativeWorkflow.getState()
|
|
let hasUpdated = false
|
|
const updatedNodes = produce(latestNodes, (draft) => {
|
|
const targetNode = draft.find((n) => n.id === nodeId)
|
|
if (!targetNode || targetNode.data.type !== BlockEnum.TriggerWebhook) return
|
|
|
|
targetNode.data = {
|
|
...targetNode.data,
|
|
webhook_url: response.webhook_url,
|
|
webhook_debug_url: response.webhook_debug_url,
|
|
}
|
|
hasUpdated = true
|
|
})
|
|
|
|
if (hasUpdated) setNodes(updatedNodes)
|
|
} catch (error: unknown) {
|
|
console.error('Failed to auto-generate webhook URL:', error)
|
|
}
|
|
},
|
|
[appId, collaborativeWorkflow, queryClient],
|
|
)
|
|
}
|