Files
supabase/apps/studio/components/layouts/StorageLayout/StorageBucketsLayout.tsx
Alaister YoungandAlaister Young 953e508a3e [FE-5110] refactor(studio): replace next/navigation with TanStack (#54041)
Moves the remaining `next/navigation` imports in Studio onto TanStack
Router hooks (and `useParams` from `common`), finishes migrating the
remaining `next/link` and `next/router` usages in the touched
components, and removes the `compat/next/navigation.ts` shim and its
Vite alias. Shared packages (`packages/**`) no longer import
`next/navigation`, so the alias can be dropped now.

**Removed:**

- `apps/studio/compat/next/navigation.ts` and the `'next/navigation'`
entry in `apps/studio/vite.config.ts`
- Global `next/navigation` mock in `apps/studio/tests/vitestSetup.ts`
and the obsolete `apps/studio/tests/lib/route-mock.test.tsx`

**Changed:**

- Replaced `usePathname` (12 files) and `useSearchParams` (3 files) with
`useLocation({ select })` from `@tanstack/react-router`
- Replaced `useRouter` in Storage bucket tables, SQL Editor commands,
and `SignInMfaForm` with `useNavigate` from `@tanstack/react-router`,
and updated `createNavigationHandler` in `lib/navigation.ts` to accept
either `NextRouter` or TanStack's `useNavigate`
- Replaced remaining `next/link` imports in touched components
(`SignInMfaForm`, `Restriction`, `DownloadLogsButton`,
`DownloadResultsButton`, `AuthEmailsLayout`, `AuthProvidersLayout`,
`JWTKeysLayout`, `StorageBucketsLayout`) with TanStack Router's `Link`
- Replaced `useParams` in `CreatePublishableAPIKeyDialog` with
`useParams` from `common` (matching `CreateSecretAPIKeyDialog`)
- Wrapped `CustomWrapper` (`tests/lib/custom-render.tsx`) in a default
TanStack `RouterContextProvider` (with optional `router` / `initialUrl`
overrides) and updated affected component tests to drop their
`next/navigation` mocks

Addresses
[FE-5110](https://linear.app/supabase/issue/FE-5110/replace-the-nextnavigation-shim-with-tanstack-router-hooks)

## To test

1. Check active tab highlighting and navigation across Auth Emails, Auth
Providers, JWT Keys, Storage Buckets, and Editor layouts
2. Verify clicking, Cmd/Ctrl-clicking, middle-clicking, and pressing
Enter on Storage bucket rows (Files, Analytics, Vector) navigates as
before
3. Run "Query a table..." and open SQL snippets from the Command Menu
and confirm `/project/$ref/sql/$id` opens with multi-line SQL preserved
in `?content=`
4. In the MFA sign-in flow, verify the recovery code link and
`forgot-password` completion preserve query params (`returnTo`,
`context=forgot-password`), and Cancel signs out and replaces history
with `/sign-in`
5. Confirm Table Editor sort/filter URL sync persists filters per table
without leaking across table switches

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-11-26 17:18:49 +08:00

101 lines
3.0 KiB
TypeScript

import { Link, useMatchRoute } from '@tanstack/react-router'
import { IS_PLATFORM, useParams } from 'common'
import { PropsWithChildren } from 'react'
import { NavMenu, NavMenuItem } from 'ui'
import {
PageHeader,
PageHeaderAside,
PageHeaderDescription,
PageHeaderMeta,
PageHeaderNavigationTabs,
PageHeaderSummary,
PageHeaderTitle,
} from 'ui-patterns/PageHeader'
import { BUCKET_TYPES } from '@/components/interfaces/Storage/Storage.constants'
import { useStorageV2Page } from '@/components/interfaces/Storage/Storage.utils'
import { URL_SIGNING_KEYS_PAGE } from '@/components/interfaces/Storage/UrlSigningKeys/UrlSigningKeys.constants'
import { DocsButton } from '@/components/ui/DocsButton'
const getPageHeaderConfig = (page: ReturnType<typeof useStorageV2Page>) => {
if (page === 'signing-keys') return URL_SIGNING_KEYS_PAGE
if (page === undefined || page === 's3') return undefined
return BUCKET_TYPES[page]
}
export const StorageBucketsLayout = ({
title,
hideSubtitle = false,
children,
}: PropsWithChildren<{ title?: string; hideSubtitle?: boolean }>) => {
const { ref } = useParams()
const matchRoute = useMatchRoute()
const page = useStorageV2Page()
const config = getPageHeaderConfig(page)
const navigationItems =
page === 'files'
? [
{
label: 'Buckets',
to: '/project/$ref/storage/files' as const,
},
...(IS_PLATFORM
? [
{
label: 'Settings',
to: '/project/$ref/storage/files/settings' as const,
},
]
: []),
{
label: 'Policies',
to: '/project/$ref/storage/files/policies' as const,
},
]
: []
return (
<>
<PageHeader>
<PageHeaderMeta>
<PageHeaderSummary>
<PageHeaderTitle>{title || (config?.displayName ?? 'Storage')}</PageHeaderTitle>
{!hideSubtitle && (
<PageHeaderDescription>
{config?.description || 'Manage your storage buckets and files.'}
</PageHeaderDescription>
)}
</PageHeaderSummary>
<PageHeaderAside>
{config?.docsUrl && <DocsButton key="docs" href={config.docsUrl} />}
</PageHeaderAside>
</PageHeaderMeta>
{navigationItems.length > 0 && (
<PageHeaderNavigationTabs>
<NavMenu>
{navigationItems.map((item) => (
<NavMenuItem
key={item.label}
active={!!matchRoute({ to: item.to, params: { ref: ref ?? '_' }, fuzzy: false })}
>
<Link
to={item.to}
params={{ ref: ref ?? '_' }}
activeOptions={{ exact: true, includeSearch: false }}
>
{item.label}
</Link>
</NavMenuItem>
))}
</NavMenu>
</PageHeaderNavigationTabs>
)}
</PageHeader>
{children}
</>
)
}