Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Flush notebook cache in valtio and react query whenever closing notebook tab (#49369)

Joshen Lim4778ae66e052
diff --git a/apps/studio/components/interfaces/Explorer/ExplorerNotebookTabCoordinator.tsx b/apps/studio/components/interfaces/Explorer/ExplorerNotebookTabCoordinator.tsx
new file mode 100644
index 00000000000..9e1c723c4ac
--- /dev/null
+++ b/apps/studio/components/interfaces/Explorer/ExplorerNotebookTabCoordinator.tsx
@@ -0,0 +1,39 @@
+import { useQueryClient } from '@tanstack/react-query'
+import { useParams } from 'common'
+import { useContext, useEffect } from 'react'
+
+import { contentKeys } from '@/data/content/keys'
+import { notebooksState } from '@/state/notebooks/notebooks-state'
+import { TabsStateContext } from '@/state/tabs'
+
+/**
+ * Evicts a notebook's content from the valtio store and the React Query cache
+ * when its tab closes, so reopening it always refetches instead of showing
+ * whatever was last loaded. Only applies to notebooks with no unsaved edits —
+ * a dirty notebook is left in the store untouched, same as today, since there's
+ * no autosave or discard-confirmation flow for notebooks yet.
+ *
+ * [Joshen] We'll address discard confirmation separately
+ */
+export const ExplorerNotebookTabCoordinator = () => {
+ const { ref } = useParams()
+ const queryClient = useQueryClient()
+ const tabs = useContext(TabsStateContext)
+
+ useEffect(() => {
+ return tabs.registerTabTypeHandler('notebook', {
+ onClose: (tab) => {
+ const notebookId = tab.metadata?.notebookId
+ if (!ref || !notebookId) return
+
+ const stateNotebook = notebooksState.notebooks[notebookId]
+ if (stateNotebook?.status !== 'saved') return
+
+ notebooksState.removeNotebook({ id: notebookId })
+ queryClient.removeQueries({ queryKey: contentKeys.resource(ref, notebookId) })
+ },
+ })
+ }, [ref, tabs, queryClient])
+
+ return null
+}
diff --git a/apps/studio/components/interfaces/Explorer/__tests__/ExplorerNotebookTabCoordinator.test.tsx b/apps/studio/components/interfaces/Explorer/__tests__/ExplorerNotebookTabCoordinator.test.tsx
new file mode 100644
index 00000000000..8ea127fcb3d
--- /dev/null
+++ b/apps/studio/components/interfaces/Explorer/__tests__/ExplorerNotebookTabCoordinator.test.tsx
@@ -0,0 +1,83 @@
+import { QueryClient } from '@tanstack/react-query'
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { ExplorerNotebookTabCoordinator } from '../ExplorerNotebookTabCoordinator'
+import { contentKeys } from '@/data/content/keys'
+import { notebooksState } from '@/state/notebooks/notebooks-state'
+import type { Notebook } from '@/state/notebooks/types'
+import { createTabId, createTabsState, TabsStateContext } from '@/state/tabs'
+import { customRender } from '@/tests/lib/custom-render'
+
+vi.mock('common', async (importOriginal) => {
+ const actual = await importOriginal<typeof import('common')>()
+ return {
+ ...actual,
+ useParams: () => ({ ref: 'default' }),
+ }
+})
+
+const NOTEBOOK_ID = 'notebook-coordinator-test'
+
+const seedNotebook = (status: 'new' | 'saved') => {
+ delete notebooksState.notebooks[NOTEBOOK_ID]
+ const notebook: Notebook = {
+ id: NOTEBOOK_ID,
+ type: 'notebook',
+ name: 'Test notebook',
+ visibility: 'project',
+ favorite: false,
+ owner_id: 1,
+ project_id: 1,
+ content: { schema_version: 1, cells: [] },
+ }
+ if (status === 'new') notebooksState.addNotebook({ projectRef: 'default', notebook })
+ else notebooksState.setNotebook({ projectRef: 'default', notebook })
+}
+
+const renderCoordinator = (queryClient: QueryClient) => {
+ const tabsState = createTabsState('default')
+ const tabId = createTabId('notebook', { id: NOTEBOOK_ID })
+ tabsState.addTab({ id: tabId, type: 'notebook', metadata: { notebookId: NOTEBOOK_ID } })
+
+ customRender(
+ <TabsStateContext.Provider value={tabsState}>
+ <ExplorerNotebookTabCoordinator />
+ </TabsStateContext.Provider>,
+ { queryClient }
+ )
+
+ return { tabsState, tabId }
+}
+
+afterEach(() => {
+ delete notebooksState.notebooks[NOTEBOOK_ID]
+ notebooksState.needsSaving.clear()
+})
+
+describe('ExplorerNotebookTabCoordinator', () => {
+ it('flushes a saved notebook from the store and evicts its cache entry on close', () => {
+ seedNotebook('saved')
+ const queryClient = new QueryClient()
+ queryClient.setQueryData(contentKeys.resource('default', NOTEBOOK_ID), { id: NOTEBOOK_ID })
+
+ const { tabsState, tabId } = renderCoordinator(queryClient)
+ tabsState.closeTabs([tabId])
+
+ expect(notebooksState.notebooks[NOTEBOOK_ID]).toBeUndefined()
+ expect(queryClient.getQueryData(contentKeys.resource('default', NOTEBOOK_ID))).toBeUndefined()
+ })
+
+ it('leaves an unsaved notebook in the store on close', () => {
+ seedNotebook('new')
+ const queryClient = new QueryClient()
+ queryClient.setQueryData(contentKeys.resource('default', NOTEBOOK_ID), { id: NOTEBOOK_ID })
+
+ const { tabsState, tabId } = renderCoordinator(queryClient)
+ tabsState.closeTabs([tabId])
+
+ expect(notebooksState.notebooks[NOTEBOOK_ID]).toBeDefined()
+ expect(queryClient.getQueryData(contentKeys.resource('default', NOTEBOOK_ID))).toEqual({
+ id: NOTEBOOK_ID,
+ })
+ })
+})
diff --git a/apps/studio/components/layouts/ExplorerLayout/ExplorerLayout.tsx b/apps/studio/components/layouts/ExplorerLayout/ExplorerLayout.tsx
index 7e2f1920e3f..0a9690187ef 100644
--- a/apps/studio/components/layouts/ExplorerLayout/ExplorerLayout.tsx
+++ b/apps/studio/components/layouts/ExplorerLayout/ExplorerLayout.tsx
@@ -16,6 +16,7 @@ import { type ExplorerResourceType } from './ExplorerLayout.constants'
import { ExplorerNavChats } from './ExplorerNavChats'
import { ExplorerNavHome } from './ExplorerNavHome'
import { ExplorerNavNotebooks } from './ExplorerNavNotebooks'
+import { ExplorerNotebookTabCoordinator } from '@/components/interfaces/Explorer/ExplorerNotebookTabCoordinator'
import { ExplorerQueryTabCoordinator } from '@/components/interfaces/Explorer/ExplorerQueryTabCoordinator'
import {
useCreateChat,
@@ -70,6 +71,9 @@ export const ExplorerLayout = ({ browserTitle, children, title }: ExplorerLayout
}
>
<ExplorerQueryTabCoordinator />
+
+ <ExplorerNotebookTabCoordinator />
+
<div className="flex flex-col h-full">
<div className={cn('h-10 md:min-h-(--header-height) flex items-center bg-surface-100')}>
<EditorTabs
diff --git a/apps/studio/state/notebooks/notebook-session-state.ts b/apps/studio/state/notebooks/notebook-session-state.ts
deleted file mode 100644
index ed760aecd43..00000000000
--- a/apps/studio/state/notebooks/notebook-session-state.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-/**
- * Ephemeral, per-session notebook state that is NOT persisted: query cell
- * results and the row limit. Kept separate from the notebook content store
- * (which deals with persistence) because none of this is saved — it lives
- * only for the current editing session, keyed by cell id rather than
- * notebook id since a single notebook can have many independent query cells.
- *
- * [Joshen] Will be fleshed out in subsequent PRs
- */