import type { TableEditorEntityAction, TableEditorEntityType } from 'common/telemetry-constants' 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 const ENTITY_TYPE_TO_TELEMETRY: Record = { [ENTITY_TYPE.TABLE]: 'table', [ENTITY_TYPE.VIEW]: 'view', [ENTITY_TYPE.MATERIALIZED_VIEW]: 'materialized_view', [ENTITY_TYPE.FOREIGN_TABLE]: 'foreign_table', [ENTITY_TYPE.PARTITIONED_TABLE]: 'partitioned_table', } // `view-policies` (tracked via pageview) and `export` (submenu trigger) are intentionally omitted export const ENTITY_ACTION_TO_TELEMETRY: Partial> = { 'copy-name': 'copy_name', 'copy-schema': 'copy_schema', 'copy-definition': 'copy_definition', 'edit-table': 'edit', 'duplicate-table': 'duplicate', 'export-csv': 'export_csv', 'export-sql': 'export_sql', 'export-cli': 'export_cli', 'view-partitions': 'view_partitions', delete: '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 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) }