Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(notebooks): warn before leaving with unsaved changes (#58676)

Alex Lider5dce61f62241
diff --git a/frontend/src/scenes/notebooks/Notebook/notebookBeforeUnload.test.ts b/frontend/src/scenes/notebooks/Notebook/notebookBeforeUnload.test.ts
new file mode 100644
index 000000000000..8d18d5c3053d
--- /dev/null
+++ b/frontend/src/scenes/notebooks/Notebook/notebookBeforeUnload.test.ts
@@ -0,0 +1,49 @@
+import { shouldWarnBeforeLeavingNotebook } from './notebookBeforeUnload'
+
+describe('shouldWarnBeforeLeavingNotebook', () => {
+ const baseInput = {
+ isLocalOnly: false,
+ isEditable: true,
+ syncStatus: 'unsaved' as const,
+ currentPathname: '/project/1/notebooks/abc',
+ }
+
+ it.each([
+ ['unsaved', 'unsaved' as const, true],
+ ['saving (in flight, including 409 retry loop)', 'saving' as const, true],
+ ['synced', 'synced' as const, false],
+ ['local', 'local' as const, false],
+ ])('warns when syncStatus is %s -> %s', (_label, syncStatus, expected) => {
+ expect(shouldWarnBeforeLeavingNotebook({ ...baseInput, syncStatus })).toBe(expected)
+ })
+
+ it('does not warn for local-only notebooks (scratchpad / canvas / template)', () => {
+ expect(shouldWarnBeforeLeavingNotebook({ ...baseInput, isLocalOnly: true })).toBe(false)
+ })
+
+ it('does not warn when the notebook is not editable (viewer access, history preview, shared/exported view)', () => {
+ expect(shouldWarnBeforeLeavingNotebook({ ...baseInput, isEditable: false })).toBe(false)
+ })
+
+ it('does not warn for in-page URL updates (same pathname)', () => {
+ expect(
+ shouldWarnBeforeLeavingNotebook({
+ ...baseInput,
+ newPathname: '/project/1/notebooks/abc',
+ })
+ ).toBe(false)
+ })
+
+ it('warns when navigating to a different pathname', () => {
+ expect(
+ shouldWarnBeforeLeavingNotebook({
+ ...baseInput,
+ newPathname: '/project/1/dashboard',
+ })
+ ).toBe(true)
+ })
+
+ it('warns when no newPathname is provided (browser tab close)', () => {
+ expect(shouldWarnBeforeLeavingNotebook(baseInput)).toBe(true)
+ })
+})
diff --git a/frontend/src/scenes/notebooks/Notebook/notebookBeforeUnload.ts b/frontend/src/scenes/notebooks/Notebook/notebookBeforeUnload.ts
new file mode 100644
index 000000000000..6a8e7b413d88
--- /dev/null
+++ b/frontend/src/scenes/notebooks/Notebook/notebookBeforeUnload.ts
@@ -0,0 +1,33 @@
+import type { NotebookSyncStatus } from '../types'
+
+export type ShouldWarnBeforeLeavingNotebookInput = {
+ isLocalOnly: boolean
+ isEditable: boolean
+ syncStatus: NotebookSyncStatus
+ currentPathname: string
+ newPathname?: string
+}
+
+/**
+ * Helper for the unsaved-changes prompt. Returns `true` if the user should be warned before
+ * navigating away.
+ *
+ * `isLocalOnly` covers scratchpad / canvas / templates (no server save ever happens).
+ * `isEditable` covers viewer-level access, history-preview mode, and shared/exported read-only
+ * views (`<Notebook editable={false}>` flips `shouldBeEditable` off, which kills `isEditable`).
+ * `syncStatus` is `unsaved` while there is local content not yet on the server, and `saving`
+ * while a save is in flight (including the 409 retry loop in collab mode).
+ */
+export function shouldWarnBeforeLeavingNotebook(input: ShouldWarnBeforeLeavingNotebookInput): boolean {
+ if (input.isLocalOnly || !input.isEditable) {
+ return false
+ }
+ if (input.syncStatus !== 'unsaved' && input.syncStatus !== 'saving') {
+ return false
+ }
+ // Ignore in-page URL updates (side panel, hash params, comment selection, ...).
+ if (input.newPathname === input.currentPathname) {
+ return false
+ }
+ return true
+}
diff --git a/frontend/src/scenes/notebooks/Notebook/notebookLogic.ts b/frontend/src/scenes/notebooks/Notebook/notebookLogic.ts
index b7a83b70378e..ac27791544b9 100644
--- a/frontend/src/scenes/notebooks/Notebook/notebookLogic.ts
+++ b/frontend/src/scenes/notebooks/Notebook/notebookLogic.ts
@@ -14,7 +14,8 @@ import {
selectors,
} from 'kea'
import { loaders } from 'kea-loaders'
-import { router, urlToAction } from 'kea-router'
+import { beforeUnload, router, urlToAction } from 'kea-router'
+import { CombinedLocation } from 'kea-router/lib/utils'
import { subscriptions } from 'kea-subscriptions'
import posthog from 'posthog-js'
@@ -70,6 +71,7 @@ import {
} from '../types'
import { updateContentHeading } from '../utils'
import { NOTEBOOKS_VERSION, migrate } from './migrations/migrate'
+import { shouldWarnBeforeLeavingNotebook } from './notebookBeforeUnload'
import { notebookCollabLogic } from './notebookCollabLogic'
import { notebookKernelInfoLogic } from './notebookKernelInfoLogic'
import type { notebookLogicType } from './notebookLogicType'
@@ -1272,6 +1274,18 @@ export const notebookLogic = kea<notebookLogicType>([
},
})),
+ beforeUnload((logic) => ({
+ enabled: (newLocation?: CombinedLocation) =>
+ shouldWarnBeforeLeavingNotebook({
+ isLocalOnly: logic.values.isLocalOnly,
+ isEditable: logic.values.isEditable,
+ syncStatus: logic.values.syncStatus,
+ currentPathname: router.values.location.pathname,
+ newPathname: newLocation?.pathname,
+ }),
+ message: 'Leave notebook?\nChanges you made may not be saved.',
+ })),
+
afterMount(({ props }) => {
drainPendingNotebookOperations(props.shortId)
}),