mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Problem The schema visualizer (`/project/[ref]/database/schemas`) sends no telemetry. We can't tell whether people use it to explore a schema or to export it, which export format they pick, or whether they edit tables from the graph. ## Solution Adds PostHog events for the visualizer's main actions: changing the schema, selecting a Find table result, copying (schema or table as SQL/Markdown, table or column name), downloading the graph as PNG or SVG, applying Auto layout, and choosing Edit table or Edit column from a node menu. Each event fires from the action's handler, so actions started from a keyboard shortcut count too. Properties are only fixed enums (`schemaType`, `content`, `format`, `action`); no schema, table, or column names are sent. Each tracked element also gets a `data-attr` for autocapture. View in Table Editor and the empty state's New table link get a `data-attr` only, because the pageview already records them. It also fixes an empty canvas after switching back to a schema that was already loaded. The graph stays mounted in that case and fitted the view only on first load, so the new schema's tables could sit off-screen until a reload. The view now fits whenever the schema on the canvas changes. Edits within the same schema still keep the current view. Studio's database pages keep the selected schema name in the `schema` URL param, and that name is chosen by the customer. Telemetry now redacts its value to `redacted` wherever it already redacts `keywords`: the event and pageview `page_url`, the query string, the referrer, first-touch data, and the URL properties posthog-js adds. This applies to every Studio event and pageview, not just this page. Params that only contain the word, such as `schemas` or `myschema`, are left alone. ## How to verify On the PR preview, open a project's Database > Schema Visualizer with PostHog debugging on (or watch the network requests to the telemetry endpoint), then: 1. Change the schema in the selector: one `database_schema_selector_updated` event with `schemaType` of `public`, `protected` (for example `auth`), or `custom`. 2. Open Find table and select a table: one `database_schema_visualizer_find_table_clicked` event. 3. Click Copy as SQL: one `database_schema_visualizer_copied` with `content: 'schema_sql'`. Copy as Markdown from the export menu sends `content: 'schema_markdown'`. 4. Download as PNG and as SVG: one `database_schema_visualizer_image_download_clicked` each, with `format` of `png` or `svg`. 5. Open Auto layout and click Apply: one `database_schema_visualizer_auto_layout_applied`. Cancel sends nothing. 6. From a table node menu, choose Copy name, Copy as SQL, and Copy as Markdown: `database_schema_visualizer_copied` with `content` of `table_name`, `table_sql`, and `table_markdown`. Column menu Copy name sends `column_name`. 7. From a table node menu choose Edit table, and from a column menu choose Edit column: `database_schema_visualizer_node_menu_clicked` with `action` of `edit_table` and `edit_column`. 8. No event carries a schema, table, or column name. 9. Switch the schema from public to auth, then back to public, with no reload: the public tables are shown and fitted on the canvas each time. Repeat auth → public → auth. 10. Edit a column from the graph and save: the view does not jump. 11. In the telemetry requests from steps 1 to 7 and in the pageview, `page_url` and `ph.search` show `schema=redacted`, never the schema name. The page itself still shows and switches the real schema. ## Checks Ran `pnpm turbo run typecheck --filter='...[origin/master]'`. Every package passed except studio, which ran out of memory beside a parallel lint run. `pnpm --filter studio run typecheck` alone then passed, and passed again after the view fix. `pnpm turbo run lint --filter='[origin/master]'` ran out of memory twice on studio's `lint:type-checks` (2 GB default heap). So I ran `eslint` and `eslint --config eslint.type-checks.config.cjs` on the changed Studio files with a larger heap: no errors, only pre-existing warnings. `vitest run components/interfaces/Database/Schemas/Schemas.utils.test.ts` passed. For the redaction, `vitest run` in `packages/common` (all 310 tests, including the new `schema` cases) and its `typecheck` passed; `common` has no lint script. `oxfmt --check` passes on all changed files. Event names and properties follow the repo's `telemetry-standards` skill. ## Checklist - [x] I have read the [contributing guidelines](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [x] The change is scoped to one feature - [x] Events are defined in `packages/common/telemetry-constants.ts` and added to the `TelemetryEvent` union ## Feedback for Tedd To ask Tedd for a change to this PR, reply in Tedd's session on [FE-5031](https://linear.app/supabase/issue/FE-5031/track-usage-on-the-schema-visualizer). Tedd doesn't read comments on this PR. --------- Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com>
144 lines
4.6 KiB
TypeScript
144 lines
4.6 KiB
TypeScript
import { Loader2Icon, SearchIcon } from 'lucide-react'
|
|
import { ComponentPropsWithoutRef, forwardRef, useMemo, useState } from 'react'
|
|
import {
|
|
Button,
|
|
Command,
|
|
CommandEmpty,
|
|
CommandGroup,
|
|
CommandInput,
|
|
CommandItem,
|
|
CommandList,
|
|
Popover,
|
|
PopoverContent,
|
|
PopoverTrigger,
|
|
ScrollArea,
|
|
} from 'ui'
|
|
|
|
import { useInfiniteTablesQuery } from '@/data/tables/tables-query'
|
|
import { useDebouncedValue } from '@/hooks/misc/useDebouncedValue'
|
|
import type { SafePostgresTable } from '@/lib/postgres-types'
|
|
|
|
type FindTableSelectorProps = Omit<ComponentPropsWithoutRef<'div'>, 'onSelect'> & {
|
|
projectRef?: string
|
|
connectionString?: string | null
|
|
schema?: string
|
|
disabled?: boolean
|
|
size?: 'tiny' | 'small'
|
|
open: boolean
|
|
onOpenChange: (open: boolean) => void
|
|
onSelect: (table: SafePostgresTable) => void
|
|
}
|
|
|
|
export const FindTableSelector = forwardRef<HTMLDivElement, FindTableSelectorProps>(
|
|
(
|
|
{
|
|
className,
|
|
projectRef,
|
|
connectionString,
|
|
schema,
|
|
disabled = false,
|
|
size = 'tiny',
|
|
open,
|
|
onOpenChange,
|
|
onSelect,
|
|
...rest
|
|
},
|
|
ref
|
|
) => {
|
|
const [search, setSearch] = useState('')
|
|
const debouncedSearch = useDebouncedValue(search, 300)
|
|
const nameFilter = debouncedSearch.trim() || undefined
|
|
|
|
const { data, isFetching, hasNextPage, isFetchingNextPage, fetchNextPage } =
|
|
useInfiniteTablesQuery(
|
|
{
|
|
projectRef,
|
|
connectionString,
|
|
schema,
|
|
includeColumns: false,
|
|
pageSize: 50,
|
|
nameFilter,
|
|
},
|
|
{ enabled: open }
|
|
)
|
|
|
|
const tables = useMemo(() => data?.pages.flat() ?? [], [data])
|
|
|
|
const handleOpenChange = (next: boolean) => {
|
|
if (!next) setSearch('')
|
|
onOpenChange(next)
|
|
}
|
|
|
|
return (
|
|
<div ref={ref} className={className} {...rest}>
|
|
<Popover open={open} onOpenChange={handleOpenChange} modal={false}>
|
|
<PopoverTrigger asChild>
|
|
<Button
|
|
size={size}
|
|
disabled={disabled}
|
|
data-testid="find-table-selector"
|
|
icon={<SearchIcon size={14} strokeWidth={1.5} className="text-foreground-muted" />}
|
|
>
|
|
<span className="text-foreground-lighter">Find table…</span>
|
|
</Button>
|
|
</PopoverTrigger>
|
|
<PopoverContent className="p-0 w-[260px] pointer-events-auto" side="bottom" align="start">
|
|
<Command shouldFilter={false}>
|
|
<CommandInput
|
|
className="text-xs"
|
|
placeholder="Find table…"
|
|
value={search}
|
|
onValueChange={setSearch}
|
|
/>
|
|
<CommandList>
|
|
{isFetching && tables.length === 0 ? (
|
|
<div className="flex items-center justify-center gap-x-2 py-6 text-xs text-foreground-light">
|
|
<Loader2Icon className="animate-spin" size={14} />
|
|
<span>Loading tables</span>
|
|
</div>
|
|
) : (
|
|
<>
|
|
<CommandEmpty>No tables found</CommandEmpty>
|
|
<CommandGroup>
|
|
<ScrollArea className={tables.length > 7 ? 'h-[210px]' : ''}>
|
|
{tables.map((table) => (
|
|
<CommandItem
|
|
key={table.id}
|
|
value={String(table.id)}
|
|
data-attr="schema-visualizer-find-table-item"
|
|
className="cursor-pointer flex items-center justify-between space-x-2 w-full"
|
|
onSelect={() => {
|
|
onSelect(table)
|
|
handleOpenChange(false)
|
|
}}
|
|
>
|
|
<span>{table.name}</span>
|
|
</CommandItem>
|
|
))}
|
|
{hasNextPage && (
|
|
<div className="px-2 py-1.5">
|
|
<Button
|
|
block
|
|
size="tiny"
|
|
loading={isFetchingNextPage}
|
|
onClick={() => fetchNextPage()}
|
|
>
|
|
Load more
|
|
</Button>
|
|
</div>
|
|
)}
|
|
</ScrollArea>
|
|
</CommandGroup>
|
|
</>
|
|
)}
|
|
</CommandList>
|
|
</Command>
|
|
</PopoverContent>
|
|
</Popover>
|
|
</div>
|
|
)
|
|
}
|
|
)
|
|
|
|
FindTableSelector.displayName = 'FindTableSelector'
|