From ac95c9c7dda5151d6aa96da517e926f7dc41eca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Fri, 9 Oct 2026 17:16:10 +0200 Subject: [PATCH] fix(day): Back on the same page still closes the day Keeping the day open across pages read every loss of ?day= as a page change, so Back past the step that opened the day on the same page reopened it at once. A loss of ?day= with the page unchanged is the person leaving the day, and it closes it as before; only a change of page carries the open day along. --- src/components/day/day-layer.tsx | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/components/day/day-layer.tsx b/src/components/day/day-layer.tsx index a1eb7e8f3..77d503dc5 100644 --- a/src/components/day/day-layer.tsx +++ b/src/components/day/day-layer.tsx @@ -1,6 +1,6 @@ "use client"; -import { useSearchParams } from "next/navigation"; +import { usePathname, useSearchParams } from "next/navigation"; import { Suspense, useCallback, @@ -169,6 +169,23 @@ function DayLayer() { closeDay(); }, [owner]); + // `?day=` gone while the page stayed the same is the person leaving the + // day (Back past the step that opened it); gone with a page change is + // only the URL of another page, and the day comes with it. Declared + // before the effects that read the flag, so they see the close. + const pathname = usePathname(); + const openOn = useRef(null); + useEffect(() => { + if (date !== null) { + openOn.current = pathname; + return; + } + if (openOn.current !== null && openOn.current === pathname) { + if (owner !== null) writeDayClosed(owner); + } + openOn.current = null; + }, [date, pathname, owner]); + const wide = useWideViewport(); const phone = useIsMobile(); const shell: "docked" | "sheet" | "bottom" = phone @@ -215,13 +232,15 @@ function DayLayer() { if (shell !== "docked" || raw !== null || !leftOpen || railDay === null) { return; } + // Read again, not from the render: a Back on this page has just closed it. + if (!readDayOpen(owner)) return; restoring.current = true; window.history.replaceState( null, "", withDayHref(window.location.pathname, window.location.search, railDay), ); - }, [shell, raw, leftOpen, railDay]); + }, [shell, raw, leftOpen, railDay, owner]); // What the polite region says: the day that just opened or was stepped to. const announcement = date !== null ? longLabel(date) : ""; @@ -249,7 +268,7 @@ function DayLayer() { } // Gone from the URL by a page change, not by the person: the day comes // back on the new page, and focus stays with the page. - if (leftOpen) { + if (readDayOpen(owner)) { collapsing.current = false; return; } @@ -262,7 +281,7 @@ function DayLayer() { } } collapsing.current = false; - }, [date, shell, owner, leftOpen]); + }, [date, shell, owner]); const onStep = useCallback( (delta: number) => {