Files
supabase/apps/studio/state/sql-editor/sql-editor-lifecycle.ts
T
Charis d5653f1f92 refactor(studio): unify snippet save + persistence into SnippetStatus (3/9) (#47251)
## 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 -->
2026-06-24 08:56:39 -04:00

41 lines
1.5 KiB
TypeScript

import type { SnippetStatus } from '@/data/content/snippet-status'
/**
* True when the snippet has never been successfully written to the database.
* Gates content fetching and list invalidation, and marks locally-created
* snippets for the replication-lag 404 swallow in the `[id]` page.
*/
export function wasNeverPersisted(status: SnippetStatus | undefined): boolean {
return status === 'new' || status === 'new_saving' || status === 'new_save_failed'
}
/** True while a save request is in flight (whether first save or re-save). */
export function isSaving(status: SnippetStatus | undefined): boolean {
return status === 'new_saving' || status === 'saving'
}
/**
* True when the most recent save attempt failed (whether first save or
* re-save).
*/
export function isSaveFailed(status: SnippetStatus | undefined): boolean {
return status === 'new_save_failed' || status === 'save_failed'
}
/**
* Transition when a save request begins, preserving the never-persisted axis.
*/
export function statusOnSaveStart(status: SnippetStatus | undefined): SnippetStatus {
return wasNeverPersisted(status) ? 'new_saving' : 'saving'
}
/** Transition when a save succeeds — the snippet is now persisted and clean. */
export function statusOnSaveSuccess(): SnippetStatus {
return 'saved'
}
/** Transition when a save fails, preserving the never-persisted axis. */
export function statusOnSaveError(status: SnippetStatus | undefined): SnippetStatus {
return wasNeverPersisted(status) ? 'new_save_failed' : 'save_failed'
}