Files
supabase/apps/studio/state/sql-editor/sql-editor-rules.ts
T
Charis Lam cc5548dbe0 refactor(studio): unify snippet save + persistence into SnippetStatus
PR 3 of a stacked refactor. Replaces the two overlapping pieces of snippet
lifecycle state — the savingStates map ('IDLE'|'UPDATING'|'UPDATING_FAILED')
and the isNotSavedInDatabaseYet boolean — with a single SnippetStatus enum.

Save progress and persistence are orthogonal, so the never-persisted 'new'
phase keeps its own saving/failed variants (new, new_saving, new_save_failed)
alongside the persisted ones (saved, unsaved, saving, save_failed). The
wasNeverPersisted() predicate recovers the persistence axis and stays true
across the entire first-save lifecycle, so content-fetching, list
invalidation, and the replication-lag 404 swallow all map faithfully — and a
new snippet's first save keeps its saving/failed indicator.

Status is attached at the data layer so a snippet is never without one:
- SnippetStatus + SnippetWithContent now live in data/content; the snippet
  queries attach status 'saved' via withSavedStatus(), and upsertContent
  returns SnippetWithContent so move/rename responses carry status too.
- A SQL-typed getSqlSnippetById/useSqlSnippetByIdQuery returns
  SnippetWithContent (the generic useContentIdQuery stays for Reports), so
  [id].tsx loads content with no casting.
- 'new' is attached on local creation (createSqlSnippetSkeletonV2).

Dependents import SnippetWithContent/SnippetStatus directly from the data
layer (no state-layer re-exports). 'unsaved' is reserved for manual-save
dirty tracking in a later PR. Pure unit tests for every predicate/transition.
2026-06-23 18:50:55 -04:00

65 lines
1.8 KiB
TypeScript

import type { UpsertContentPayload } from '@/data/content/content-upsert-mutation'
import type { SnippetWithContent } from '@/data/content/sql-folders-query'
/**
*
* 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 },
}
}