Files
Alaister YoungandAlaister Young c59821fde6 [FE-5109] (part 2) refactor(studio): migrate shared UI components off next/router (#54054)
Part 2 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 ← this PR** | **`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 | Remove `next/router` & `next/compat/router` shims,
`_router-events.ts`, Vite aliases, and test mocks |

Migrates all 15 `apps/studio/components/ui/**` files off `next/router`.

**Changed:**

- Replaced `useRouter` across `AIAssistantPanel` (`AIAssistant`,
`ModelSelector`), `Charts/ComposedChartHandler`, `EditorPanel`,
`ErrorBoundary` (`ClientSideExceptionHandler`,
`GlobalErrorBoundaryState`, `InsertBeforeRemoveChildErrorHandler`),
`GlobalShortcuts/ShortcutsReferenceSheet`, `HelpPanel`
(`HelpOptionsList`, `HelpPanel`), `ProductMenu/ProductMenuShortcuts`,
`QueryBlock/EditQueryButton`, and `ResourceExhaustionWarningBanner` with
TanStack's `useNavigate`, `useLocation`, `useRoutePattern`, and
`useParams` from `common`
- Updated `AIAssistant.test.tsx`, `HelpOptionsList.test.tsx`, and
`ProductMenuShortcuts.test.tsx` to use TanStack Router instead of
`next/router` mocks

**Simplified:**

- `EditQueryButton` and `ShortcutsReferenceSheet` match against
`useRoutePattern()` instead of the real pathname, as `router.pathname`
did before. A ref or slug containing `sql` or `advisors` no longer
misfires.
- Removed the unused `restrictToRoutes` field from
`ResourceExhaustionWarningBanner` (no warning sets it)
- `AIAssistant` clears the handoff param with `navigate({ to: '.' })`,
dropping a pathname subscription and an `AnyRouter` cast
- `ProductMenuShortcuts` inlines its handler (`useShortcut` always calls
the latest callback)
- Moved the `AdvisorsNavigationShortcuts.test.tsx` fix into this PR so
it passes on its own
- Tests use the shared `createTestRouter` and `customRender` router

Addresses
[FE-5109](https://linear.app/supabase/issue/FE-5109/replace-the-nextrouter-shims-with-tanstack-router-hooks)

## To test

- [ ] AI Assistant panel links, model selector navigation, and "Edit in
SQL Editor" (`EditQueryButton`) navigate cleanly
- [ ] Keyboard shortcuts in `ProductMenuShortcuts` and the shortcuts
reference sheet (`?`) match the active section and navigate as expected
- [ ] Error boundary recovery ("Reload page" / navigation actions) and
Help panel links still work
- [ ] Resource exhaustion warning banner matches project routes as
before

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-11-30 14:49:15 +08:00

134 lines
5.1 KiB
TypeScript

import { IS_PLATFORM } from 'common'
import { ChevronLeftIcon, XIcon } from 'lucide-react'
import { useEffect, useState } from 'react'
import SVG from 'react-inlinesvg'
import { Button } from 'ui'
import { useSnapshot } from 'valtio'
import { ASSISTANT_SUGGESTIONS } from './HelpPanel.constants'
import { HelpSection } from './HelpSection'
import type { SupportFormUrlKeys } from '@/components/interfaces/Support/SupportForm.utils'
import {
SupportForm,
SupportFormStatusButton,
} from '@/components/interfaces/Support/SupportSidebarForm'
import { SIDEBAR_KEYS } from '@/components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { BASE_PATH } from '@/lib/constants'
import { useAiAssistantStateSnapshot } from '@/state/ai-assistant-state'
import { helpPanelState } from '@/state/help-panel-state'
import { useSidebarManagerSnapshot } from '@/state/sidebar-manager-state'
export const HelpPanel = ({
onClose,
projectRef,
supportLinkQueryParams,
}: {
onClose: () => void
projectRef: string | undefined
supportLinkQueryParams: Partial<SupportFormUrlKeys> | undefined
}) => {
const snap = useAiAssistantStateSnapshot()
const { openSidebar, closeSidebar } = useSidebarManagerSnapshot()
const helpSnap = useSnapshot(helpPanelState)
const [view, setView] = useState<'home' | 'support'>(() => helpPanelState.requestedView)
useEffect(() => {
if (helpSnap.requestedView !== 'home') {
setView(helpSnap.requestedView)
helpPanelState.requestedView = 'home'
}
}, [helpSnap.requestedView])
const isSupportView = view === 'support'
const openAssistant = () => {
onClose()
openSidebar(SIDEBAR_KEYS.AI_ASSISTANT)
snap.newChat(ASSISTANT_SUGGESTIONS)
}
return (
<div className="flex h-full flex-col overflow-hidden">
<div className="flex h-(--header-height) items-center justify-between gap-2 border-b pl-4 pr-3">
<div className="flex min-w-0 items-center gap-1.5 text-xs">
{isSupportView && (
<ButtonTooltip
variant="text"
className="h-7 w-7"
onClick={() => setView('home')}
icon={<ChevronLeftIcon strokeWidth={1.5} />}
tooltip={{ content: { side: 'bottom', text: 'Back' } }}
/>
)}
<span className="truncate">{isSupportView ? 'Contact support' : 'Help & Support'}</span>
</div>
<div className="flex items-center gap-2">
<SupportFormStatusButton />
<ButtonTooltip
variant="text"
className="w-7 h-7"
onClick={() => closeSidebar(SIDEBAR_KEYS.HELP_PANEL)}
icon={<XIcon strokeWidth={1.5} />}
tooltip={{ content: { side: 'bottom', text: 'Close' } }}
/>
</div>
</div>
<div className="flex-1 overflow-hidden">
{isSupportView ? (
<SupportForm initialParams={supportLinkQueryParams} />
) : (
<div className="flex h-full flex-col overflow-y-auto pb-5">
<HelpSection
excludeIds={['discord']}
isPlatform={IS_PLATFORM}
projectRef={projectRef}
supportLinkQueryParams={supportLinkQueryParams}
onAssistantClick={openAssistant}
onSupportClick={() => {
setView('support')
return false
}}
/>
<div className="flex flex-col gap-4 border-t pt-5">
<div className="px-5 flex flex-col gap-0.5">
<h5 className="text-foreground">Community support</h5>
<p className="text-xs text-foreground-lighter text-balance">
Our Discord community can help with code-related issues. Many questions are
answered in minutes.
</p>
</div>
<div className="px-5">
<div
className="relative space-y-2 overflow-hidden rounded-sm px-4 py-4 pb-12 shadow-md"
style={{ background: '#404EED' }}
>
<a
href="https://discord.supabase.com"
target="_blank"
rel="noreferrer"
className="group dark block cursor-pointer"
>
<img
className="absolute inset-0 h-full w-full object-cover opacity-50 transition-opacity group-hover:opacity-40"
src={`${BASE_PATH}/img/support/discord-bg-small.jpg`}
alt="Discord illustration"
/>
<Button
variant="secondary"
size="tiny"
className="bg-white hover:bg-white/90"
icon={<SVG src={`${BASE_PATH}/img/discord-icon.svg`} className="h-4 w-4" />}
>
<span style={{ color: '#404EED' }}>Join us on Discord</span>
</Button>
</a>
</div>
</div>
</div>
</div>
)}
</div>
</div>
)
}