MCPcopy Index your code
hub / github.com/simstudioai/sim / useWebhookManagement

Function useWebhookManagement

apps/sim/hooks/use-webhook-management.ts:86–196  ·  view source on GitHub ↗
({
  blockId,
  triggerId,
  isPreview = false,
  useWebhookUrl = false,
}: UseWebhookManagementProps)

Source from the content-addressed store, hash-verified

84 * Webhook creation/updates are handled by the deploy flow.
85 */
86export 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

Callers 3

SubBlockComponentFunction · 0.90
short-input.tsxFile · 0.90
WizardModalFunction · 0.90

Calls 6

getBaseUrlFunction · 0.90
useWebhookQueryFunction · 0.90
infoMethod · 0.80
warnMethod · 0.65

Tested by

no test coverage detected