({
blockId,
triggerId,
isPreview = false,
useWebhookUrl = false,
}: UseWebhookManagementProps)
| 84 | * Webhook creation/updates are handled by the deploy flow. |
| 85 | */ |
| 86 | export function useWebhookManagement({ |
| 87 | blockId, |
| 88 | triggerId, |
| 89 | isPreview = false, |
| 90 | useWebhookUrl = false, |
| 91 | }: UseWebhookManagementProps): WebhookManagementState { |
| 92 | const params = useParams() |
| 93 | const workflowId = params.workflowId as string |
| 94 | const syncedRef = useRef(false) |
| 95 | |
| 96 | const webhookId = useSubBlockStore( |
| 97 | useCallback((state) => state.getValue(blockId, 'webhookId') as string | null, [blockId]) |
| 98 | ) |
| 99 | const webhookPath = useSubBlockStore( |
| 100 | useCallback((state) => state.getValue(blockId, 'triggerPath') as string | null, [blockId]) |
| 101 | ) |
| 102 | |
| 103 | const webhookUrl = useMemo(() => { |
| 104 | const baseUrl = getBaseUrl() |
| 105 | if (!webhookPath) { |
| 106 | return `${baseUrl}/api/webhooks/trigger/${blockId}` |
| 107 | } |
| 108 | return `${baseUrl}/api/webhooks/trigger/${webhookPath}` |
| 109 | }, [webhookPath, blockId]) |
| 110 | |
| 111 | useEffect(() => { |
| 112 | if (triggerId && !isPreview) { |
| 113 | const storedTriggerId = useSubBlockStore.getState().getValue(blockId, 'triggerId') |
| 114 | if (storedTriggerId !== triggerId) { |
| 115 | useSubBlockStore.getState().setValue(blockId, 'triggerId', triggerId) |
| 116 | } |
| 117 | } |
| 118 | }, [triggerId, blockId, isPreview]) |
| 119 | |
| 120 | const queryEnabled = useWebhookUrl && !isPreview && Boolean(workflowId && blockId) |
| 121 | |
| 122 | // Reset sync flag when blockId changes or query becomes disabled (render-phase guard) |
| 123 | const prevBlockIdRef = useRef(blockId) |
| 124 | if (blockId !== prevBlockIdRef.current) { |
| 125 | prevBlockIdRef.current = blockId |
| 126 | syncedRef.current = false |
| 127 | } |
| 128 | if (!queryEnabled) { |
| 129 | syncedRef.current = false |
| 130 | } |
| 131 | |
| 132 | const { data: webhook, isLoading: queryLoading } = useWebhookQuery( |
| 133 | workflowId, |
| 134 | blockId, |
| 135 | queryEnabled |
| 136 | ) |
| 137 | |
| 138 | useEffect(() => { |
| 139 | if (!queryEnabled || syncedRef.current) return |
| 140 | if (webhook === undefined) return |
| 141 | |
| 142 | if (webhook) { |
| 143 | syncedRef.current = true |
no test coverage detected