Files
supabase/apps/studio/state/replication-pipeline-request-status.tsx
edec85d1ca fix(pipelines): Make pipeline actions and status updates reliable (#50085)
## Summary

Make pipeline actions and status feedback reliable while requests are
running or fail. Let the backend coordinate table resets and restarts,
keep stopped pipelines stopped after resets or settings changes, and
refresh the UI from confirmed backend state.

## Pipeline actions and recovery

- Reset one table, all errored tables, or all tables through the
rollback endpoint without separate frontend stop/start requests. Explain
which destination data is deleted, which rows are copied again, initial
sync charges, and the skip-initial-sync setting.
- Keep pending feedback until the action and a fresh status read finish,
including across navigation and polling errors. Prevent overlapping
actions and disable start/stop controls when status is unavailable or
transitioning.
- Close the creation form once the pipeline is created. If its initial
start fails, users can retry Start on the existing pipeline without
creating a duplicate.
- Wait for confirmed shutdown before deletion; a shutdown error or
timeout leaves deletion retryable. Keep failed version updates open and
avoid reporting success.
- Clarify recovery guidance and pending labels, suppress duplicate error
toasts, and hide stale table errors during transitions.

## Status updates and shared UI

- Poll pipeline status and table metrics one second after each response,
share in-flight reads, pause dashboard polling in background tabs, and
respect rate-limit backoff. The shutdown waiter continues in the
background.
- Refresh metadata after mutations even when an older read is in flight,
while preserving shared polling requests. Refresh affected data after
failures that may follow a committed reset or settings change.
- Move pending request state into the shared, project-keyed
`DatabaseLayout` so the list, detail page, and diagram stay consistent.
The surrounding database-page changes update named imports in both
Next.js and TanStack routes.
- Simplify action, status, and form rendering; announce status changes
to assistive technology; and sort table statuses without mutating cached
data.

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: Danny White <3104761+dnywh@users.noreply.github.com>
2026-09-18 11:32:48 +08:00

91 lines
3.1 KiB
TypeScript

import { useQueryClient } from '@tanstack/react-query'
import { useParams } from 'common'
import { createContext, useContext, useState, type ReactNode } from 'react'
import { replicationPipelineStatusQueryOptions } from '@/data/replication/pipeline-status-query'
export enum PipelineStatusRequestStatus {
None = 'None',
StartRequested = 'StartRequested',
StopRequested = 'StopRequested',
}
type PipelineRequest = {
id: symbol
status: PipelineStatusRequestStatus
}
interface PipelineRequestStatusContextType {
getRequestStatus: (pipelineId: number) => PipelineStatusRequestStatus
isRequestPending: (pipelineId: number) => boolean
runWithRequestStatus: <T>(
pipelineId: number,
status: PipelineStatusRequestStatus,
action: () => Promise<T>
) => Promise<T>
}
const PipelineRequestStatusContext = createContext<PipelineRequestStatusContextType | undefined>(
undefined
)
export const PipelineRequestStatusProvider = ({ children }: { children: ReactNode }) => {
const { ref: projectRef } = useParams()
const queryClient = useQueryClient()
const [requests, setRequests] = useState<Record<number, PipelineRequest>>({})
const runWithRequestStatus: PipelineRequestStatusContextType['runWithRequestStatus'] = async (
pipelineId,
status,
action
) => {
const id = Symbol('pipeline request')
setRequests((previous) => ({ ...previous, [pipelineId]: { id, status } }))
try {
return await action()
} finally {
const options = {
...replicationPipelineStatusQueryOptions({ projectRef, pipelineId }),
staleTime: 0,
}
// A status fetch that was already in flight started before this mutation resolved, so it
// may resolve with pre-mutation data. `fetchQuery` dedupes against it instead of starting
// a new request, so first wait for it to drain.
const hasFetchInFlight =
queryClient.getQueryState(options.queryKey)?.fetchStatus === 'fetching'
if (hasFetchInFlight) await queryClient.fetchQuery(options).catch(() => {})
// Nothing is in flight now, so this always starts a fresh request reflecting the
// post-mutation state. Errors are swallowed: query consumers already display fetch
// failures, and we don't want that to override the mutation's own result/error.
await queryClient.fetchQuery(options).catch(() => {})
setRequests((previous) => {
if (previous[pipelineId]?.id !== id) return previous
const { [pipelineId]: _removed, ...rest } = previous
return rest
})
}
}
return (
<PipelineRequestStatusContext.Provider
value={{
getRequestStatus: (pipelineId) =>
requests[pipelineId]?.status ?? PipelineStatusRequestStatus.None,
isRequestPending: (pipelineId) => requests[pipelineId] !== undefined,
runWithRequestStatus,
}}
>
{children}
</PipelineRequestStatusContext.Provider>
)
}
export const usePipelineRequestStatus = () => {
const context = useContext(PipelineRequestStatusContext)
if (context === undefined)
throw new Error('usePipelineRequestStatus must be used within a PipelineRequestStatusProvider')
return context
}