mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
Part 4 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 ← this PR** | **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 the database, branching, compute, functions, explorer, SQL editor, storage, and table editor interfaces off `next/router` and `next/compat/router`. **Changed:** - Migrated the sole `next/compat/router` consumer (`components/interfaces/Database/Replication/Destinations.tsx`) to `useNavigate` - Replaced `useRouter` across `BranchManagement`, `Branching`, `Compute`, `Database` (`Backups`, `Functions`, `Replication`, `Schemas`, `Tables`, `Triggers`), `EdgeFunctions`, `Explorer`, `Functions`, `SQLEditor`, `Storage` (`AnalyticsBuckets`, `FilesBuckets`, `VectorBuckets`, `StorageMenuV2`, `Storage.utils`), and `TableGridEditor` with TanStack Router hooks (`useNavigate`, `useLocation`, `useRoutePattern`) and `useParams` from `common` - Updated unit and component tests across `Database/Replication`, `Explorer`, `Functions`, and `Storage/FilesBuckets/DeleteBucketModal.test.tsx` to use TanStack Router **Simplified:** - `useSnippetEditor` and `useRedirectLegacyReadReplicaDestination` read search values from `useParams` instead of casting `location.search` - `resolveSourceSwitch` returns `replace: boolean` instead of a `push`/`replace` method name - `ExplorerQueryTab` and `EdgeFunctionDetails` guard on a defined ref instead of navigating with `ref ?? ''` - `useFunctionsDetailShortcuts` and `StorageMenuV2` use a local `goTo` helper for their navigation shortcuts - Tests use the shared `createTestRouter` and `customRender` router, use `toMatchObject` for search assertions, and drop duplicate assertions Addresses [FE-5109](https://linear.app/supabase/issue/FE-5109/replace-the-nextrouter-shims-with-tanstack-router-hooks) ## To test - [ ] SQL Editor: creating, renaming, moving, opening templates/examples, and keyboard shortcuts navigate between snippets cleanly - [ ] Table Editor & Database: opening tables, schema table nodes, triggers, functions, backups/PITR, and replication destination flows navigate as expected - [ ] Storage: switching between Files, Analytics, and Vector buckets and deleting a bucket navigates cleanly - [ ] Edge Functions & Explorer: opening function details/logs, explorer chat/query tabs, and branch management actions navigate properly --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
190 lines
7.1 KiB
TypeScript
190 lines
7.1 KiB
TypeScript
import { useParams } from 'common'
|
|
import Link from 'next/link'
|
|
import { parseAsString, useQueryState } from 'nuqs'
|
|
import { useCallback } from 'react'
|
|
import { Button } from 'ui'
|
|
import { Admonition } from 'ui-patterns/Admonition'
|
|
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
|
|
|
|
import { DeleteConfirmationDialogs } from './DeleteConfirmationDialogs'
|
|
import { SidePanelEditor } from './SidePanelEditor/SidePanelEditor'
|
|
import { TableDefinition } from './TableDefinition'
|
|
import { useCloseDeletedTableTab } from './useCloseDeletedTableTab'
|
|
import { SupabaseGrid } from '@/components/grid/SupabaseGrid'
|
|
import { useSyncTableEditorStateFromLocalStorageWithUrl } from '@/components/grid/SupabaseGrid.utils'
|
|
import {
|
|
Entity,
|
|
isForeignTable,
|
|
isMaterializedView,
|
|
isTableLike,
|
|
isView,
|
|
TableLike,
|
|
} from '@/data/table-editor/table-editor-types'
|
|
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
|
|
import { useDashboardHistory } from '@/hooks/misc/useDashboardHistory'
|
|
import { useQuerySchemaState } from '@/hooks/misc/useSchemaQueryState'
|
|
import { useIsProtectedSchema } from '@/hooks/useProtectedSchemas'
|
|
import { TableEditorTableStateContextProvider } from '@/state/table-editor-table'
|
|
import { useTabRouter, useTabsStateSnapshot } from '@/state/tabs'
|
|
|
|
export interface TableGridEditorProps {
|
|
isLoadingSelectedTable?: boolean
|
|
selectedTable?: Entity
|
|
}
|
|
|
|
export const TableGridEditor = ({
|
|
isLoadingSelectedTable = false,
|
|
selectedTable,
|
|
}: TableGridEditorProps) => {
|
|
const router = useTabRouter()
|
|
const { ref: projectRef, id } = useParams()
|
|
const { setLastVisitedTable } = useDashboardHistory()
|
|
const { selectedSchema } = useQuerySchemaState()
|
|
|
|
const tabs = useTabsStateSnapshot()
|
|
|
|
useSyncTableEditorStateFromLocalStorageWithUrl({
|
|
projectRef,
|
|
table: selectedTable,
|
|
})
|
|
|
|
const [selectedView] = useQueryState('view', parseAsString.withDefault('data'))
|
|
|
|
const { can: canEditTables } = useAsyncCheckPermissionsV2(FGA_PERMISSIONS.PROJECT.DATABASE_WRITE)
|
|
const { can: canEditColumns } = useAsyncCheckPermissionsV2(FGA_PERMISSIONS.PROJECT.DATABASE_WRITE)
|
|
const isReadOnly = !canEditTables && !canEditColumns
|
|
const tabId = !!id ? tabs.openTabs.find((x) => x.endsWith(id)) : undefined
|
|
const openTabs = tabs.openTabs.filter((x) => !x.startsWith('sql'))
|
|
|
|
const onTableCreated = useCallback(
|
|
(table: { id: number }) => {
|
|
router.push(
|
|
`/project/${projectRef}/editor/${table.id}${!!selectedSchema ? `?schema=${selectedSchema}` : ''}`
|
|
)
|
|
},
|
|
[projectRef, router, selectedSchema]
|
|
)
|
|
|
|
const { closeDeletedTableTab } = useCloseDeletedTableTab()
|
|
|
|
const { isSchemaLocked } = useIsProtectedSchema({ schema: selectedTable?.schema ?? '' })
|
|
|
|
// NOTE: DO NOT PUT HOOKS AFTER THIS LINE
|
|
if (isLoadingSelectedTable || !projectRef) {
|
|
return (
|
|
<>
|
|
<div className="flex flex-col">
|
|
<div className="h-10 bg-dash-sidebar dark:bg-surface-100" />
|
|
<div className="h-9 border-y" />
|
|
<div className="p-2 col-span-full">
|
|
<GenericSkeletonLoader />
|
|
</div>
|
|
</div>
|
|
<SidePanelEditor onTableCreated={onTableCreated} />
|
|
<DeleteConfirmationDialogs onTableDeleted={closeDeletedTableTab} />
|
|
</>
|
|
)
|
|
}
|
|
|
|
const isViewSelected = isView(selectedTable) || isMaterializedView(selectedTable)
|
|
const isTableSelected = isTableLike(selectedTable)
|
|
const isForeignTableSelected = isForeignTable(selectedTable)
|
|
|
|
const canEditViaTableEditor = isTableSelected && !isSchemaLocked
|
|
const editable = !isReadOnly && canEditViaTableEditor
|
|
|
|
const gridKey = !!selectedTable
|
|
? `${selectedTable.schema}_${selectedTable.name}`
|
|
: 'unknown-table'
|
|
|
|
/** [Joshen] We're going to need to refactor SupabaseGrid eventually to make the code here more readable
|
|
* For context we previously built the SupabaseGrid as a reusable npm component, but eventually decided
|
|
* to just integrate it directly into the dashboard. The header, and body (+footer) should be decoupled.
|
|
*/
|
|
|
|
return (
|
|
// When any click happens in a table tab, the tab becomes permanent
|
|
<div className="h-full" onClick={() => tabs.makeActiveTabPermanent()}>
|
|
{!selectedTable ? (
|
|
<div className="flex items-center justify-center h-full">
|
|
<div className="w-[400px]">
|
|
<Admonition
|
|
type="default"
|
|
title={`Unable to find your table with ID ${id}`}
|
|
description="This table doesn't exist in your database"
|
|
>
|
|
{!!tabId ? (
|
|
<Button
|
|
className="mt-2"
|
|
onClick={() => {
|
|
tabs.handleTabClose({
|
|
id: tabId,
|
|
router,
|
|
editor: 'table',
|
|
onClearDashboardHistory: () => setLastVisitedTable(undefined),
|
|
})
|
|
}}
|
|
>
|
|
Close tab
|
|
</Button>
|
|
) : openTabs.length > 0 ? (
|
|
<Button asChild className="mt-2" onClick={() => setLastVisitedTable(undefined)}>
|
|
<Link href={`/project/${projectRef}/editor/${openTabs[0].split('-')[1]}`}>
|
|
Close tab
|
|
</Link>
|
|
</Button>
|
|
) : (
|
|
<Button asChild className="mt-2" onClick={() => setLastVisitedTable(undefined)}>
|
|
<Link href={`/project/${projectRef}/editor`}>Head back</Link>
|
|
</Button>
|
|
)}
|
|
</Admonition>
|
|
</div>
|
|
</div>
|
|
) : (
|
|
<TableEditorTableStateContextProvider
|
|
// Table ids are per-database OIDs, so the same id can exist in another project.
|
|
key={`table-editor-table-${projectRef}-${selectedTable.id}`}
|
|
projectRef={projectRef}
|
|
table={selectedTable}
|
|
editable={editable}
|
|
>
|
|
<SupabaseGrid
|
|
key={gridKey}
|
|
gridProps={{ height: '100%' }}
|
|
customHeader={
|
|
(isViewSelected || isTableSelected) && selectedView === 'definition' ? (
|
|
<div className="px-2 flex items-center gap-x-2">
|
|
<p>
|
|
SQL Definition of <code className="text-sm">{selectedTable.name}</code>{' '}
|
|
</p>
|
|
<p className="text-foreground-light text-sm">(Read only)</p>
|
|
</div>
|
|
) : null
|
|
}
|
|
>
|
|
{(isViewSelected || isTableSelected) && selectedView === 'definition' && (
|
|
<TableDefinition entity={selectedTable} />
|
|
)}
|
|
</SupabaseGrid>
|
|
|
|
<DeleteConfirmationDialogs
|
|
selectedTable={selectedTable}
|
|
onTableDeleted={closeDeletedTableTab}
|
|
/>
|
|
</TableEditorTableStateContextProvider>
|
|
)}
|
|
|
|
{!selectedTable && <DeleteConfirmationDialogs onTableDeleted={closeDeletedTableTab} />}
|
|
|
|
<SidePanelEditor
|
|
editable={editable}
|
|
selectedTable={
|
|
isTableSelected || isForeignTableSelected ? (selectedTable as TableLike) : undefined
|
|
}
|
|
onTableCreated={onTableCreated}
|
|
/>
|
|
</div>
|
|
)
|
|
}
|