fix(url): write page state into the query string through one serialised helper

Next 16.4 adopts a native history write a render later. A second write
inside that gap was undone when the router adopted the first, and a
write computed from useSearchParams started from the URL before the
last one. Clearing the vault's filters could therefore leave
?encounter= behind: the debounced search wrote the filters it had
rendered back over the clear.

@/lib/url-state is now the one way a page writes its own query: each
write starts from the live address bar, at most one waits for the
router at a time, and later updates queue on top of it and go together
once the router has it (or after a short grace). useUrlSearch reads the
same live value and reports each adoption. The filter hook (documents,
measurements, mood), the open document, the day layer, the timeline's
zoom and range, the medication tabs, the AI provider choice, the
Coach's ?c= and the one-shot parameter strips all go through it; the
filter changes merge into the live filters. A guard refuses raw
history writes and router writes built from the rendered params
outside the helper.

The timeline spec now waits for the values menu to hand focus back
before scrolling to the last row; it measured mid-animation and the
focus return scrolled the page back.
This commit is contained in:
Marc-André Bombeck committed 2026-10-23 02:04:21 +02:00
1 parent 333dfe8746
commit d8e4cf6000
27 files changed
+846 -276

No files matched your search

@@ -10,7 +10,7 @@
* user's click (post-render setState is a no-op under the server renderer;
* the router spies record the navigation the click causes).
*/
import { beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
const baseEntries = [
@@ -101,6 +101,13 @@ vi.mock("@/components/ui/filter-bar", () => ({
import { I18nProvider } from "@/lib/i18n/context";
import { queryKeys } from "@/lib/query-keys";
import { MoodList } from "../mood-list";
import {
installUrlStateBrowser,
type UrlStateBrowser,
} from "@/__tests__/helpers/url-state-browser";
let browser: UrlStateBrowser = installUrlStateBrowser("/");
afterEach(() => browser.uninstall());
function render(search: string) {
mockSearch = search;
@@ -108,11 +115,15 @@ function render(search: string) {
barProps = null;
dateRange = null;
recordedQueryKeys.length = 0;
return renderToStaticMarkup(
const html = renderToStaticMarkup(
<I18nProvider initialLocale="en">
<MoodList />
</I18nProvider>,
);
// The live address the writes start from (`@/lib/url-state`).
browser.uninstall();
browser = installUrlStateBrowser(search ? `/mood?${search}` : "/mood");
return html;
}
const moodSelect = () => selects[0]!;
@@ -127,37 +138,33 @@ describe("MoodList — filter changes write the URL", () => {
it("pushes the mood facet", () => {
render("");
moodSelect().onValueChange("GUT");
expect(pushMock).toHaveBeenCalledWith("/mood?mood=GUT", { scroll: false });
expect(browser.push).toHaveBeenCalledWith("/mood?mood=GUT");
});
it("pushes the source facet on top of the active mood", () => {
render("mood=GUT");
sourceSelect().onValueChange("TELEGRAM");
expect(pushMock).toHaveBeenCalledWith("/mood?mood=GUT&source=TELEGRAM", {
scroll: false,
});
expect(browser.push).toHaveBeenCalledWith("/mood?mood=GUT&source=TELEGRAM");
});
it("replaces (no history entry) for a committed date bound", () => {
render("");
dateRange!.onToChange("2026-05-31");
expect(replaceMock).toHaveBeenCalledWith("/mood?to=2026-05-31", {
scroll: false,
});
expect(pushMock).not.toHaveBeenCalled();
expect(browser.replace).toHaveBeenCalledWith("/mood?to=2026-05-31");
expect(browser.push).not.toHaveBeenCalled();
});
it("clearing the last facet restores the bare pathname", () => {
render("mood=GUT");
moodSelect().onValueChange("ALL");
expect(pushMock).toHaveBeenCalledWith("/mood", { scroll: false });
expect(browser.push).toHaveBeenCalledWith("/mood");
});
it("reset clears every facet in one push", () => {
render("mood=GUT&source=TELEGRAM&from=2026-05-01");
expect(barProps!.isFiltered).toBe(true);
barProps!.onReset();
expect(pushMock).toHaveBeenCalledWith("/mood", { scroll: false });
expect(browser.push).toHaveBeenCalledWith("/mood");
});
});
+5 -5
View File
@@ -197,7 +197,7 @@ export function MoodList({ onAddFirst, onEmptyChange }: MoodListProps = {}) {
// through the router, so a filtered view survives navigation, reload and
// back/forward.
// v1.15.13 — management-list source filter + optional date range.
const { filters, applyFilters } = useUrlFilterSync({
const { filters, applyFilters, patchFilters } = useUrlFilterSync({
parse: parseMoodListSearchParams,
serialise: moodListFiltersToSearch,
});
@@ -298,25 +298,25 @@ export function MoodList({ onAddFirst, onEmptyChange }: MoodListProps = {}) {
// vault's chips); the committed date bounds replace — a from/to pair set in
// one sitting should not cost two Back presses.
const setMoodFilter = (value: string) => {
applyFilters({ ...filters, mood: value === "ALL" ? undefined : value });
patchFilters({ mood: value === "ALL" ? undefined : value });
setPage(1);
clearSelection();
};
const setSourceFilter = (value: string) => {
applyFilters({ ...filters, source: value === "ALL" ? undefined : value });
patchFilters({ source: value === "ALL" ? undefined : value });
setPage(1);
clearSelection();
};
const setFromDay = (value: string) => {
applyFilters({ ...filters, fromDay: value || undefined }, "replace");
patchFilters({ fromDay: value || undefined }, "replace");
setPage(1);
clearSelection();
};
const setToDay = (value: string) => {
applyFilters({ ...filters, toDay: value || undefined }, "replace");
patchFilters({ toDay: value || undefined }, "replace");
setPage(1);
clearSelection();
};