Files
supabase/apps/studio/components/layouts/TableEditorLayout/EntityActions.utils.ts
Joshen Lim df6eafef7c Joshenlim/fe 5116 support editduplicatedelete tables from (#53662)
## 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
2026-11-20 10:19:00 +07:00

89 lines
2.9 KiB
TypeScript

import { ENTITY_TYPE } from '@/data/entity-types/entity-type-constants'
import type { Entity } from '@/data/entity-types/entity-types-infinite-query'
export type EntityActionId =
| 'copy-name'
| 'copy-schema'
| 'copy-definition'
| 'edit-table'
| 'duplicate-table'
| 'view-policies'
| 'export'
| 'export-csv'
| 'export-sql'
| 'export-cli'
| 'view-partitions'
| 'delete'
export type EntityAction = {
id: EntityActionId
label: string
href?: string
children?: EntityAction[]
}
export type EntityActionGroup = EntityAction[]
export function isTableLikeEntity(entity: { type?: string }) {
return entity.type === ENTITY_TYPE.TABLE || entity.type === ENTITY_TYPE.PARTITIONED_TABLE
}
type GetEntityActionsParams = {
entity: Pick<Entity, 'id' | 'schema' | 'type' | 'is_partition'>
projectRef: string
isLocked: boolean
}
/**
* Every action works off the entity itself, including edit, duplicate and delete, which act on the
* table named in the table editor state. The same list is available from any row.
*/
export const getEntityActions = ({
entity,
projectRef,
isLocked,
}: GetEntityActionsParams): EntityActionGroup[] => {
const isTableLike = isTableLikeEntity(entity)
const isViewLike =
entity.type === ENTITY_TYPE.VIEW || entity.type === ENTITY_TYPE.MATERIALIZED_VIEW
const noun = isViewLike ? 'view' : 'table'
const copyGroup: EntityActionGroup = [{ id: 'copy-name', label: 'Copy name' }]
if (isTableLike) copyGroup.push({ id: 'copy-schema', label: 'Copy table schema' })
if (isViewLike) copyGroup.push({ id: 'copy-definition', label: 'Copy definition' })
const manageGroup: EntityActionGroup = []
if (isTableLike) {
const canEditTable = !isLocked && entity.type === ENTITY_TYPE.TABLE && !entity.is_partition
if (canEditTable) {
manageGroup.push(
{ id: 'edit-table', label: 'Edit table' },
{ id: 'duplicate-table', label: 'Duplicate table' }
)
}
manageGroup.push({
id: 'view-policies',
label: 'View policies',
href: `/project/${projectRef}/database/policies?schema=${encodeURIComponent(entity.schema)}&search=${encodeURIComponent(String(entity.id))}`,
})
}
if (isTableLike || isViewLike) {
const exportActions: EntityAction[] = [
{ id: 'export-csv', label: `Export ${noun} as CSV` },
{ id: 'export-sql', label: `Export ${noun} as SQL` },
]
if (isTableLike) exportActions.push({ id: 'export-cli', label: 'Export table via CLI' })
manageGroup.push({ id: 'export', label: 'Export data', children: exportActions })
}
const partitionsGroup: EntityActionGroup =
entity.type === ENTITY_TYPE.PARTITIONED_TABLE
? [{ id: 'view-partitions', label: 'View partitions' }]
: []
const deleteGroup: EntityActionGroup =
!isLocked && (isTableLike || isViewLike) ? [{ id: 'delete', label: `Delete ${noun}` }] : []
return [copyGroup, manageGroup, partitionsGroup, deleteGroup].filter((group) => group.length > 0)
}