diff --git a/src/app/insights/page.tsx b/src/app/insights/page.tsx index c05923f44..8fdfffb72 100644 --- a/src/app/insights/page.tsx +++ b/src/app/insights/page.tsx @@ -1,12 +1,12 @@ "use client"; -import { useEffect } from "react"; import { useQuery } from "@tanstack/react-query"; import Link from "next/link"; import { Loader2, TrendingUp } from "lucide-react"; import { useAuth } from "@/hooks/use-auth"; import { useFeatureFlags } from "@/hooks/use-feature-flags"; +import { useScrollResetOnRoute } from "@/hooks/use-scroll-reset-on-route"; import { useTranslations } from "@/lib/i18n/context"; import { Button } from "@/components/ui/button"; import { EmptyState } from "@/components/ui/empty-state"; @@ -16,6 +16,7 @@ import { TrendsRow } from "@/components/insights/trends-row"; import { CorrelationRow } from "@/components/insights/correlation-row"; import { useInsightsAdvisorQuery } from "@/components/insights/use-insights-advisor"; import { useCoachLaunch } from "@/lib/insights/coach-launch-context"; +import { useAnalyticsQuery } from "@/lib/queries/use-analytics-query"; import type { CorrelationResult } from "@/lib/insights/correlations"; import type { DataSummary } from "@/lib/analytics/trends"; @@ -95,20 +96,13 @@ export default function InsightsPage() { const { isAuthenticated, user } = useAuth(); const { t } = useTranslations(); - // v1.4.28 FB-D3 — mirror ``'s deferred scroll-reset on - // the mother page so a back-navigation from a sub-page (where - // `SubPageShell` already reset the scroll on mount) lands cleanly - // at the top of the overview instead of inheriting the sub-page's - // vertical position through the cached scroll state. Single - // mount-only effect; uses `requestAnimationFrame` so the reset - // settles after first paint, same as the sub-page shell. - useEffect(() => { - if (typeof window === "undefined") return; - const handle = window.requestAnimationFrame(() => { - window.scrollTo({ top: 0, behavior: "auto" }); - }); - return () => window.cancelAnimationFrame(handle); - }, []); + // v1.4.33 IW9 — scroll-to-top on route mount centralised in the + // shared `useScrollResetOnRoute()` hook. The mother page + the + // `` both consume the same hook; the legacy duplicate + // RAF that lived here pre-v1.4.33 produced a visible double-snap on + // slow hydrates (chart skeletons inflating between the two + // callbacks). + useScrollResetOnRoute(); // v1.4.27 R3d MB4 — Coach drawer state lives in the layout-level // `` so every routed sub-page can reach it. @@ -134,16 +128,12 @@ export default function InsightsPage() { // beyond the React-state subscription. const advisor = useInsightsAdvisorQuery(isAuthenticated); - const { data: analytics } = useQuery({ - queryKey: ["analytics"], - queryFn: async () => { - const res = await fetch("/api/analytics"); - if (!res.ok) throw new Error("Failed"); - const json = await res.json(); - return json.data as AnalyticsData; - }, - enabled: isAuthenticated, - }); + // v1.4.33 IW2 — the mother page reads `correlations` + `healthScore` + // (thick-only fields) so it stays on the default thick slice. The + // shared hook still centralises the cache settings so the consumer + // dedups with the sub-page mounts that ride the slim slice instead. + const analyticsQuery = useAnalyticsQuery(); + const analytics = analyticsQuery.data as AnalyticsData | undefined; if (isLoading) { return ( diff --git a/src/components/insights/sub-page-shell.tsx b/src/components/insights/sub-page-shell.tsx index e4856dd29..142e1c3b7 100644 --- a/src/components/insights/sub-page-shell.tsx +++ b/src/components/insights/sub-page-shell.tsx @@ -3,6 +3,7 @@ import { useEffect, useRef, type ReactNode } from "react"; import { Badge } from "@/components/ui/badge"; +import { useScrollResetOnRoute } from "@/hooks/use-scroll-reset-on-route"; import { cn } from "@/lib/utils"; /** @@ -63,21 +64,18 @@ export function SubPageShell({ // CF-35 made the focus call opt-in via `focusOnMount` — the // default-on version stole focus from soft-keyboards on mobile. // - // v1.4.28 FB-D3 — defer the `scrollTo` to the next animation frame - // so the sub-page's first paint settles before the scroll lands. - // The legacy unconditional in-effect scroll fired before the chart - // skeleton had a height; the viewport snapped to 0, then the chart - // popped in and the scroll position visibly jumped again. Wrapping - // in `requestAnimationFrame` runs the scroll after the layout pass - // so the user sees one smooth landing. + // v1.4.33 IW9 — the scroll-to-top affordance moved into the shared + // `useScrollResetOnRoute()` hook so the mother page + sub-page can't + // both fire (which produced a visible double-snap when the chart + // skeleton inflated between the two RAF callbacks). The hook itself + // still defers to `requestAnimationFrame` so the scroll lands after + // first paint, same behaviour the legacy in-line effect provided. const headingRef = useRef(null); + useScrollResetOnRoute(); useEffect(() => { - if (typeof window === "undefined") return; + if (!focusOnMount || typeof window === "undefined") return; const handle = window.requestAnimationFrame(() => { - window.scrollTo({ top: 0, behavior: "auto" }); - if (focusOnMount) { - headingRef.current?.focus({ preventScroll: true }); - } + headingRef.current?.focus({ preventScroll: true }); }); return () => window.cancelAnimationFrame(handle); }, [focusOnMount]); diff --git a/src/hooks/use-scroll-reset-on-route.ts b/src/hooks/use-scroll-reset-on-route.ts new file mode 100644 index 000000000..7cae39724 --- /dev/null +++ b/src/hooks/use-scroll-reset-on-route.ts @@ -0,0 +1,34 @@ +"use client"; + +import { useEffect } from "react"; + +/** + * v1.4.33 IW9 — single source of truth for the "scroll to top on + * route mount" affordance. + * + * The pre-v1.4.33 wiring had `` AND the insights mother + * page each firing their own `requestAnimationFrame → scrollTo({top:0})` + * on mount. Two consumers doing the same trick is the kind of + * duplication that drifts; the second `scrollTo` lands ~16 ms after the + * first and re-snaps the viewport once the chart skeleton has inflated. + * On slow hydrates the user sees a visible "jump" — exactly the + * complaint in `.planning/round-v1433-audit-polish.md` §2.1. + * + * This hook is the single owner of that reset. Both the shell and the + * mother page call into it; the effect is mount-only and uses + * `requestAnimationFrame` so the scroll lands after the first paint + * (chart skeleton heights have settled, no double-snap). + * + * `behavior: "auto"` is intentional — `prefers-reduced-motion` users + * get the same instant reset everyone else does. A smooth scroll on + * a route change is itself a motion that some users opt out of. + */ +export function useScrollResetOnRoute() { + useEffect(() => { + if (typeof window === "undefined") return; + const handle = window.requestAnimationFrame(() => { + window.scrollTo({ top: 0, behavior: "auto" }); + }); + return () => window.cancelAnimationFrame(handle); + }, []); +}