mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-15 23:12:04 +00:00
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:
1 parent
333dfe8746
commit
d8e4cf6000
27 files changed
+846
-276
No files matched your search
@@ -16,6 +16,7 @@ import { queryKeys } from "@/lib/query-keys";
|
||||
import { apiGet } from "@/lib/api/api-fetch";
|
||||
import { COACH_SCOPE_SOURCES } from "@/lib/ai/coach/scope-sources";
|
||||
import type { CoachScopeSource } from "@/lib/ai/coach/types";
|
||||
import { writeUrlState } from "@/lib/url-state";
|
||||
|
||||
/**
|
||||
* v1.12.0 (Coach v2 #6) — full-page Coach conversation.
|
||||
@@ -133,13 +134,7 @@ function CoachPageBody() {
|
||||
);
|
||||
useEffect(() => {
|
||||
if (!openSettingsOnData) return;
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.delete("settings");
|
||||
window.history.replaceState(
|
||||
window.history.state,
|
||||
"",
|
||||
url.pathname + url.search,
|
||||
);
|
||||
writeUrlState((params) => params.delete("settings"), { state: "keep" });
|
||||
}, [openSettingsOnData]);
|
||||
|
||||
// #781 — a navigation away mid-reply recorded the interrupted conversation
|
||||
|
||||
@@ -23,6 +23,7 @@ import { Plus } from "lucide-react";
|
||||
import { useRouter, useSearchParams } from "next/navigation";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
import { SUB_PAGE_SLUGS } from "@/lib/insights/sub-page-metric";
|
||||
import { dropParams, writeUrlState } from "@/lib/url-state";
|
||||
|
||||
const INSIGHT_METRIC_RETURN_PATHS: Readonly<Record<string, true>> =
|
||||
Object.fromEntries(
|
||||
@@ -120,12 +121,10 @@ export default function MeasurementsPage() {
|
||||
// during SSR.
|
||||
useEffect(() => {
|
||||
if (!addParam && !returnToParam) return;
|
||||
const next = new URLSearchParams(searchParams.toString());
|
||||
next.delete("add");
|
||||
next.delete("returnTo");
|
||||
const search = next.toString();
|
||||
router.replace(search ? `/measurements?${search}` : "/measurements");
|
||||
}, [addParam, returnToParam, searchParams, router]);
|
||||
// From the live query at the moment of the write (`@/lib/url-state`), so
|
||||
// a filter written meanwhile is never put back to its old value.
|
||||
writeUrlState(dropParams("add", "returnTo"), { afterPaint: true });
|
||||
}, [addParam, returnToParam]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isLoading && !isAuthenticated) {
|
||||
|
||||
@@ -60,6 +60,7 @@ import { useMedicationComplianceSummaryAll } from "@/lib/queries/use-medication-
|
||||
import { useMedicationListLayout } from "@/lib/queries/use-medication-list-layout";
|
||||
import { applyMedicationOrder } from "@/lib/medications/medication-order";
|
||||
import { DEFAULT_TIMEZONE } from "@/lib/tz/format";
|
||||
import { setAllParams, writeUrlState } from "@/lib/url-state";
|
||||
|
||||
interface Schedule {
|
||||
id: string;
|
||||
@@ -310,14 +311,13 @@ export default function MedicationsPageClient() {
|
||||
if (!shouldOpenFromUrl) return;
|
||||
|
||||
// Child effects may run before the App Router installs its native-history
|
||||
// integration, while the responsive sheet also settles its first client
|
||||
// layout. Wait until the following painted frame so replaceState preserves
|
||||
// Next's internals and synchronizes useSearchParams without starting a
|
||||
// router navigation that can remount the open wizard and discard fields.
|
||||
// integration while the sheet settles its first layout. Wait two painted
|
||||
// frames so the write keeps Next's internals and syncs useSearchParams
|
||||
// without a router navigation that would remount the open wizard.
|
||||
let cleanupFrame: number | undefined;
|
||||
const layoutFrame = window.requestAnimationFrame(() => {
|
||||
cleanupFrame = window.requestAnimationFrame(() => {
|
||||
window.history.replaceState(null, "", "/medications");
|
||||
writeUrlState(setAllParams(""));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ import { useRouter, useSearchParams } from "next/navigation";
|
||||
import { useEffect } from "react";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
import { InlineAction } from "@/components/ui/inline-action";
|
||||
import { writeUrlState } from "@/lib/url-state";
|
||||
|
||||
export default function MoodPageClient() {
|
||||
const { user, isAuthenticated, isLoading } = useAuth();
|
||||
@@ -99,11 +100,10 @@ export default function MoodPageClient() {
|
||||
|
||||
useEffect(() => {
|
||||
if (!searchParams.has("add")) return;
|
||||
const next = new URLSearchParams(searchParams.toString());
|
||||
next.delete("add");
|
||||
const search = next.toString();
|
||||
router.replace(search ? `/mood?${search}` : "/mood");
|
||||
}, [searchParams, router]);
|
||||
// From the live query at the moment of the write, never the rendered
|
||||
// copy, so a filter written meanwhile is kept (`@/lib/url-state`).
|
||||
writeUrlState((params) => params.delete("add"), { afterPaint: true });
|
||||
}, [searchParams]);
|
||||
|
||||
useEffect(() => {
|
||||
if (isLoading) return;
|
||||
|
||||
Reference in new issue
Block a user