From 5cfc905f100c689f70d981ad784273714050b93b Mon Sep 17 00:00:00 2001 From: Linus Rath Date: Sun, 12 Apr 2026 01:33:28 +0200 Subject: [PATCH] fix: seed list history entry when app initializes on an email view --- hooks/use-browser-navigation.ts | 35 ++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/hooks/use-browser-navigation.ts b/hooks/use-browser-navigation.ts index 76c5f680..f4676f6f 100644 --- a/hooks/use-browser-navigation.ts +++ b/hooks/use-browser-navigation.ts @@ -147,9 +147,38 @@ export function useBrowserNavigation({ if (!initializedRef.current) { initializedRef.current = true; - // Replace the current entry on the very first run so we don't - // create an extra step the user has to back through to leave the app. - window.history.replaceState(newState, ""); + + if (emailId || threadId) { + // The app is initializing directly on an email/thread view (e.g. the + // user navigated here from /settings or an external link). Seed a + // "list" history entry first so that the toolbar back button returns + // to the list instead of leaving the app entirely. + const listSnapshot: NavSnapshot = { + mailboxId, + emailId: null, + threadId: null, + composerOpen: false, + sidebarOpen, + }; + const listStored: StoredNavState = { + ...listSnapshot, + navId: ++navIdCounter, + }; + const baseState = (window.history.state ?? {}) as Record< + string, + unknown + >; + window.history.replaceState( + { ...baseState, [STATE_KEY]: listStored }, + "", + ); + // Now push the actual email state on top of the synthetic list entry. + window.history.pushState(newState, ""); + } else { + // Replace the current entry on the very first run so we don't + // create an extra step the user has to back through to leave the app. + window.history.replaceState(newState, ""); + } } else { window.history.pushState(newState, ""); }