mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +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>
195 lines
5.7 KiB
TypeScript
195 lines
5.7 KiB
TypeScript
import { Link, useLocation } from '@tanstack/react-router'
|
|
import { IS_PLATFORM, useParams } from 'common'
|
|
import saveAs from 'file-saver'
|
|
import { ChevronDownIcon, CopyIcon, DownloadIcon, SettingsIcon } from 'lucide-react'
|
|
import { useMemo } from 'react'
|
|
import { toast } from 'sonner'
|
|
import {
|
|
Button,
|
|
copyToClipboard,
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuItem,
|
|
DropdownMenuTrigger,
|
|
KeyboardShortcut,
|
|
} from 'ui'
|
|
|
|
import { ButtonTooltip } from './ButtonTooltip'
|
|
import {
|
|
convertResultsToCSV,
|
|
convertResultsToJSON,
|
|
convertResultsToMarkdown,
|
|
} from '@/components/interfaces/SQLEditor/UtilityPanel/Results.utils'
|
|
import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
|
|
import { useShortcut } from '@/state/shortcuts/useShortcut'
|
|
|
|
interface DownloadResultsButtonProps {
|
|
iconOnly?: boolean
|
|
variant?: 'text' | 'default'
|
|
text?: string
|
|
align?: 'start' | 'center' | 'end'
|
|
results: readonly any[]
|
|
fileName: string
|
|
enableCopyShortcuts?: boolean
|
|
onDownloadAsCSV?: () => void
|
|
onCopyAsMarkdown?: () => void
|
|
onCopyAsJSON?: () => void
|
|
onCopyAsCSV?: () => void
|
|
}
|
|
|
|
export const DownloadResultsButton = ({
|
|
iconOnly = false,
|
|
variant = 'default',
|
|
text = 'Export',
|
|
align = 'start',
|
|
results,
|
|
fileName,
|
|
enableCopyShortcuts = true,
|
|
onDownloadAsCSV,
|
|
onCopyAsMarkdown,
|
|
onCopyAsJSON,
|
|
onCopyAsCSV,
|
|
}: DownloadResultsButtonProps) => {
|
|
const { ref } = useParams()
|
|
const pathname = useLocation({ select: (location) => location.pathname })
|
|
const isLogs = pathname.includes('/logs')
|
|
const isEmpty = useMemo(() => results.length === 0, [results])
|
|
|
|
const downloadAsCSV = () => {
|
|
const csv = convertResultsToCSV(results)
|
|
if (!csv) {
|
|
toast('Results are empty')
|
|
return
|
|
}
|
|
|
|
const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' })
|
|
saveAs(blob, `${fileName}.csv`)
|
|
toast.success('Downloading results as CSV')
|
|
onDownloadAsCSV?.()
|
|
}
|
|
|
|
const copyAsMarkdown = () => {
|
|
const markdownData = convertResultsToMarkdown(results)
|
|
if (!markdownData) {
|
|
toast('Results are empty')
|
|
return
|
|
}
|
|
copyToClipboard(markdownData, () => {
|
|
toast.success('Copied Markdown to clipboard')
|
|
onCopyAsMarkdown?.()
|
|
})
|
|
}
|
|
|
|
const copyAsJSON = () => {
|
|
const jsonData = convertResultsToJSON(results)
|
|
if (!jsonData) {
|
|
toast('Results are empty')
|
|
return
|
|
}
|
|
copyToClipboard(jsonData, () => {
|
|
toast.success('Copied JSON to clipboard')
|
|
onCopyAsJSON?.()
|
|
})
|
|
}
|
|
|
|
const copyAsCSV = () => {
|
|
const csv = convertResultsToCSV(results)
|
|
if (!csv) {
|
|
toast('Results are empty')
|
|
return
|
|
}
|
|
copyToClipboard(csv, () => {
|
|
toast.success('Copied CSV to clipboard')
|
|
onCopyAsCSV?.()
|
|
})
|
|
}
|
|
|
|
useShortcut(SHORTCUT_IDS.RESULTS_COPY_MARKDOWN, copyAsMarkdown, {
|
|
enabled: !isEmpty && enableCopyShortcuts,
|
|
conflictBehavior: 'allow',
|
|
registerInCommandMenu: true,
|
|
})
|
|
useShortcut(SHORTCUT_IDS.RESULTS_COPY_JSON, copyAsJSON, {
|
|
enabled: !isEmpty && enableCopyShortcuts,
|
|
conflictBehavior: 'allow',
|
|
registerInCommandMenu: true,
|
|
})
|
|
useShortcut(SHORTCUT_IDS.RESULTS_COPY_CSV, copyAsCSV, {
|
|
enabled: !isEmpty && enableCopyShortcuts,
|
|
conflictBehavior: 'allow',
|
|
registerInCommandMenu: true,
|
|
})
|
|
useShortcut(SHORTCUT_IDS.RESULTS_DOWNLOAD_CSV, downloadAsCSV, {
|
|
enabled: !isEmpty,
|
|
registerInCommandMenu: true,
|
|
})
|
|
|
|
return (
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
{iconOnly ? (
|
|
<ButtonTooltip
|
|
variant={variant}
|
|
aria-label="Download results"
|
|
aria-describedby={undefined}
|
|
icon={<DownloadIcon />}
|
|
disabled={results.length === 0}
|
|
className="w-7"
|
|
tooltip={{ content: { side: 'bottom', text: 'Download results' } }}
|
|
/>
|
|
) : (
|
|
<Button variant={variant} iconRight={<ChevronDownIcon />} disabled={results.length === 0}>
|
|
{text}
|
|
</Button>
|
|
)}
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align={align} className={enableCopyShortcuts ? 'w-60' : 'w-44'}>
|
|
{isLogs && IS_PLATFORM && (
|
|
<DropdownMenuItem asChild className="gap-x-2">
|
|
<Link to="/project/$ref/settings/log-drains" params={{ ref: ref ?? '_' }}>
|
|
<SettingsIcon size={14} />
|
|
<p>Add a Log Drain</p>
|
|
</Link>
|
|
</DropdownMenuItem>
|
|
)}
|
|
<DropdownMenuItem onClick={copyAsMarkdown} className="gap-x-2">
|
|
<CopyIcon size={14} />
|
|
<p>Copy as Markdown</p>
|
|
{enableCopyShortcuts && (
|
|
<span className="ml-auto">
|
|
<KeyboardShortcut keys={['Shift', 'Meta', 'm']} />
|
|
</span>
|
|
)}
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem onClick={copyAsJSON} className="gap-x-2">
|
|
<CopyIcon size={14} />
|
|
<p>Copy as JSON</p>
|
|
{enableCopyShortcuts && (
|
|
<span className="ml-auto">
|
|
<KeyboardShortcut keys={['Shift', 'Meta', 'j']} />
|
|
</span>
|
|
)}
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem onClick={copyAsCSV} className="gap-x-2">
|
|
<CopyIcon size={14} />
|
|
<p>Copy as CSV</p>
|
|
{enableCopyShortcuts && (
|
|
<span className="ml-auto">
|
|
<KeyboardShortcut keys={['Shift', 'Meta', 'c']} />
|
|
</span>
|
|
)}
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem className="gap-x-2" onClick={() => downloadAsCSV()}>
|
|
<DownloadIcon size={14} />
|
|
<p>Download CSV</p>
|
|
{enableCopyShortcuts && (
|
|
<span className="ml-auto">
|
|
<KeyboardShortcut keys={['Shift', 'Meta', 'd']} />
|
|
</span>
|
|
)}
|
|
</DropdownMenuItem>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
)
|
|
}
|