mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
## Context We previously introduced context menus to the table editor [here](https://github.com/supabase/supabase/pull/53506), but prevented edit, delete, and duplicate actions as those actions were coupled to the router. Hence this PR decouples that by making those actions target a table by a provided ID "More options" action now also shows on every row, just visibly shown only if its active or the user hovers over the row <img width="335" height="183" alt="image" src="https://github.com/user-attachments/assets/9897945d-37f4-4658-9420-29b948173208" /> ## To test Can test edit / delete / duplicate table on a table that you're not currently actively on, via both context menu and dropdown menu
87 lines
3.4 KiB
TypeScript
87 lines
3.4 KiB
TypeScript
import { createFileRoute, useNavigate } from '@tanstack/react-router'
|
|
import { useEffect } from 'react'
|
|
|
|
import { buildTableEditorUrl } from '@/components/grid/SupabaseGrid.utils'
|
|
import { DeleteConfirmationDialogs } from '@/components/interfaces/TableGridEditor/DeleteConfirmationDialogs'
|
|
import { SidePanelEditor } from '@/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor'
|
|
import { useCloseDeletedTableTab } from '@/components/interfaces/TableGridEditor/useCloseDeletedTableTab'
|
|
import { NewTab } from '@/components/layouts/Tabs/NewTab'
|
|
import { useDashboardHistory } from '@/hooks/misc/useDashboardHistory'
|
|
import { useQuerySchemaState } from '@/hooks/misc/useSchemaQueryState'
|
|
import { splitInternalUrl } from '@/lib/internal-url'
|
|
import { editorEntityTypes, useTabsStateSnapshot } from '@/state/tabs'
|
|
|
|
export const Route = createFileRoute('/project/$ref/editor/')({
|
|
component: TableEditorPage,
|
|
})
|
|
|
|
function TableEditorPage() {
|
|
const navigate = useNavigate()
|
|
const { closeDeletedTableTab } = useCloseDeletedTableTab()
|
|
const { ref: projectRef } = Route.useParams()
|
|
const tabStore = useTabsStateSnapshot()
|
|
const { selectedSchema } = useQuerySchemaState()
|
|
const { history, isHistoryLoaded } = useDashboardHistory()
|
|
|
|
const onTableCreated = (table: { id: number }) => {
|
|
void navigate({
|
|
to: '/project/$ref/editor/$id',
|
|
params: { ref: projectRef, id: String(table.id) },
|
|
search: !!selectedSchema ? { schema: selectedSchema } : undefined,
|
|
})
|
|
}
|
|
|
|
// buildTableEditorUrl returns an absolute URL (origin + basePath); split it so the
|
|
// router navigates client-side instead of treating it as a document navigation
|
|
const navigateToTable = (url: string) => {
|
|
const { to, search } = splitInternalUrl(url)
|
|
void navigate({ to, search })
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (isHistoryLoaded && projectRef) {
|
|
const lastOpenedTableId = Number(history.editor)
|
|
const lastTableTabId = tabStore.openTabs.find((id) =>
|
|
editorEntityTypes.table.includes(tabStore.tabsMap[id]?.type)
|
|
)
|
|
const lastTableTab = lastTableTabId ? tabStore.tabsMap[lastTableTabId] : undefined
|
|
const lastTabId = lastTableTab?.metadata?.tableId
|
|
|
|
// Handle redirect to last opened table tab, or last table tab
|
|
if (Number.isInteger(lastOpenedTableId)) {
|
|
// Tabs are keyed by a prefixed id (e.g. "r-293522"), but history only stores
|
|
// the bare table id, so look up the tab by its metadata instead of by key.
|
|
const lastOpenedTableData = Object.values(tabStore.tabsMap).find(
|
|
(tab) =>
|
|
editorEntityTypes.table.includes(tab.type) &&
|
|
tab.metadata?.tableId === lastOpenedTableId
|
|
)
|
|
navigateToTable(
|
|
buildTableEditorUrl({
|
|
projectRef,
|
|
tableId: lastOpenedTableId,
|
|
schema: lastOpenedTableData?.metadata?.schema,
|
|
})
|
|
)
|
|
} else if (lastTabId !== undefined) {
|
|
navigateToTable(
|
|
buildTableEditorUrl({
|
|
projectRef,
|
|
tableId: lastTabId,
|
|
schema: lastTableTab?.metadata?.schema,
|
|
})
|
|
)
|
|
}
|
|
}
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [isHistoryLoaded, projectRef])
|
|
|
|
return (
|
|
<>
|
|
<NewTab />
|
|
<SidePanelEditor onTableCreated={onTableCreated} />
|
|
<DeleteConfirmationDialogs onTableDeleted={closeDeletedTableTab} />
|
|
</>
|
|
)
|
|
}
|