mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
Part 6 (final) 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 | Database, Branching, Compute, Edge Functions, Explorer, SQL Editor, Storage, Table Editor | | 5 | #54057 | Remaining `components/interfaces/**` and `QueryInsightsTable` router events | | **6** | **#54058 ← this PR** | **Remove `next/router` & `next/compat/router` shims, `_router-events.ts`, Vite aliases, and test mocks** | Now that no files in `apps/studio` (or `packages/**`) import `next/router` or `next/compat/router`, deletes the router shims, `_router-events.ts`, their Vite aliases, and the obsolete router test mocks. `RouteValidationWrapper` is intentionally left for FE-3107. **Removed:** - `apps/studio/compat/next/router.ts`, `apps/studio/compat/next/compat/router.ts`, and `apps/studio/compat/next/_router-events.ts` - `'next/router'` and `'next/compat/router'` entries from `nextShims` in `apps/studio/vite.config.ts` - Global `vi.mock('next/router', ...)` and `vi.mock('next/compat/router', ...)` in `apps/studio/tests/vitestSetup.ts`, `mockNextRouter` in `apps/studio/tests/lib/route-aware-hooks.ts`, and the unused `vi.mock('next/router')` in `tests/features/logs/LogTable.test.tsx` - Obsolete shim tests and test helpers: `compat/next/router.test.ts`, `compat/next/router-navigation.test.tsx`, `tests/config/router.tsx`, and `tests/config/router.test.tsx` **Changed:** - Simplified `createNavigationHandler` in `apps/studio/lib/navigation.ts` (and `navigation.test.ts`) to accept TanStack's `UseNavigateResult<string>` only, dropping the `NextRouter` union branch - Updated `compat/next/link-basepath-navigation.test.tsx` (renamed from `router-basepath-navigation.test.tsx`), `lib/router-url-shape.test.tsx`, and `tests/lib/route-render.test.tsx` to exercise TanStack Router hooks directly instead of the removed router shim - Cleaned up stale `next/router` comments in `compat/next/link.tsx`, `lib/internal-url.ts`, `router.tsx`, and `tests/lib/route-mock.ts` **Simplified:** - Removed `routerMock` setup from about 19 tests that no longer read it. `next-router-mock` stays for now, because the test `FrameworkProvider` still feeds `useParams` from it. - `AGENTS.md` and the `vite.config.ts` error text now say only `next/link` and `next/script` are shimmed - Renamed `compat/next/router-basepath-navigation.test.tsx` to `link-basepath-navigation.test.tsx`, and removed stale router-shim references from comments Addresses [FE-5109](https://linear.app/supabase/issue/FE-5109/replace-the-nextrouter-shims-with-tanstack-router-hooks) ## To test - [ ] Confirm `git grep -E "next/(compat/)?router|_router-events" apps/studio` returns no code or config imports (only the prose guideline in `apps/studio/AGENTS.md`) - [ ] Confirm `pnpm --filter studio run typecheck`, `pnpm --filter studio run lint`, `pnpm --filter studio run build`, Knip, and the full Studio Vitest suite pass - [ ] Spot-check basePath navigation (`next/link` + `navigateToUrl`) and multi-line query param URLs in Studio --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
88 lines
3.0 KiB
TypeScript
88 lines
3.0 KiB
TypeScript
import type { AnyRouter, UseNavigateResult } from '@tanstack/react-router'
|
|
|
|
import { BASE_PATH } from './constants'
|
|
import { isAbsoluteUrl, splitInternalUrl } from './internal-url'
|
|
|
|
const MIDDLE_MOUSE_BUTTON = 1
|
|
|
|
/** `url` must be an app-relative absolute path (`/project/...`, optionally with ?query/#hash) or an external URL; bare `?query`/`#hash` targets aren't resolved against the current path. */
|
|
export const navigateToUrl = (
|
|
navigate: UseNavigateResult<string>,
|
|
url: string,
|
|
{ replace }: { replace?: boolean } = {}
|
|
) => {
|
|
const { to, search, hash } = splitInternalUrl(url)
|
|
if (isAbsoluteUrl(to)) {
|
|
const href = to.startsWith('//') ? new URL(to, window.location.href).href : to
|
|
return navigate({ href, reloadDocument: true, replace })
|
|
}
|
|
return navigate<AnyRouter, string>({ to, search: search ?? {}, hash: hash ?? '', replace })
|
|
}
|
|
|
|
/**
|
|
* Creates a navigation handler that supports keyboard, modifier clicks, and middle mouse button.
|
|
*
|
|
* This is a curried function that takes a URL and navigate function, and returns an event handler that:
|
|
* - Handles keyboard navigation (Enter/Space keys)
|
|
* - Opens in new tab on Cmd/Ctrl + click
|
|
* - Opens in new tab on middle mouse button click
|
|
* - Performs normal navigation on regular click
|
|
*
|
|
* @param url - The relative URL to navigate to (e.g., "/project/123/functions/my-function")
|
|
* @param navigate - TanStack Router navigate function
|
|
* @returns Event handler function for onClick, onAuxClick, and onKeyDown
|
|
*
|
|
* @example
|
|
* ```tsx
|
|
* const navigate = useNavigate()
|
|
* const handleNavigation = createNavigationHandler(`/project/${ref}/functions/${slug}`, navigate)
|
|
*
|
|
* <TableRow
|
|
* onClick={handleNavigation}
|
|
* onAuxClick={handleNavigation}
|
|
* onKeyDown={handleNavigation}
|
|
* tabIndex={0}
|
|
* />
|
|
* ```
|
|
*/
|
|
export const createNavigationHandler = (url: string, navigate: UseNavigateResult<string>) => {
|
|
return (event: React.MouseEvent | React.KeyboardEvent) => {
|
|
// Handle keyboard events
|
|
if ('key' in event) {
|
|
if (event.key === 'Enter' || event.key === ' ') {
|
|
event.preventDefault()
|
|
|
|
const isModifierKey = event.metaKey || event.ctrlKey
|
|
if (isModifierKey) {
|
|
window.open(`${BASE_PATH}${url}`, '_blank')
|
|
} else {
|
|
void navigateToUrl(navigate, url)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// Handle Cmd/Ctrl + left click (modifier click)
|
|
const isModifierClick =
|
|
'button' in event && event.button === 0 && (event.metaKey || event.ctrlKey)
|
|
if (isModifierClick) {
|
|
event.preventDefault()
|
|
window.open(`${BASE_PATH}${url}`, '_blank')
|
|
return
|
|
}
|
|
|
|
// Handle middle mouse button click
|
|
const isMiddleClick = 'button' in event && event.button === MIDDLE_MOUSE_BUTTON
|
|
if (isMiddleClick) {
|
|
event.preventDefault()
|
|
window.open(`${BASE_PATH}${url}`, '_blank')
|
|
return
|
|
}
|
|
|
|
// Regular left click only. Other aux buttons (e.g. right-click) must not navigate.
|
|
if ('button' in event && event.button !== 0) return
|
|
|
|
void navigateToUrl(navigate, url)
|
|
}
|
|
}
|