mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Part 4 of a 6-PR stack migrating all `next/router` and `next/compat/router` imports in `apps/studio` onto TanStack Router hooks and removing the router shims. | Part | PR | Scope | | --- | --- | --- | | 1 | #54053 | Core navigation helpers, `hooks/**`, `lib/**`, `data/prefetchers/**` | | 2 | #54054 | `components/ui/**` | | 3 | #54055 | `components/layouts/**` and `state/tabs.tsx` | | **4** | **#54056 ← this PR** | **Database, Branching, Compute, Edge Functions, Explorer, SQL Editor, Storage, Table Editor** | | 5 | #54057 | Remaining `components/interfaces/**` and `QueryInsightsTable` router events | | 6 | #54058 | Remove `next/router` & `next/compat/router` shims, `_router-events.ts`, Vite aliases, and test mocks | Migrates the database, branching, compute, functions, explorer, SQL editor, storage, and table editor interfaces off `next/router` and `next/compat/router`. **Changed:** - Migrated the sole `next/compat/router` consumer (`components/interfaces/Database/Replication/Destinations.tsx`) to `useNavigate` - Replaced `useRouter` across `BranchManagement`, `Branching`, `Compute`, `Database` (`Backups`, `Functions`, `Replication`, `Schemas`, `Tables`, `Triggers`), `EdgeFunctions`, `Explorer`, `Functions`, `SQLEditor`, `Storage` (`AnalyticsBuckets`, `FilesBuckets`, `VectorBuckets`, `StorageMenuV2`, `Storage.utils`), and `TableGridEditor` with TanStack Router hooks (`useNavigate`, `useLocation`, `useRoutePattern`) and `useParams` from `common` - Updated unit and component tests across `Database/Replication`, `Explorer`, `Functions`, and `Storage/FilesBuckets/DeleteBucketModal.test.tsx` to use TanStack Router **Simplified:** - `useSnippetEditor` and `useRedirectLegacyReadReplicaDestination` read search values from `useParams` instead of casting `location.search` - `resolveSourceSwitch` returns `replace: boolean` instead of a `push`/`replace` method name - `ExplorerQueryTab` and `EdgeFunctionDetails` guard on a defined ref instead of navigating with `ref ?? ''` - `useFunctionsDetailShortcuts` and `StorageMenuV2` use a local `goTo` helper for their navigation shortcuts - Tests use the shared `createTestRouter` and `customRender` router, use `toMatchObject` for search assertions, and drop duplicate assertions Addresses [FE-5109](https://linear.app/supabase/issue/FE-5109/replace-the-nextrouter-shims-with-tanstack-router-hooks) ## To test - [ ] SQL Editor: creating, renaming, moving, opening templates/examples, and keyboard shortcuts navigate between snippets cleanly - [ ] Table Editor & Database: opening tables, schema table nodes, triggers, functions, backups/PITR, and replication destination flows navigate as expected - [ ] Storage: switching between Files, Analytics, and Vector buckets and deleting a bucket navigates cleanly - [ ] Edge Functions & Explorer: opening function details/logs, explorer chat/query tabs, and branch management actions navigate properly --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
288 lines
11 KiB
TypeScript
288 lines
11 KiB
TypeScript
import { useNavigate } from '@tanstack/react-router'
|
|
import { useParams } from 'common'
|
|
import { ChevronRightIcon, MinusIcon } from 'lucide-react'
|
|
import { useState } from 'react'
|
|
import { toast } from 'sonner'
|
|
import { cn, TableCell, TableRow } from 'ui'
|
|
import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
|
|
|
|
import { DeleteDestination } from './DeleteDestination'
|
|
import { DestinationLogo } from './DestinationLogo'
|
|
import { DetailSubtext } from './DetailSubtext'
|
|
import { PipelineStatePill } from './PipelineStatePill'
|
|
import { PipelineStatusName } from './Replication.constants'
|
|
import { canModifyDestination } from './ReplicationDiagram/Nodes.utils'
|
|
import {
|
|
getFormattedLagValue,
|
|
getInitialSyncProgress,
|
|
} from './ReplicationPipelineStatus/ReplicationPipelineStatus.utils'
|
|
import { RowMenu } from './RowMenu'
|
|
import { UpdateVersionModal } from './UpdateVersionModal'
|
|
import { useDestinationInformation } from './useDestinationInformation'
|
|
import { AlertError } from '@/components/ui/AlertError'
|
|
import { useDeleteDestinationPipelineMutation } from '@/data/replication/delete-destination-pipeline-mutation'
|
|
import { useReplicationPipelineReplicationStatusQuery } from '@/data/replication/pipeline-replication-status-query'
|
|
import { useReplicationPipelineStatusQuery } from '@/data/replication/pipeline-status-query'
|
|
import { useReplicationPipelineVersionQuery } from '@/data/replication/pipeline-version-query'
|
|
import { useStopPipelineMutation } from '@/data/replication/stop-pipeline-mutation'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import { createNavigationHandler } from '@/lib/navigation'
|
|
import {
|
|
PipelineStatusRequestStatus,
|
|
usePipelineRequestStatus,
|
|
} from '@/state/replication-pipeline-request-status'
|
|
|
|
interface DestinationRowProps {
|
|
destinationId: number
|
|
}
|
|
|
|
export const DestinationRow = ({ destinationId }: DestinationRowProps) => {
|
|
const navigate = useNavigate()
|
|
const { ref: projectRef } = useParams()
|
|
const [showDeleteDestinationForm, setShowDeleteDestinationForm] = useState(false)
|
|
const [isDeleting, setIsDeleting] = useState(false)
|
|
const [showUpdateVersionModal, setShowUpdateVersionModal] = useState(false)
|
|
|
|
const { type, statusName, destination, pipeline, pipelineStatus, pipelineFetcher } =
|
|
useDestinationInformation({
|
|
id: destinationId,
|
|
})
|
|
const {
|
|
error: pipelineError,
|
|
isPending: isPipelineLoading,
|
|
isError: isPipelineError,
|
|
} = pipelineFetcher
|
|
const destinationName = destination?.name ?? pipeline?.destination_name ?? ''
|
|
|
|
const {
|
|
error: pipelineStatusError,
|
|
isPending: isPipelineStatusLoading,
|
|
isError: isPipelineStatusError,
|
|
isSuccess: isPipelineStatusSuccess,
|
|
} = useReplicationPipelineStatusQuery({
|
|
projectRef,
|
|
pipelineId: pipeline?.id,
|
|
})
|
|
const { getRequestStatus } = usePipelineRequestStatus()
|
|
const requestStatus = pipeline?.id
|
|
? getRequestStatus(pipeline.id)
|
|
: PipelineStatusRequestStatus.None
|
|
|
|
const { mutateAsync: stopPipeline } = useStopPipelineMutation({ onError: () => {} })
|
|
const { mutateAsync: deleteDestinationPipeline } = useDeleteDestinationPipelineMutation({
|
|
onError: () => {},
|
|
})
|
|
|
|
// Fetch table-level replication status to surface errors in list view
|
|
const {
|
|
data: replicationStatusData,
|
|
isPending: isReplicationStatusLoading,
|
|
isError: isReplicationStatusError,
|
|
} = useReplicationPipelineReplicationStatusQuery({ projectRef, pipelineId: pipeline?.id }, {})
|
|
const tableStatuses = replicationStatusData?.table_statuses ?? []
|
|
const errorCount = tableStatuses.filter((t) => t.state?.name === 'error').length
|
|
const applyLag = replicationStatusData?.apply_lag
|
|
// Slot acknowledgment can trail local worker progress; missing lag is not a caught-up signal.
|
|
const lagBytes = applyLag?.confirmed_flush_lsn_bytes
|
|
const lag = getFormattedLagValue('bytes', lagBytes)
|
|
// The lag figure only covers ongoing changes, so it reads as "Caught up" while an initial copy
|
|
// is still running. Say what's actually happening instead.
|
|
const { syncingCount } = getInitialSyncProgress(tableStatuses)
|
|
const isInitialSyncRunning = syncingCount > 0
|
|
const isCaughtUp = lagBytes === 0
|
|
// Hide old table errors while an optimistic lifecycle action is displayed.
|
|
const isPipelineStopped = statusName === PipelineStatusName.STOPPED
|
|
const isTransitioning = requestStatus !== PipelineStatusRequestStatus.None
|
|
const hasTableErrors = errorCount > 0 && !isPipelineStopped && !isTransitioning
|
|
|
|
// Check if a newer pipeline version is available (one-time check cached for session)
|
|
const { data: versionData } = useReplicationPipelineVersionQuery({
|
|
projectRef,
|
|
pipelineId: pipeline?.id,
|
|
})
|
|
const hasUpdate = Boolean(versionData?.new_version)
|
|
|
|
const handleNavigation = pipeline
|
|
? createNavigationHandler(`/project/${projectRef}/database/pipelines/${pipeline.id}`, navigate)
|
|
: undefined
|
|
|
|
const onDeleteClick = async () => {
|
|
if (!projectRef) {
|
|
return console.error('Project ref is required')
|
|
}
|
|
if (!pipeline) {
|
|
return toast.error('No pipeline found')
|
|
}
|
|
|
|
try {
|
|
setIsDeleting(true)
|
|
await stopPipeline({ projectRef, pipelineId: pipeline.id, waitUntilStopped: true })
|
|
await deleteDestinationPipeline({
|
|
projectRef,
|
|
destinationId: destinationId,
|
|
pipelineId: pipeline.id,
|
|
})
|
|
// Close dialog after successful deletion
|
|
setShowDeleteDestinationForm(false)
|
|
toast.success(`Deleted pipeline "${destinationName}"`)
|
|
} catch (error) {
|
|
toast.error(`Failed to delete pipeline: ${(error as StudioError).message}`)
|
|
} finally {
|
|
setIsDeleting(false)
|
|
}
|
|
}
|
|
|
|
// Five distinct states, so early returns rather than a ternary chain.
|
|
const renderLag = () => {
|
|
if (isReplicationStatusLoading) return <ShimmeringLoader />
|
|
if (isInitialSyncRunning)
|
|
return <span className="text-foreground-light whitespace-nowrap">Initial sync</span>
|
|
if (isReplicationStatusError || !applyLag)
|
|
return (
|
|
<>
|
|
<MinusIcon size={18} className="text-foreground-lighter" aria-hidden />
|
|
<span className="sr-only">Lag unavailable</span>
|
|
</>
|
|
)
|
|
if (isCaughtUp)
|
|
return <span className="text-foreground-light whitespace-nowrap">Caught up</span>
|
|
return <span className="text-foreground-light whitespace-nowrap">{lag.display}</span>
|
|
}
|
|
|
|
return (
|
|
<>
|
|
{isPipelineError && (
|
|
<TableRow>
|
|
<TableCell colSpan={6}>
|
|
<AlertError error={pipelineError} subject="Failed to retrieve pipeline information" />
|
|
</TableCell>
|
|
</TableRow>
|
|
)}
|
|
{(isPipelineLoading || pipeline) && (
|
|
<TableRow
|
|
className={cn('relative', pipeline && 'cursor-pointer focus-inset')}
|
|
onClick={handleNavigation}
|
|
onAuxClick={handleNavigation}
|
|
onKeyDown={handleNavigation}
|
|
tabIndex={pipeline ? 0 : undefined}
|
|
>
|
|
<TableCell className="!pr-1">
|
|
{type ? <DestinationLogo type={type} hasErrors={hasTableErrors} /> : null}
|
|
</TableCell>
|
|
|
|
<TableCell className="max-w-[180px]">
|
|
{isPipelineLoading && !destinationName ? (
|
|
<ShimmeringLoader />
|
|
) : (
|
|
<div className="flex flex-col gap-y-0.5">
|
|
<p className="text-sm font-medium text-foreground truncate">
|
|
{destinationName || type}
|
|
</p>
|
|
{pipeline && (
|
|
<DetailSubtext className="flex items-center gap-x-1.5">
|
|
<span>#{pipeline.id}</span>
|
|
<span aria-hidden>·</span>
|
|
<span>{type}</span>
|
|
{hasTableErrors && (
|
|
<>
|
|
<span aria-hidden>·</span>
|
|
<span className="text-destructive">
|
|
{errorCount} table error{errorCount === 1 ? '' : <span>s</span>}
|
|
</span>
|
|
</>
|
|
)}
|
|
</DetailSubtext>
|
|
)}
|
|
</div>
|
|
)}
|
|
</TableCell>
|
|
|
|
<TableCell>
|
|
{isPipelineLoading || !pipeline ? (
|
|
<ShimmeringLoader />
|
|
) : (
|
|
<PipelineStatePill
|
|
pipelineStatus={pipelineStatus?.status}
|
|
error={pipelineStatusError}
|
|
isLoading={isPipelineStatusLoading}
|
|
isError={isPipelineStatusError}
|
|
isSuccess={isPipelineStatusSuccess}
|
|
requestStatus={requestStatus}
|
|
projectRef={projectRef}
|
|
pipelineId={pipeline?.id}
|
|
/>
|
|
)}
|
|
</TableCell>
|
|
|
|
<TableCell>
|
|
<div aria-live="polite" aria-atomic="true">
|
|
{isReplicationStatusLoading && <span className="sr-only">Loading lag</span>}
|
|
{renderLag()}
|
|
</div>
|
|
</TableCell>
|
|
|
|
<TableCell>
|
|
{isPipelineLoading || !pipeline ? (
|
|
<ShimmeringLoader />
|
|
) : (
|
|
pipeline.config.publication_name
|
|
)}
|
|
</TableCell>
|
|
|
|
<TableCell>
|
|
<div className="flex items-center justify-end gap-x-2">
|
|
{pipeline ? (
|
|
<>
|
|
<div
|
|
onClick={(event) => event.stopPropagation()}
|
|
onAuxClick={(event) => event.stopPropagation()}
|
|
onKeyDown={(event) => event.stopPropagation()}
|
|
>
|
|
<RowMenu
|
|
destinationId={destinationId}
|
|
destinationType={type}
|
|
pipeline={pipeline}
|
|
pipelineStatus={pipelineStatus?.status}
|
|
error={pipelineStatusError}
|
|
isLoading={isPipelineStatusLoading}
|
|
isError={isPipelineStatusError}
|
|
onDeleteClick={() => setShowDeleteDestinationForm(true)}
|
|
hasUpdate={hasUpdate}
|
|
onUpdateClick={() => setShowUpdateVersionModal(true)}
|
|
/>
|
|
</div>
|
|
<ChevronRightIcon
|
|
size={16}
|
|
strokeWidth={1.5}
|
|
className="text-foreground-lighter"
|
|
aria-hidden
|
|
/>
|
|
<button tabIndex={-1} className="sr-only">
|
|
Go to pipeline details
|
|
</button>
|
|
</>
|
|
) : (
|
|
<ShimmeringLoader className="h-6 w-6 py-0" />
|
|
)}
|
|
</div>
|
|
</TableCell>
|
|
</TableRow>
|
|
)}
|
|
|
|
<DeleteDestination
|
|
visible={showDeleteDestinationForm}
|
|
setVisible={setShowDeleteDestinationForm}
|
|
onDelete={onDeleteClick}
|
|
isLoading={isDeleting}
|
|
name={destinationName}
|
|
/>
|
|
|
|
<UpdateVersionModal
|
|
visible={canModifyDestination(type) && showUpdateVersionModal}
|
|
pipeline={pipeline}
|
|
onClose={() => setShowUpdateVersionModal(false)}
|
|
/>
|
|
</>
|
|
)
|
|
}
|