Files
supabase/apps/studio/state/sql-editor/sql-editor-rules.ts
T
Charis e1e2498db0 refactor(studio): extract SQL editor domain rules into pure module (2/9) (#47204)
## What

PR 2 of a stacked refactor of the SQL editor snippet state. **Stacked on
#47203 (PR 1)** — review/merge that first.

Extracts scattered business rules + the upsert-payload builder into a
new **pure** module `apps/studio/state/sql-editor/sql-editor-rules.ts`
(no Valtio, React, toast, or runtime data-layer imports):

- `canEditSnippet` — read-only rule (shared snippet you don't own), was
inline in `MonacoEditor` `disableEdit`
- `isSnippetOwner` — owner check, was inline in `ReadOnlyBadge` /
`SavingIndicator`
- `validateMoveToFolder` — 'shared snippet cannot be within a folder',
was a buried `toast.error`
- `buildUpsertPayload` — the PUT /content payload, was an inline object
literal (all `??` defaults preserved)
- `isLoadedSnippet` — type guard (see below)

## Bug fix: no more empty-content saves (and no non-null assertion)

The old payload builder used `{ ...content!, content_id: id }`. Tracing
that `!` upstream surfaced a real bug: **favoriting a snippet from the
sidebar that had never been opened** enqueued a save with no loaded
content, producing a PUT with an empty content body (rejected by API).

The requirement that a persisted snippet has loaded content is now
enforced **at the type level** rather than by a runtime assertion or
comment:
- `buildUpsertPayload` accepts only a `LoadedSnippet` (content
non-nullable) — the `!` is gone.
- the save subscriber crosses that boundary via the `isLoadedSnippet`
type guard.
- the sidebar favorite toggle loads content first (mirroring
`onSelectDuplicate` / the share modals), narrowing the fetched union
content to the SQL variant via its discriminant — **no type cast**.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Bug Fixes**
* Improved consistency in read-only behavior and ownership checks across
the SQL editor by centralizing permission logic.
* Fixed favorite toggle to ensure snippet content is fully loaded before
persisting changes.

* **Refactor**
* Centralized SQL snippet permission rules and validation logic into a
dedicated helper module.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-06-23 09:42:49 -04:00

65 lines
1.7 KiB
TypeScript

import type { SnippetWithContent } from './types'
import type { UpsertContentPayload } from '@/data/content/content-upsert-mutation'
/**
*
* A snippet is read-only when it's shared to the project and you are not its
* owner. Returns true when editing IS allowed.
*
*/
export function canEditSnippet(
snippet: Pick<SnippetWithContent, 'visibility' | 'owner_id'>,
profileId?: number
): boolean {
return !(snippet.visibility === 'project' && snippet.owner_id !== profileId)
}
export function isSnippetOwner(
snippet: Pick<SnippetWithContent, 'owner_id'>,
profileId?: number
): boolean {
return profileId === snippet.owner_id
}
/**
*
* Shared snippets cannot live in a folder.
*
*/
export function validateMoveToFolder({
visibility,
folderId,
}: {
visibility?: SnippetWithContent['visibility']
folderId?: string | null
}): { ok: boolean; error?: string } {
if (visibility === 'project' && !!folderId) {
return { ok: false, error: 'Shared snippet cannot be within a folder' }
}
return { ok: true }
}
export type LoadedSnippet = SnippetWithContent & {
content: NonNullable<SnippetWithContent['content']>
}
export function isLoadedSnippet(snippet: SnippetWithContent): snippet is LoadedSnippet {
return snippet.content != null
}
export function buildUpsertPayload(snippet: LoadedSnippet, id: string): UpsertContentPayload {
const { name, description, visibility, project_id, owner_id, folder_id, content, favorite } =
snippet
return {
id,
type: 'sql',
name: name ?? 'Untitled',
description: description ?? '',
visibility: visibility ?? 'user',
project_id: project_id ?? 0,
owner_id,
folder_id: folder_id ?? undefined,
favorite: favorite ?? false,
content: { ...content, content_id: id },
}
}