studio-best-practices

作者: supabase

React 和 TypeScript 在 Supabase Studio 中的最佳实践。在编写时使用

npx skills add https://github.com/supabase/supabase --skill studio-best-practices

Studio Best Practices

Applies to apps/studio/**/*.{ts,tsx}.

Boolean Naming

Use descriptive prefixes — derive from existing state rather than storing separately:

  • is — state/identity: isLoading, isPaused, isNewRecord
  • has — possession: hasPermission, hasData
  • can — capability: canUpdateColumns, canDelete
  • should — conditional behavior: shouldFetch, shouldRender

Extract complex conditions into named variables:

// ❌ inline multi-condition
{
  !isSchemaLocked && isTableLike(selectedTable) && canUpdateColumns && !isLoading && <Button />
}

// ✅ named variable
const canShowAddButton =
  !isSchemaLocked && isTableLike(selectedTable) && canUpdateColumns && !isLoading
{
  canShowAddButton && <Button />
}

Derive booleans — don't store them:

// ❌ stored derived state
const [isFormValid, setIsFormValid] = useState(false)
useEffect(() => {
  setIsFormValid(name.length > 0 && email.includes('@'))
}, [name, email])

// ✅ derived
const isFormValid = name.length > 0 && email.includes('@')

Component Structure

See vercel-composition-patterns skill for compound component and composition patterns.

Keep components under 200–300 lines. Split when you see:

  • Multiple distinct UI sections
  • Complex conditional rendering
  • Multiple unrelated useState calls
  • Hard to understand at a glance

Co-locate sub-components in the same directory as the parent. Avoid barrel re-export files.

Extract repeated JSX patterns into small components.

Data Fetching

All data fetching uses TanStack Query (React Query). See studio-queries skill for query/mutation patterns and studio-error-handling skill for error display conventions.

Loading / Error / Success Pattern

Top level:

const { data, error, isLoading, isError, isSuccess } = useQuery(...)

if (isLoading) return <GenericSkeletonLoader />
if (isError) return <AlertError error={error} subject="Failed to load data" />
if (isSuccess && data.length === 0) return <EmptyState />
return <DataDisplay data={data} />

Use early returns — avoid deeply nested conditionals.

Inline:

<div>
  {isLoading && <InlineLoader />}
  {isError && <InlineError error={error} />}
  {isSuccess && data.length === 0 && <EmptyState />}
  {isSuccess && data.length > 0 && <DataDisplay data={data} />}
</div>

State Management

Keep state as local as possible; lift only when needed.

Group related form state with react-hook-form rather than multiple useState calls. See studio-ui-patterns skill for form layout and component conventions.

// ❌ multiple related useState
const [name, setName] = useState('')
const [email, setEmail] = useState('')

// ✅ grouped with react-hook-form
const form = useForm<FormValues>({ defaultValues: { name: '', email: '' } })

Custom Hooks

Extract complex or reusable logic into hooks. Return objects, not arrays:

// ❌ array return (hard to extend)
return [value, toggle]

// ✅ object return
return { value, toggle, setTrue, setFalse }

Event Handlers

  • Prop callbacks: on prefix (onClose, onSave)
  • Internal handlers: handle prefix (handleSubmit, handleCancel)

Use useCallback for handlers passed to memoized children; avoid unnecessary inline arrow functions.

Conditional Rendering

// Simple show/hide
<>{isVisible && <Component />}</>

// Binary choice
<>{isLoading ? <Spinner /> : <Content />}</>

// Multiple conditions — use early returns, not nested ternaries
if (isLoading) return <Spinner />
if (isError) return <Error />
return <Content />

Performance

useMemo for genuinely expensive computations (measured, not assumed). Don't wrap everything — only optimize when you have a measured problem or are passing values to memoized children.

TypeScript

Define prop interfaces explicitly. Use discriminated unions for complex state:

type AsyncState<T> =
  | { status: 'idle' }
  | { status: 'loading' }
  | { status: 'success'; data: T }
  | { status: 'error'; error: Error }

Avoid as any / as Type casts. Validate at boundaries with zod:

// ❌ type cast
const user = apiResponse as User

// ✅ zod parse
const user = userSchema.parse(apiResponse)
// or safe:
const result = userSchema.safeParse(apiResponse)

Testing

Extract logic into .utils.ts pure functions and test exhaustively. See the studio-testing skill for the full testing strategy and decision tree.

来自 supabase 的更多技能

studio-e2e-tests
supabase
编写并运行Supabase Studio的Playwright端到端测试。在收到相关请求时使用。
official
vitest
supabase
由 Vite 驱动的快速单元测试框架,兼容 Jest API。在编写测试、模拟、配置覆盖率或处理测试时使用。
official
skill-creator
supabase
创建模块化技能的全面指南,用于扩展Claude的能力,涵盖专业知识和工作流程。技能包含必需的SKILL.md文件(含YAML前置元数据和Markdown指令),以及按用途组织、渐进加载以节省上下文的可选捆绑资源(脚本、参考资料、素材)。围绕具体使用案例设计技能;为确定性任务识别可复用脚本,为领域知识准备参考文件,为...提供素材。
official
supabase
supabase
在执行任何涉及Supabase的任务时使用。触发条件:Supabase产品(数据库、认证、边缘函数、实时功能、存储、向量、定时任务、队列);客户端…
official
supabase-server
supabase
在编写使用Supabase的服务器端代码时使用——包括Edge Functions、Hono应用、Webhook处理器或任何需要Supabase认证和客户端创建的后端。
official
clickhouse-logs-queries
supabase
Write, review, and migrate Supabase logs queries against the ClickHouse-backed `logs` table (the `logs.all.otel` analytics endpoint). Use this whenever a task…
official
dev-toolbar-review
supabase
在审查涉及 packages/dev-tools/、packages/common/posthog-client.ts 的 PR 时使用
official
e2e-studio-tests
supabase
在Studio应用中运行端到端测试。当被要求运行端到端测试、运行Studio测试、Playwright测试或测试功能时使用。
official