Files
Monica Khoury 8bdc2bf0c5 feat(studio): refresh webhook deliveries on demand instead of polling (#52444)
## Problem

The deliveries table polled the API every 5 seconds, for as long as the
page was open. Studio tables are not polled by default, and polling is
only needed here because delivery results arrive asynchronously.

Stacked on #52314. Linear:
[FE-3750](https://linear.app/supabase/issue/FE-3750/add-pagination-to-platform-webhooks-deliveries-table)

## Solution

- Deliveries are no longer polled by default.
- A **Live** button turns polling on (every 5 seconds), and a
**Refresh** button reloads once.
- Polling also turns on by itself while a delivery is pending, and after
a test event or retry is sent until its delivery appears (30 seconds at
most). The action never looks like it did nothing.
- Hovering a failed delivery explains that failed deliveries are usually
retried automatically. The API returns no retry state yet, so a
scheduled retry only shows up once its attempt starts.

## Review instructions

1. Enable the Platform Webhooks feature preview, open an endpoint with
deliveries, and filter DevTools > Network by `deliveries`. No request
repeats on its own.
2. Click Refresh. One request is sent.
3. Click Live. A request is sent every 5 seconds and the dot pulses.
Click Live again and the requests stop.
4. Send a test event. Requests repeat for a few seconds until the new
row appears, then stop.
5. Set the endpoint URL to `https://httpbin.org/delay/10` and send a
test event. While the new row shows `pending`, requests repeat. Once it
shows `success`, they stop.
6. Hover a `failure` badge. The retry tooltip shows.
2026-10-29 12:44:44 +02:00

99 lines
3.1 KiB
TypeScript

import { DELIVERIES_POLL_INTERVAL_MS } from './PlatformWebhooks.constants'
import type { WebhookDelivery, WebhookScope } from './PlatformWebhooks.types'
export const shouldHandleEndpointNotFound = ({
endpointId,
hasSelectedEndpoint,
pendingCreatedEndpointId,
}: {
endpointId?: string
hasSelectedEndpoint: boolean
pendingCreatedEndpointId: string | null
}) => {
if (!endpointId) return false
if (hasSelectedEndpoint) return false
return endpointId !== pendingCreatedEndpointId
}
/**
* The table's rows refresh every few seconds, but the sheet's delivery is loaded once, so its status
* can be out of date. Prefer the table's row, and only fall back to the sheet's delivery when it is
* not on the current page.
*/
export const findDeliveryToRetry = ({
deliveryId,
selectedDelivery,
pageDeliveries,
}: {
deliveryId: string
selectedDelivery: WebhookDelivery | null
pageDeliveries: WebhookDelivery[]
}) =>
pageDeliveries.find((delivery) => delivery.id === deliveryId) ??
(selectedDelivery?.id === deliveryId ? selectedDelivery : undefined)
/**
* Only redirect away once the flags have loaded and the route params are populated; before
* that, the flag reads `false` and `slug`/`ref` are undefined even for enabled users.
*/
export const shouldRedirectFromWebhooks = ({
areFlagsReady,
isScopeReady,
isEnabled,
}: {
areFlagsReady: boolean
isScopeReady: boolean
isEnabled: boolean
}) => areFlagsReady && isScopeReady && !isEnabled
interface PendingSigningSecretReveal {
endpointId: string
signingSecret: string
}
const pendingSigningSecretRevealByScope: Partial<Record<WebhookScope, PendingSigningSecretReveal>> =
{}
export const setPendingSigningSecretReveal = (
scope: WebhookScope,
value: PendingSigningSecretReveal
) => {
pendingSigningSecretRevealByScope[scope] = value
}
export const getPendingSigningSecretReveal = (scope: WebhookScope, endpointId?: string) => {
const pending = pendingSigningSecretRevealByScope[scope]
if (!pending) return null
if (endpointId && pending.endpointId !== endpointId) return null
return pending
}
export const clearPendingSigningSecretReveal = (scope: WebhookScope) => {
delete pendingSigningSecretRevealByScope[scope]
}
export const resetPendingSigningSecretRevealForTests = () => {
for (const key of Object.keys(pendingSigningSecretRevealByScope) as WebhookScope[]) {
delete pendingSigningSecretRevealByScope[key]
}
}
/**
* Deliveries are not polled by default. Polling is on while the user chose "Live", while a delivery
* is still pending, and for a short wait after a test event or retry was sent. That wait cannot end
* when the new row shows up, because the API returns an ingress ID, not the ID of the delivery.
*/
export const getDeliveriesRefetchInterval = ({
isLive,
isAwaitingDelivery,
deliveries,
}: {
isLive: boolean
isAwaitingDelivery: boolean
deliveries: WebhookDelivery[]
}) => {
const hasPendingDelivery = deliveries.some((delivery) => delivery.status === 'pending')
return isLive || isAwaitingDelivery || hasPendingDelivery ? DELIVERIES_POLL_INTERVAL_MS : false
}