Files
supabase/apps/studio/state/sql-editor/sql-editor-lifecycle.ts
Charis 5cb81123ae refactor(studio): move SQL editor save trigger into a scheduler + provider (5/9) (#47316)
## What

PR 5 of a stacked refactor. Moves *when to save* out of a module-load
`subscribe` and into an injectable **scheduler** armed by a headless
**provider**, splits the save queue, and adds an unsaved-close warning.

### Scheduler (`sql-editor-save-scheduler.ts`)
`createSaveScheduler({ state, saveMechanism, notify, getSaveMode })`
owns the save *policy*:
- **auto** mode drains the dirty snippet queue as edits land; **manual**
mode (the seam for a future opt-in; defaults to `auto`) leaves snippets
queued until `requestSave`. Folder saves always drain.
- `start()` returns an unsubscribe; `requestSave(id)` is the
explicit-save entry.

### Provider (`sql-editor-save-coordinator.tsx`)
Headless `SqlEditorSaveCoordinatorProvider` instantiates the mechanism
(invalidation via the **React Query client from context**, not the
global `getQueryClient`) + scheduler, `start()`s it in an effect
(start/stop with the provider), and exposes `requestSave` via
`useSqlEditorSaveCoordinator()`. Mounted in `ProjectContext` (under the
app's QueryClientProvider). Cmd+S and the SavingIndicator Retry now go
through `requestSave`.

### Queue split
`needsSaving` (snippets) and `pendingFolderSaves` (folders) are separate
queues, drained independently — the old snippet-vs-folder `if/else` is
gone.

### Unsaved-close warning
A `beforeunload` guard triggers the browser's native "Leave site?"
prompt while any snippet's `status !== 'saved'` (failed / in-flight /
never-saved).

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

## Summary by CodeRabbit

* **New Features**
* Improved SQL editor saving with a centralized save flow, including
automatic/manual save handling and immediate “Save Query” requests.
* Added unsaved-change detection so the app can warn before closing or
reloading when edits are still pending.

* **Bug Fixes**
* Retry actions now use the updated save flow for more reliable
re-saving.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-06-25 16:24:04 -04:00

87 lines
3.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'
}
/**
* True when the snippet holds changes that are not safely persisted: never
* saved ('new' family), a save in flight, a failed save, or pending local edits
* ('unsaved'). Used to warn before the tab is closed. Only 'saved' is clean.
*/
export function hasUnsavedChanges(status: SnippetStatus | undefined): boolean {
return status !== undefined && status !== 'saved'
}
/**
* 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'
}
/**
* The lifecycle of a folder in the SQL editor nav, as a single set of
* mutually-exclusive states. Like SnippetStatus, this collapses two orthogonal
* axes — persistence (a locally-created placeholder vs a persisted folder) and
* progress (inline-name editing / save in flight / settled) — into one enum, so
* a folder can never be in a nonsensical combination (e.g. "new" yet "idle").
* The predicates below recover each axis.
*/
export type FolderStatus =
// Never persisted to the database (a locally-created placeholder):
| 'new_editing' // its name is being entered inline
| 'new_saving' // its create is in flight
// Persisted to the database:
| 'idle' // settled
| 'editing' // its name is being edited inline (rename)
| 'saving' // its rename is in flight
/** True for a locally-created placeholder folder that has not been persisted. */
export function isNewFolder(status: FolderStatus | undefined): boolean {
return status === 'new_editing' || status === 'new_saving'
}
/** True while the folder's name is being edited inline. */
export function isFolderEditing(status: FolderStatus | undefined): boolean {
return status === 'new_editing' || status === 'editing'
}
/** True while a folder create/rename is in flight. */
export function isFolderSaving(status: FolderStatus | undefined): boolean {
return status === 'new_saving' || status === 'saving'
}
/** Transition when a folder save begins, preserving the never-persisted axis. */
export function folderStatusOnSaveStart(status: FolderStatus): FolderStatus {
return isNewFolder(status) ? 'new_saving' : 'saving'
}