"use client"; import { useEffect } from "react"; // A LINK TO A RELEASE ON ANOTHER PAGE. /changelog is paged (?page=N), and a // copied link is `/changelog#` with no page — so a link to an older // release would open page 1 and find nothing. This looks the anchor up in the // map the server sent (every anchor → its page) and, when another page holds // it, goes there. It sits in the page's shell, outside the streamed notes, so // it acts as soon as the shell hydrates rather than after page 1's notes load. export function ChangelogAnchorResolver({ anchors, page, }: { anchors: Record; page: number; }) { useEffect(() => { const resolve = () => { let id: string; try { id = decodeURIComponent(window.location.hash.slice(1)); } catch { return; } if (!id || !Object.hasOwn(anchors, id)) return; const target = anchors[id]; if (target === page) return; const query = target === 1 ? "" : `?page=${target}`; window.location.replace( `${window.location.pathname}${query}#${encodeURIComponent(id)}`, ); }; resolve(); window.addEventListener("hashchange", resolve); return () => window.removeEventListener("hashchange", resolve); }, [anchors, page]); return null; } // THE SCROLL A STREAMED PAGE MISSES. The notes arrive after the shell, moved // into place by React's streaming script, so the browser's own scroll to // `#anchor` can fire while the heading is still in a hidden holding node and // leave the page at the top. Rendered at the END of the notes, this mounts // once they are in the document, and scrolls to the hash's element if it is // not already in view. export function ScrollToHash() { useEffect(() => { let id: string; try { id = decodeURIComponent(window.location.hash.slice(1)); } catch { return; } if (!id) return; const el = document.getElementById(id); if (!el) return; const box = el.getBoundingClientRect(); if (box.top >= 0 && box.bottom <= window.innerHeight && box.height > 0) return; // An empty `v` marker has no box of its own; its heading does. (box.height > 0 ? el : (el.parentElement ?? el)).scrollIntoView({ block: "start" }); }, []); return null; }