: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(notebooks): stop leave warnings on unrelated pages (#130036)
Paul D'Ambra3d1cdc3b8f2a
diff --git a/frontend/src/scenes/notebooks/Notebook/Notebook.tsx b/frontend/src/scenes/notebooks/Notebook/Notebook.tsxindex fe23f8ff6e18..86eea85c800a 100644--- a/frontend/src/scenes/notebooks/Notebook/Notebook.tsx+++ b/frontend/src/scenes/notebooks/Notebook/Notebook.tsx@@ -70,7 +70,15 @@ export function Notebook({isLocalOnly,shouldBeEditable,} = useValues(logic)- const { duplicateNotebook, loadNotebook, setEditable, setLocalContent, setContainerSize } = useActions(logic)+ const {+ duplicateNotebook,+ loadNotebook,+ notebookEditorMounted,+ notebookEditorUnmounted,+ setEditable,+ setLocalContent,+ setContainerSize,+ } = useActions(logic)const { isMarkdownExpanded } = useValues(notebookSettingsLogic)useAttachedContext(notebook && !isShared@@ -101,6 +109,11 @@ export function Notebook({// A retained notebook is present on the first render. Rendering it read-only and then switching every block// to editable rewrites `contenteditable` across the whole document, so the content waits for this value.const [editableApplied, setEditableApplied] = useState(shouldBeEditable === editable)+ useLayoutEffect(() => {+ notebookEditorMounted()+ return () => notebookEditorUnmounted()+ }, [notebookEditorMounted, notebookEditorUnmounted])+useLayoutEffect(() => {setEditable(editable)setEditableApplied(true)diff --git a/frontend/src/scenes/notebooks/Notebook/markdownNotebookEditorState.test.ts b/frontend/src/scenes/notebooks/Notebook/markdownNotebookEditorState.test.tsindex a1de2d47de9f..b1f04c5a20d4 100644--- a/frontend/src/scenes/notebooks/Notebook/markdownNotebookEditorState.test.ts+++ b/frontend/src/scenes/notebooks/Notebook/markdownNotebookEditorState.test.ts@@ -12,6 +12,7 @@ import {getMarkdownNotebookMarkdown,isMarkdownNotebookContent,} from './markdownNotebookV2'+import { shouldWarnBeforeLeavingNotebook } from './notebookBeforeUnload'import { NotebookLogicMode, notebookLogic } from './notebookLogic'import { NOTEBOOK_AI_PRESENCE_CLIENT_ID, NOTEBOOK_AI_PRESENCE_NAME } from './notebookPresence'@@ -108,6 +109,37 @@ describe('notebookLogic markdown editor state', () => {retained.unmount()})+ it('stops the in-app leave warning after the last editor unmounts', () => {+ const warningInput = {+ isLocalOnly: false,+ isEditable: true,+ syncStatus: 'unsaved' as const,+ currentPathname: '/project/1/groups',+ newPathname: '/project/1/dashboard',+ }++ logic.actions.notebookEditorMounted()+ logic.actions.notebookEditorMounted()+ expect(logic.values.mountedEditorCount).toBe(2)+ expect(+ shouldWarnBeforeLeavingNotebook({+ ...warningInput,+ hasMountedEditor: logic.values.mountedEditorCount > 0,+ })+ ).toBe(true)++ logic.actions.notebookEditorUnmounted()+ expect(logic.values.mountedEditorCount).toBe(1)+ logic.actions.notebookEditorUnmounted()+ expect(logic.values.mountedEditorCount).toBe(0)+ expect(+ shouldWarnBeforeLeavingNotebook({+ ...warningInput,+ hasMountedEditor: logic.values.mountedEditorCount > 0,+ })+ ).toBe(false)+ })+it('applies editor changes to local content when no interaction is active', () => {const nextMarkdown = `${BASE_MARKDOWN} edited`diff --git a/frontend/src/scenes/notebooks/Notebook/notebookBeforeUnload.test.ts b/frontend/src/scenes/notebooks/Notebook/notebookBeforeUnload.test.tsindex 8d18d5c3053d..fc68fe2a4157 100644--- a/frontend/src/scenes/notebooks/Notebook/notebookBeforeUnload.test.ts+++ b/frontend/src/scenes/notebooks/Notebook/notebookBeforeUnload.test.ts@@ -5,6 +5,7 @@ describe('shouldWarnBeforeLeavingNotebook', () => {isLocalOnly: false,isEditable: true,syncStatus: 'unsaved' as const,+ hasMountedEditor: true,currentPathname: '/project/1/notebooks/abc',}@@ -46,4 +47,12 @@ describe('shouldWarnBeforeLeavingNotebook', () => {it('warns when no newPathname is provided (browser tab close)', () => {expect(shouldWarnBeforeLeavingNotebook(baseInput)).toBe(true)})++ it.each([+ [false, '/project/1/dashboard', false],+ [true, '/project/1/dashboard', true],+ [false, undefined, true],+ ])('guards navigation with mounted editor %s to %s: %s', (hasMountedEditor, newPathname, expected) => {+ expect(shouldWarnBeforeLeavingNotebook({ ...baseInput, hasMountedEditor, newPathname })).toBe(expected)+ })})diff --git a/frontend/src/scenes/notebooks/Notebook/notebookBeforeUnload.ts b/frontend/src/scenes/notebooks/Notebook/notebookBeforeUnload.tsindex 6a8e7b413d88..f10ff77c79f9 100644--- a/frontend/src/scenes/notebooks/Notebook/notebookBeforeUnload.ts+++ b/frontend/src/scenes/notebooks/Notebook/notebookBeforeUnload.ts@@ -4,6 +4,7 @@ export type ShouldWarnBeforeLeavingNotebookInput = {isLocalOnly: booleanisEditable: booleansyncStatus: NotebookSyncStatus+ hasMountedEditor: booleancurrentPathname: stringnewPathname?: string}@@ -29,5 +30,8 @@ export function shouldWarnBeforeLeavingNotebook(input: ShouldWarnBeforeLeavingNoif (input.newPathname === input.currentPathname) {return false}+ if (input.newPathname && !input.hasMountedEditor) {+ return false+ }return true}diff --git a/frontend/src/scenes/notebooks/Notebook/notebookLogic.ts b/frontend/src/scenes/notebooks/Notebook/notebookLogic.tsindex 82507e31fb09..53a808c84233 100644--- a/frontend/src/scenes/notebooks/Notebook/notebookLogic.ts+++ b/frontend/src/scenes/notebooks/Notebook/notebookLogic.ts@@ -353,6 +353,7 @@ export interface notebookLogicValues {markdownRemoteParticipants: NotebookRemoteParticipant[]markdownRemotePresence: Record<string, NotebookRemotePresenceState>mode: NotebookLogicMode+ mountedEditorCount: numbernewNotebook: NotebookType | nullnewNotebookLoading: booleannodeLogics: Record<string, BuiltLogic<notebookNodeLogicType>>@@ -509,6 +510,12 @@ export interface notebookLogicActions {value: true}}+ notebookEditorMounted: () => {+ value: true+ }+ notebookEditorUnmounted: () => {+ value: true+ }notebookVariablesSaved: (variables: NotebookVariableApi[]) => {variables: NotebookVariableApi[]}@@ -791,6 +798,8 @@ export const notebookLogic = kea<notebookLogicType>([],})),actions({+ notebookEditorMounted: true,+ notebookEditorUnmounted: true,setAutosavePaused: (paused: boolean) => ({ paused }),setMarkdownEditorInteractionActive: (active: boolean) => ({ active }),setMarkdownAIPresenceActive: (active: boolean) => ({ active }),@@ -856,6 +865,13 @@ export const notebookLogic = kea<notebookLogicType>([setAccessDeniedToNotebook: true,}),reducers(({ props }) => ({+ mountedEditorCount: [+ 0,+ {+ notebookEditorMounted: (count) => count + 1,+ notebookEditorUnmounted: (count) => Math.max(0, count - 1),+ },+ ],isShareModalOpen: [false,{@@ -2260,6 +2276,7 @@ export const notebookLogic = kea<notebookLogicType>([isLocalOnly: logic.values.isLocalOnly,isEditable: logic.values.isEditable,syncStatus: logic.values.syncStatus,+ hasMountedEditor: logic.values.mountedEditorCount > 0,currentPathname: router.values.location.pathname,newPathname: newLocation?.pathname,}),