Files
supabase__supabase/apps/studio/data/content/notebooks/notebook-cache.ts
Joshen Lim a1686025b6 Joshenlim/fe 4291 keep unsaved notebooks accessible after page refresh (#49673)
## Context

Changes here adds a "Draft" state for notebooks with a new
`notebook-drafts` store - similar to how we handle query tabs in the
explorer.
This implies that if a user refreshes the tab while there's unsaved
changes to notebooks, the changes can be persisted locally and the user
will be able to continue from where they left off.

This also implies that If you create a new notebook (OR open an existing
notebook and make some changes) and refresh the browser, we no longer
show the native browser confirmation dialog about discarding changes.

We also reuse the existing confirmation dialog when saving a notebook if
its draft has diverged from the server side content - just updated the
language to be more generic rather than saying that the Assistant made
changes
<img width="429" height="238" alt="image"
src="https://github.com/user-attachments/assets/5c392aed-1633-4428-8060-28f495a01f04"
/>

Also fixes an unrelated issue - renaming a notebook should mark the
notebook as having unsaved changes (with the orange indicator)

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

## Summary by CodeRabbit

* **New Features**
* Unsaved notebook edits are saved locally and restored when reopening
Studio.
* Drafts are scoped by project and protected from server changes through
conflict detection.
* Notebook tabs indicate unsaved changes, including drafts from unsaved
notebooks.
* **Bug Fixes**
* Closing a tab with local edits prompts for confirmation and removes
its saved draft.
  * Notebook save state reflects the server-confirmed update time.
  * Conflict messages clearly describe changes made on the server.
* **Style**
  * Improved keyboard focus behavior for tab controls.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-09-09 17:17:25 +08:00

51 lines
1.7 KiB
TypeScript

import type { QueryClient } from '@tanstack/react-query'
import type { Snapshot } from 'valtio'
import { contentKeys } from '@/data/content/keys'
import { removeNotebookDraft } from '@/state/notebooks/notebook-drafts'
import { notebooksState } from '@/state/notebooks/notebooks-state'
import type { StateNotebook } from '@/state/notebooks/types'
import { hasUnsavedChanges } from '@/state/sql-editor/sql-editor-lifecycle'
/**
* Whether a notebook has edits worth discarding on close: anything not yet
* saved, except a never-persisted notebook that's still empty (nothing to
* lose by closing it).
*/
export function hasDiscardableChanges(
stateNotebook: StateNotebook | Snapshot<StateNotebook> | undefined
): boolean {
if (!hasUnsavedChanges(stateNotebook?.status)) return false
const isEmptyNewNotebook =
stateNotebook?.status === 'new' && (stateNotebook.notebook.content?.cells.length ?? 0) === 0
return !isEmptyNewNotebook
}
/**
* Evicts a notebook from the React Query cache and the Valtio store together.
* Callers are responsible for confirming before discarding unsaved edits.
*
* Removes the query entry outright rather than invalidating it: a mounted
* `useNotebookQuery` observer would otherwise read the stale cached value
* synchronously, before its refetch lands, and `notebooksState.setNotebook`'s
* merge guard would treat that stale merge as already-loaded and drop the
* real update.
*
*/
export function evictNotebookFromCaches({
queryClient,
projectRef,
id,
}: {
queryClient: QueryClient
projectRef: string
id: string
}): boolean {
notebooksState.removeNotebook({ id })
queryClient.removeQueries({ queryKey: contentKeys.resource(projectRef, id) })
removeNotebookDraft({ projectRef, id })
return true
}