mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
Adds PostHog telemetry events across the Table Editor sidebar, new-tab page, and column/table/row/cell side panels so we can measure schema usage, entity-list search/filtering, entity menu actions, and panel saves without logging schema, table, column, or search names. **Added:** - Event definitions in `packages/common/telemetry-constants.ts` (added to `TelemetryEvent`): - `table_editor_schema_selector_updated` (`schemaType: 'public' | 'protected' | 'custom'`) when switching schemas in `TableEditorMenu`. It uses the same `schemaType` property and derivation as the existing `database_schema_selector_updated` (now shared through `getDatabaseSchemaType`), so the two selectors can be compared in one insight - `table_editor_new_item_clicked` (`itemType: 'schema'` or `itemType: 'table'` with `trigger: 'sidebar' | 'new_tab_card'`) from the sidebar and `NewTab` card - `table_editor_table_search_submitted` (debounced 500ms, cancelled on clear, schema switch via selector or URL/tab change, and unmount) - `table_editor_entity_filter_updated` (`visibleTypeCount`) on entity-type checkbox toggle, "Select only", and filter reset - `table_editor_entity_action_clicked` (`action`, `entityType`) across dropdown and context menus in `useEntityActionHandlers` (excluding the `View policies` link, which is covered by pageviews) - `table_column_created` / `table_column_updated` (`hasForeignKey`, `isArray`) on column side-panel saves via `SidePanelEditor.utils.tsx`, with `hasForeignKey` scoped to the saved column and no-op saves skipped - `table_updated` (`columnCountChanged`) on existing table edits in `SidePanelEditor.tsx` once table, column, primary key, foreign key, Realtime, and Data API updates succeed and at least one change was persisted; no-op saves and saves where any step fails are skipped - `table_editor_row_updated` (`editMethod: 'side_panel'`) when an existing row edit from the row side panel is persisted. Saves from the expanded cell editors and the foreign row selector don't count, and neither do edits queued with queue operations enabled. The grid ticket (FE-5023) can add more `editMethod` values when it tracks inline edits - `table_editor_cell_editor_opened` (`editorType: 'json' | 'text'`) when expanding JSON or text cell editors from the grid cell editors (`JsonEditor` / `TextEditor`) or `RowEditor` - Column, table and row save events are attributed to the project the save ran against (third `track()` argument), and fire only after the save succeeds - Focused MSW-backed tests for non-obvious telemetry edge cases (search debounce + cancellation on clear and selector/external schema switches, schema switch single-fire + PII checks, column-scoped FK/array derivation and no-op gating in `createColumn`/`updateColumn`, change/error reporting in `updateTable` without double-firing column events) ## To test Watch `POST /platform/telemetry/event` in DevTools (and confirm arrival in staging PostHog): - Switch schemas (`public` -> `auth` -> custom schema -> `public`): fires `table_editor_schema_selector_updated` with `schemaType` `protected` -> `custom` -> `public` and no schema name; re-selecting the active schema fires nothing - On the Database > Schemas visualizer, switching schemas still fires `database_schema_selector_updated` with the same `schemaType` values as before - Click **Create a new schema** and **New table** in the sidebar, plus **Create a table** on `/project/[ref]/editor/new`: fires `table_editor_new_item_clicked` with the expected `itemType` and `trigger` - Type in **Search tables...** and pause >500ms: fires one `table_editor_table_search_submitted` with no query text; clearing or switching schemas (via the selector or clicking a tab in another schema) before 500ms fires nothing - Toggle entity-type checkboxes, click **Select only**, or reset filters: fires `table_editor_entity_filter_updated` with the new `visibleTypeCount` - Trigger actions from a table/view menu (both ⋮ and right-click): fires `table_editor_entity_action_clicked` with `action` and `entityType` (**View policies** does not fire it) - Create and edit a column via the column panel: fires `table_column_created` / `table_column_updated` with `hasForeignKey` (scoped to that column) and `isArray`; clicking **Save** with no changes fires nothing - Edit an existing table (with and without adding/removing a column, or toggling Realtime alone): fires `table_updated` with `columnCountChanged`, without firing `table_column_*`; clicking **Save** with no changes fires nothing - Edit an existing row in the row side panel: fires `table_editor_row_updated` with `editMethod: 'side_panel'`. Inserting a new row, saving from an expanded JSON/text cell editor, picking a foreign row, or saving with queue operations enabled does not fire it - Edit an existing table so that adding a column (or updating Realtime/Data API) fails: `table_updated` does not fire - Expand JSON and text cell editors from both the grid and inside the row panel: fires `table_editor_cell_editor_opened` with `editorType: 'json' | 'text'` Notes for review: - No `data-attr`s were added. Autocapture is off, so each tracked interaction gets an explicit `track()` call instead. - `table_editor_cell_editor_opened` counts the expand buttons in both the grid cell editors and the row side panel fields. It's the same click on both surfaces. - `table_editor_row_updated` only has `editMethod: 'side_panel'` for now. The grid toolbar ticket (FE-5023) can add values when it tracks inline edits. --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
159 lines
4.7 KiB
TypeScript
159 lines
4.7 KiB
TypeScript
import { toast } from 'sonner'
|
|
import { copyToClipboard } from 'ui'
|
|
|
|
import {
|
|
ENTITY_ACTION_TO_TELEMETRY,
|
|
ENTITY_TYPE_TO_TELEMETRY,
|
|
type EntityActionId,
|
|
} from './EntityActions.utils'
|
|
import { useExportAllRowsAsCsv, useExportAllRowsAsSql } from './ExportAllRows'
|
|
import { useTableActions } from '@/components/interfaces/TableGridEditor/useTableActions'
|
|
import { getTableDefinition } from '@/data/database/table-definition-query'
|
|
import { getViewDefinition } from '@/data/database/view-definition-query'
|
|
import { ENTITY_TYPE } from '@/data/entity-types/entity-type-constants'
|
|
import type { Entity } from '@/data/entity-types/entity-types-infinite-query'
|
|
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
|
|
import { formatSql } from '@/lib/formatSql'
|
|
import { useTrack } from '@/lib/telemetry/track'
|
|
|
|
type UseEntityActionHandlersParams = {
|
|
entity: Entity
|
|
projectRef: string
|
|
rowCount?: number
|
|
onExportCLI: (id: number) => void
|
|
onViewPartitions?: (id: number) => void
|
|
}
|
|
|
|
export const useEntityActionHandlers = ({
|
|
entity,
|
|
projectRef,
|
|
rowCount,
|
|
onExportCLI,
|
|
onViewPartitions,
|
|
}: UseEntityActionHandlersParams) => {
|
|
const track = useTrack()
|
|
const { data: project } = useSelectedProjectQuery()
|
|
const { editTable, duplicateTable, deleteTable, deleteView, deleteMaterializedView } =
|
|
useTableActions()
|
|
|
|
const { exportCsv, confirmationModal: exportCsvConfirmationModal } = useExportAllRowsAsCsv({
|
|
enabled: true,
|
|
projectRef,
|
|
connectionString: project?.connectionString ?? null,
|
|
entity,
|
|
type: 'fetch_all',
|
|
totalRows: rowCount,
|
|
})
|
|
|
|
const { exportSql, confirmationModal: exportSqlConfirmationModal } = useExportAllRowsAsSql({
|
|
enabled: true,
|
|
projectRef,
|
|
connectionString: project?.connectionString ?? null,
|
|
entity,
|
|
type: 'fetch_all',
|
|
totalRows: rowCount,
|
|
})
|
|
|
|
const copyDefinition = async ({
|
|
label,
|
|
getDefinition,
|
|
}: {
|
|
label: string
|
|
getDefinition: () => Promise<string | undefined | null>
|
|
}) => {
|
|
const capitalizedLabel = label[0].toUpperCase() + label.slice(1)
|
|
const toastId = toast.loading(`Getting ${label}...`)
|
|
|
|
const formattedDefinition = getDefinition().then((definition) => {
|
|
if (!definition) throw new Error(`Failed to get ${label}`)
|
|
return formatSql(definition)
|
|
})
|
|
|
|
try {
|
|
await copyToClipboard(formattedDefinition, () => {
|
|
toast.success(`${capitalizedLabel} copied to clipboard`, { id: toastId })
|
|
})
|
|
} catch (err: unknown) {
|
|
const message = err instanceof Error ? err.message : String(err)
|
|
toast.error(`Failed to copy ${label}: ${message}`, { id: toastId })
|
|
}
|
|
}
|
|
|
|
const onSelectAction = (actionId: EntityActionId) => {
|
|
const telemetryAction = ENTITY_ACTION_TO_TELEMETRY[actionId]
|
|
if (telemetryAction) {
|
|
track('table_editor_entity_action_clicked', {
|
|
action: telemetryAction,
|
|
entityType: ENTITY_TYPE_TO_TELEMETRY[entity.type],
|
|
})
|
|
}
|
|
|
|
switch (actionId) {
|
|
case 'copy-name':
|
|
copyToClipboard(entity.name)
|
|
break
|
|
case 'copy-schema':
|
|
copyDefinition({
|
|
label: 'table schema',
|
|
getDefinition: () =>
|
|
getTableDefinition({
|
|
id: entity.id,
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
}),
|
|
})
|
|
break
|
|
case 'copy-definition':
|
|
copyDefinition({
|
|
label:
|
|
entity.type === ENTITY_TYPE.MATERIALIZED_VIEW
|
|
? 'materialized view definition'
|
|
: 'view definition',
|
|
getDefinition: () =>
|
|
getViewDefinition({
|
|
id: entity.id,
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
includeCreateStatement: true,
|
|
}),
|
|
})
|
|
break
|
|
case 'edit-table':
|
|
editTable(entity.id)
|
|
break
|
|
case 'duplicate-table':
|
|
duplicateTable(entity.id)
|
|
break
|
|
case 'export-csv':
|
|
exportCsv()
|
|
break
|
|
case 'export-sql':
|
|
exportSql()
|
|
break
|
|
case 'export-cli':
|
|
onExportCLI(entity.id)
|
|
break
|
|
case 'view-partitions':
|
|
onViewPartitions?.(entity.id)
|
|
break
|
|
case 'delete':
|
|
if (entity.type === ENTITY_TYPE.VIEW) deleteView(entity.id)
|
|
else if (entity.type === ENTITY_TYPE.MATERIALIZED_VIEW) deleteMaterializedView(entity.id)
|
|
else deleteTable(entity.id)
|
|
break
|
|
// Navigation (`view-policies`) is handled by the item's href, and `export` only opens a submenu
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
const modals = (
|
|
<>
|
|
{exportCsvConfirmationModal}
|
|
{exportSqlConfirmationModal}
|
|
</>
|
|
)
|
|
|
|
return { onSelectAction, modals }
|
|
}
|