Files
supabase/apps/studio/components/ui/QueryBlock/EditQueryButton.tsx
T
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

119 lines
3.7 KiB
TypeScript

import { EditIcon } from 'lucide-react'
import { ComponentProps } from 'react'
import {
cn,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
TooltipContent,
} from 'ui'
import { ButtonTooltip } from '../ButtonTooltip'
import { useIsInlineEditorEnabled } from '@/components/interfaces/Account/Preferences/useDashboardSettings'
import { useNewQuery } from '@/components/interfaces/SQLEditor/hooks'
import { DiffType } from '@/components/interfaces/SQLEditor/SQLEditor.types'
import { SIDEBAR_KEYS } from '@/components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider'
import { useRoutePattern } from '@/hooks/misc/useRoutePattern'
import { useTrack } from '@/lib/telemetry/track'
import { editorPanelState } from '@/state/editor-panel-state'
import { useSidebarManagerSnapshot } from '@/state/sidebar-manager-state'
import { useSqlEditorDiffRequestSnapshot } from '@/state/sql-editor/sql-editor-diff-request'
interface EditQueryButtonProps {
id?: string
title: string
sql?: string
className?: string
variant?: 'default' | 'text'
}
export const EditQueryButton = ({
id,
sql,
title,
className,
variant = 'text',
}: EditQueryButtonProps) => {
const routePattern = useRoutePattern()
const { newQuery } = useNewQuery()
const diffRequest = useSqlEditorDiffRequestSnapshot()
const { closeSidebar, openSidebar } = useSidebarManagerSnapshot()
const isInSQLEditor = routePattern.includes('/sql')
const isInNewSnippet = routePattern.endsWith('/sql')
const isInlineEditorEnabled = useIsInlineEditorEnabled()
const tooltip: { content: ComponentProps<typeof TooltipContent> & { text: string } } = {
content: { side: 'bottom', text: 'Edit in SQL Editor' },
}
const track = useTrack()
if (id !== undefined) {
return (
<ButtonTooltip
variant={variant}
size="tiny"
className={cn('w-7 h-7', className)}
icon={<EditIcon size={14} strokeWidth={1.5} />}
tooltip={tooltip}
onClick={() => {
editorPanelState.setActiveSnippetId(id)
openSidebar(SIDEBAR_KEYS.EDITOR_PANEL)
}}
/>
)
}
return !isInSQLEditor || isInNewSnippet ? (
<ButtonTooltip
variant={variant}
size="tiny"
className={cn('w-7 h-7', className)}
icon={<EditIcon size={14} strokeWidth={1.5} />}
onClick={() => {
if (isInlineEditorEnabled) {
// This component needs to be updated to work with local EditorPanel state
// For now, fall back to creating a new query
if (sql) newQuery(sql, title)
closeSidebar(SIDEBAR_KEYS.AI_ASSISTANT)
} else {
if (sql) newQuery(sql, title)
}
track('assistant_edit_in_sql_editor_clicked', {
isInSQLEditor,
isInNewSnippet,
})
}}
tooltip={tooltip}
/>
) : (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<ButtonTooltip
variant={variant}
size="tiny"
disabled={!sql}
className={cn('w-7 h-7', className)}
icon={<EditIcon size={14} strokeWidth={1.5} />}
tooltip={!!sql ? tooltip : { content: { side: 'bottom', text: undefined } }}
/>
</DropdownMenuTrigger>
{!!sql && (
<DropdownMenuContent className="w-36">
<DropdownMenuItem onClick={() => diffRequest.requestDiff(sql, DiffType.Addition)}>
Insert code
</DropdownMenuItem>
<DropdownMenuItem onClick={() => diffRequest.requestDiff(sql, DiffType.Modification)}>
Replace code
</DropdownMenuItem>
<DropdownMenuItem onClick={() => newQuery(sql, title)}>
Create new snippet
</DropdownMenuItem>
</DropdownMenuContent>
)}
</DropdownMenu>
)
}