mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
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>
102 lines
3.4 KiB
TypeScript
102 lines
3.4 KiB
TypeScript
import {
|
|
createBrowserHistory,
|
|
createRootRoute,
|
|
createRoute,
|
|
createRouter,
|
|
Outlet,
|
|
RouterProvider,
|
|
useParams,
|
|
type AnyRouter,
|
|
} from '@tanstack/react-router'
|
|
import { act, render, screen, waitFor } from '@testing-library/react'
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { STORAGE_KEY_PREFIX } from './constants'
|
|
import {
|
|
loadTableEditorStateFromLocalStorage,
|
|
useSyncTableEditorStateFromLocalStorageWithUrl,
|
|
} from './SupabaseGrid.utils'
|
|
import { ENTITY_TYPE } from '@/data/entity-types/entity-type-constants'
|
|
import type { ForeignTable } from '@/data/table-editor/table-editor-types'
|
|
import { parseSearch, stringifySearch } from '@/lib/router-search-params'
|
|
|
|
vi.mock('nuqs', async (importOriginal) => ({
|
|
...(await importOriginal<typeof import('nuqs')>()),
|
|
useQueryStates: () => [{}, vi.fn()],
|
|
}))
|
|
|
|
const tableA = '/project/default/editor/1?schema=public&filter=id%3Aeq%3A2'
|
|
const tableB = '/project/default/editor/2?schema=public'
|
|
|
|
// The hook only reads `id`; a foreign table is the smallest complete `Entity`.
|
|
const getTable = (id: number): ForeignTable => ({
|
|
entity_type: ENTITY_TYPE.FOREIGN_TABLE,
|
|
id,
|
|
schema: 'public',
|
|
name: `table_${id}`,
|
|
comment: null,
|
|
foreign_server_name: '',
|
|
foreign_data_wrapper_name: '',
|
|
foreign_data_wrapper_handler: '',
|
|
columns: [],
|
|
})
|
|
|
|
function TablePage() {
|
|
const { id } = useParams<AnyRouter, undefined, false>({ strict: false })
|
|
useSyncTableEditorStateFromLocalStorageWithUrl({
|
|
projectRef: 'default',
|
|
table: getTable(Number(id)),
|
|
})
|
|
return <output data-testid="table">{id}</output>
|
|
}
|
|
|
|
afterEach(() => {
|
|
localStorage.removeItem(`${STORAGE_KEY_PREFIX}_default`)
|
|
sessionStorage.removeItem(`${STORAGE_KEY_PREFIX}_default`)
|
|
})
|
|
|
|
describe('useSyncTableEditorStateFromLocalStorageWithUrl', () => {
|
|
it('saves filters only under the table the URL belongs to while navigation is pending', async () => {
|
|
window.history.replaceState(null, '', tableA)
|
|
const history = createBrowserHistory()
|
|
const root = createRootRoute({ component: Outlet })
|
|
const table = createRoute({
|
|
getParentRoute: () => root,
|
|
path: '/project/$ref/editor/$id',
|
|
// Keeps the old route params rendered after the location has already moved on.
|
|
loader: () => new Promise((resolve) => setTimeout(resolve, 50)),
|
|
component: TablePage,
|
|
})
|
|
const router = createRouter({
|
|
routeTree: root.addChildren([table]),
|
|
history,
|
|
parseSearch,
|
|
stringifySearch,
|
|
isServer: false,
|
|
})
|
|
await router.load()
|
|
const view = render(<RouterProvider router={router} />)
|
|
|
|
try {
|
|
await waitFor(() =>
|
|
expect(loadTableEditorStateFromLocalStorage('default', 1)?.filters).toEqual(['id:eq:2'])
|
|
)
|
|
|
|
await act(async () => history.push(tableB))
|
|
await waitFor(() => expect(screen.getByTestId('table')).toHaveTextContent('2'))
|
|
await waitFor(() =>
|
|
expect(loadTableEditorStateFromLocalStorage('default', 2)?.filters).toEqual([])
|
|
)
|
|
expect(loadTableEditorStateFromLocalStorage('default', 1)?.filters).toEqual(['id:eq:2'])
|
|
|
|
await act(async () => history.back())
|
|
await waitFor(() => expect(screen.getByTestId('table')).toHaveTextContent('1'))
|
|
expect(loadTableEditorStateFromLocalStorage('default', 2)?.filters).toEqual([])
|
|
expect(loadTableEditorStateFromLocalStorage('default', 1)?.filters).toEqual(['id:eq:2'])
|
|
} finally {
|
|
view.unmount()
|
|
history.destroy()
|
|
}
|
|
})
|
|
})
|