mirror of
https://github.com/supabase/supabase.git
synced 2026-10-07 18:35:07 +03:00
## What PR 3 of a stacked refactor of the SQL editor snippet state. 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. ## Status is attached at the data layer (never absent) - `SnippetStatus` + `SnippetWithContent` now live in `data/content`. The snippet queries attach `status: 'saved'` via a typed `withSavedStatus()` helper, and `upsertContent` returns `SnippetWithContent` so move/rename responses carry status too. - A SQL-typed `getSqlSnippetById`/`useSqlSnippetByIdQuery` returns `SnippetWithContent` (the generic `useContentIdQuery` stays for Reports, which use it). `[id].tsx` loads content with **no casting**. - `'new'` is attached on local creation (`createSqlSnippetSkeletonV2`). ## Behavior Behavior-preserving for the existing auto-save flow (faithful mapping of both old fields, including the replication-lag swallow). One incidental fix: the read-only/saving indicator now also covers a brand-new snippet's first save (previously only re-saves of persisted snippets had distinct saving/failed states in some paths). ## Tests New `sql-editor-lifecycle.test.ts` (29 tests) covering every predicate and transition; existing rules tests updated. `pnpm --filter studio typecheck` clean; 52 state/sql-editor unit tests pass. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **Refactor** * Restructured SQL snippet persistence tracking, replacing boolean flags with a comprehensive status system for clearer visibility into save progress. * Enhanced saving indicator UI to reflect accurate snippet save states. * **Tests** * Added test coverage for snippet persistence state transitions and lifecycle scenarios. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
65 lines
1.8 KiB
TypeScript
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 },
|
|
}
|
|
}
|