mirror of
https://github.com/supabase/supabase.git
synced 2026-12-05 07:59:03 +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>
145 lines
5.0 KiB
TypeScript
145 lines
5.0 KiB
TypeScript
import { useNavigate } from '@tanstack/react-router'
|
|
import { useParams } from 'common'
|
|
import dayjs from 'dayjs'
|
|
import { ClockIcon } from 'lucide-react'
|
|
import { useState } from 'react'
|
|
import { toast } from 'sonner'
|
|
import { Admonition } from 'ui-patterns/Admonition'
|
|
import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
|
|
import { TimestampInfo } from 'ui-patterns/TimestampInfo'
|
|
|
|
import { BackupItem } from './BackupItem'
|
|
import { BackupsEmpty } from './BackupsEmpty'
|
|
import { BackupsStorageAlert } from './BackupsStorageAlert'
|
|
import Panel from '@/components/ui/Panel'
|
|
import { UpgradeToPro } from '@/components/ui/UpgradeToPro'
|
|
import { useBackupRestoreMutation } from '@/data/database/backup-restore-mutation'
|
|
import { DatabaseBackup, useBackupsQuery } from '@/data/database/backups-query'
|
|
import { useSetProjectStatus } from '@/data/projects/project-detail-query'
|
|
import { useCheckEntitlements } from '@/hooks/misc/useCheckEntitlements'
|
|
import { useIsHighAvailability, useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
|
|
import { PROJECT_STATUS } from '@/lib/constants'
|
|
|
|
export const BackupsList = () => {
|
|
const navigate = useNavigate()
|
|
const { ref: projectRef } = useParams()
|
|
const [selectedBackup, setSelectedBackup] = useState<DatabaseBackup>()
|
|
const { hasAccess: hasAccessToBackups } = useCheckEntitlements('backup.retention_days')
|
|
|
|
const { setProjectStatus } = useSetProjectStatus()
|
|
const { data: selectedProject } = useSelectedProjectQuery()
|
|
const isHealthy = selectedProject?.status === PROJECT_STATUS.ACTIVE_HEALTHY
|
|
const isHighAvailability = useIsHighAvailability()
|
|
|
|
const { data: backups } = useBackupsQuery({ projectRef })
|
|
const {
|
|
mutate: restoreFromBackup,
|
|
isPending: isRestoring,
|
|
isSuccess: isSuccessBackup,
|
|
} = useBackupRestoreMutation({
|
|
onSuccess: () => {
|
|
if (projectRef) {
|
|
setTimeout(() => {
|
|
setProjectStatus({ ref: projectRef, status: PROJECT_STATUS.RESTORING })
|
|
toast.success(
|
|
`Restoring database back to ${dayjs(selectedBackup?.inserted_at).format(
|
|
'DD MMM YYYY HH:mm:ss'
|
|
)}`
|
|
)
|
|
void navigate({ to: '/project/$ref', params: { ref: projectRef } })
|
|
}, 3000)
|
|
}
|
|
},
|
|
})
|
|
|
|
const sortedBackups = (backups?.backups ?? []).sort(
|
|
(a, b) => new Date(b.inserted_at).valueOf() - new Date(a.inserted_at).valueOf()
|
|
)
|
|
const isPitrEnabled = backups?.pitr_enabled
|
|
|
|
if (!hasAccessToBackups) {
|
|
return (
|
|
<UpgradeToPro
|
|
addon="pitr"
|
|
source="backups"
|
|
featureProposition="have up to 7 days of scheduled backups"
|
|
icon={<ClockIcon size={20} />}
|
|
primaryText="Free Plan does not include project backups."
|
|
secondaryText="Upgrade to the Pro Plan for up to 7 days of scheduled backups."
|
|
buttonText="Upgrade"
|
|
/>
|
|
)
|
|
}
|
|
|
|
if (isPitrEnabled) return null
|
|
|
|
return (
|
|
<>
|
|
<div className="space-y-6">
|
|
{sortedBackups.length === 0 ? (
|
|
<BackupsEmpty />
|
|
) : (
|
|
<>
|
|
<BackupsStorageAlert />
|
|
<Panel>
|
|
{sortedBackups?.map((x, i: number) => {
|
|
return (
|
|
<BackupItem
|
|
key={x.id}
|
|
backup={x}
|
|
index={i}
|
|
isHealthy={isHealthy}
|
|
isHighAvailability={isHighAvailability}
|
|
onSelectBackup={() => setSelectedBackup(x)}
|
|
/>
|
|
)
|
|
})}
|
|
</Panel>
|
|
</>
|
|
)}
|
|
</div>
|
|
<ConfirmationModal
|
|
size="small"
|
|
confirmLabel="Restore"
|
|
confirmLabelLoading="Restoring..."
|
|
variant="warning"
|
|
visible={selectedBackup !== undefined}
|
|
title="Restore from backup"
|
|
loading={isRestoring || isSuccessBackup}
|
|
onCancel={() => setSelectedBackup(undefined)}
|
|
onConfirm={() => {
|
|
if (projectRef === undefined) return console.error('Project ref required')
|
|
if (selectedBackup === undefined) return console.error('Backup required')
|
|
restoreFromBackup({ ref: projectRef, backup: selectedBackup })
|
|
}}
|
|
>
|
|
<div className="space-y-3">
|
|
{!!selectedBackup && (
|
|
<p className="text-sm">
|
|
This will restore your database to the backup made on{' '}
|
|
<TimestampInfo
|
|
displayAs="utc"
|
|
utcTimestamp={selectedBackup.inserted_at}
|
|
labelFormat="DD MMM YYYY HH:mm:ss (ZZ)"
|
|
className="text-sm!"
|
|
/>
|
|
</p>
|
|
)}
|
|
|
|
<Admonition
|
|
showIcon={false}
|
|
type="warning"
|
|
title="This action cannot be undone"
|
|
description={
|
|
<ul className="list-disc list-inside">
|
|
<li>Your project will be offline during restoration</li>
|
|
<li>Any new data since this backup will be lost</li>
|
|
</ul>
|
|
}
|
|
/>
|
|
</div>
|
|
</ConfirmationModal>
|
|
</>
|
|
)
|
|
}
|