Files
supabase/apps/studio/state/sql-editor/sql-editor-rules.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

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: true } | { ok: false; 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 },
}
}