Files
supabase/apps/studio/AGENTS.md

7.2 KiB
Raw Permalink Blame History

Supabase Studio

TanStack Start + React 19. Dev server: pnpm dev:studio → http://localhost:8082.

Skills — load before working

Load every skill matching the task; stack them when a task spans areas:

Task Skill
Routes, layouts, redirects, server API handlers (routes/**) studio-routes
Query/mutation hooks, query keys (data/**) studio-queries
UI: pages, links, tables, charts, dialogs, sheets, empty states, a11y studio-ui-patterns
Form logic: react-hook-form fields, watch/formState, reset, number inputs react-hook-form
Displaying API errors studio-error-handling
Tests: deciding, writing, reviewing unit & MSW component tests studio-testing
Playwright E2E tests (e2e/studio/**) studio-e2e-tests
PostHog event tracking telemetry-standards
SQL against user databases or analytics logs safe-sql-execution
Logs Explorer SQL, data/logs clickhouse-logs-queries
Keyboard shortcuts, search/filter inputs studio-shortcuts
User-facing copy copywriting

Orientation

  • Routing — TanStack Start file routes under routes/**; routeTree.gen.ts is generated (never hand-edit). Use native TanStack APIs (no next/* imports); load studio-routes for route modules, redirects, and routes/api/**.
  • Data & state — Platform API calls go through data/fetchers.ts (openapi-fetch + api-types) with handleError; never raw fetch. State: valtio (state/), nuqs (URL), react-hook-form + zod (forms).
  • Platform & flags — IS_PLATFORM gates platform-only behavior (withAuth is a no-op self-hosted). Gate platform/flag-only UI in the parent before mounting or fetching; disabled flagged UI early-returns null (verify both states).
  • Tests — Write tests only when they guard a plausible runtime regression static analysis cannot catch. Skipping tests for static copy, pure markup, constants, pass-through wrappers, and standard data/** hooks/keys is expected (studio-testing).
  • Assistant & PATs — Read lib/ai/README.md before changing Assistant chat, tools, prompts, or access handling (and update it when moving entry points or changing the request/tool lifecycle). After editing packages/shared-data/scoped-access-token-permissions.ts, run make -C apps/docs/spec generate.partials.access-control.
  • Reuse first — Search hooks/, lib/, packages/common, and packages/ui-patterns before adding helpers. Reuse EMPTY_ARR/EMPTY_OBJ (lib/void), pluralize/formatCurrency (lib/helpers), getErrorMessage (lib/get-error-message), useLatest (hooks/misc/useLatest), useOnChange (common), and cn (ui).

Engineering principles

Older Studio code predates these conventions. Follow them in new or modified code rather than copying legacy neighbors:

  • Boundary & contract honesty — Validate untrusted inputs at entry (URL params, JSON, process.env, caught unknown) with Zod or guards; inside, trust contracts and fix drift in packages/api-types (satisfies) rather than as casts or defensive shims.
  • Async & lifecycle completeness — Walk pending, in-flight, error, empty, and closed states: unsettled queries never read as false/0, errors never collapse into empty states, slow actions lock in-flight, controlled inputs stay defined, and modals reset on close.
  • State & useEffect — Keep state local, derive values instead of mirroring in useState (Boolean(selectedId)), and avoid unnecessary React Context. Use useEffect only for external sync right before return; fix exhaustive-deps or use useEffectEvent from 'react' (not 'common', never eslint-disable).
  • Modularity & refactoring — Split components around ~200–300 lines; co-locate sub-components, avoid barrel files, and extract non-trivial pure logic to .utils.ts (keep static copy in .tsx, unit-test only real edge cases). Update importers directly (no re-export shims) and delete dead code.
  • Composition & React 19 — Prefer children, compound components, and explicit variants over boolean-prop proliferation or render props, and pass ref as a regular prop in new React 19 components.
  • Types & fetch rendering — Model multi-state values as discriminated unions and name booleans is/has/can/should. Never nest ternaries — render fetch states with early returns or mutually exclusive sibling && blocks (isPending → isError → empty → data).
  • Naming & memoization — Prop callbacks are onX, internal handlers handleX. Custom hooks return useMemo-wrapped objects (not tuples); otherwise memoize only for measured cost or referential stability.
  • Runtime matrix & blast radius — Verify platform vs self-hosted, flag on/off, plan/provider/branch, and permissions; subtract only the exact condition when relaxing a guard. Fire each side effect in one layer and update companion surfaces together.

Non-deducible repo traps

  • CI-only gates — pnpm --filter studio run lint:ratchet (warning counts cannot increase) and pnpm knip --workspace apps/studio (dead files/deps; no inline knip-ignore — add unimported entry files to knip.jsonc entry, or temporary stacked-PR files to ignoreIssues with used from #NNN).
  • ?error= query param — Never use ?error= as a custom URL param: auth-js in AuthProvider intercepts it across all Studio pages and fires a spurious error toast.
  • copyToClipboard (ui) — Never await before calling it (Safari gesture rule, lint-enforced — pass a Promise<string> as the 1st arg). It catches errors internally, so run post-copy feedback (toast.success, setCopied) in its 2nd callback arg, never after await.
  • Params & permissions — Inside components/**, useParams() comes from 'common' (camelCases kebab-case keys, merges search + route params, returns string | undefined); inside routes/**, use Route.useParams() (don't mix both in the same file). useAsyncCheckPermissions (hooks/misc/useCheckPermissions) returns can: true self-hosted.
  • Feature & cloud gating — useIsFeatureEnabled (product features) and useFlag from 'common' (ConfigCat flags) are separate systems. useIsAwsCloudProvider (hooks/misc/useSelectedProject) matches AWS only — also check useIsAwsK8sCloudProvider / useIsAwsNimbusCloudProvider for AWS-hosted features.
  • Icons, dates, toasts & SQL — lucide-react icons import *Icon directly (never Check as CheckIcon aliases); use dayjs (plugins pre-loaded in routes/__root.tsx), toast from 'sonner', and executeSql (data/sql/execute-sql-mutation) for ad-hoc Postgres SQL.