Files
HealthLog/src/hooks/use-chart-overlay-prefs.ts
T
Marc-André BombeckandClaude Opus 4.7 3048dd6637 style(prettier): sweep formatting across the tree
Pre-existing format drift accumulated across v1.4.x; running
prettier brings the whole tree back to baseline so future diffs
read clean. No behaviour change.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 11:05:40 +02:00

116 lines
3.8 KiB
TypeScript

"use client";
import { useCallback, useMemo } from "react";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import {
DEFAULT_CHART_OVERLAY_PREFS,
type ChartOverlayKey,
type ChartOverlayPrefs,
type ChartOverlayPrefsMap,
type DashboardLayout,
} from "@/lib/dashboard-layout";
/**
* v1.4.18 — TanStack Query hook that returns a single chart's overlay
* prefs and a setter that persists them via the new
* `PUT /api/dashboard/chart-overlay-prefs` endpoint.
*
* Reads from the existing dashboard-widgets cache (queryKey
* `["dashboard-layout"]`) so a chart on the dashboard doesn't fire a
* second network request — the layout already lands once on initial
* dashboard render.
*
* The mutation is optimistic: we update the cached layout immediately
* so the chart re-renders with the new toggle state before the network
* round-trip resolves. On error the cache is rolled back to its
* previous value.
*/
export function useChartOverlayPrefs(
chartKey: ChartOverlayKey | null | undefined,
): {
prefs: ChartOverlayPrefs;
setPrefs: (next: ChartOverlayPrefs) => void;
isSaving: boolean;
} {
const queryClient = useQueryClient();
const { data: layout } = useQuery({
queryKey: ["dashboard-layout"],
queryFn: async (): Promise<DashboardLayout> => {
const res = await fetch("/api/dashboard/widgets");
if (!res.ok) throw new Error("Failed to load dashboard layout");
const json = await res.json();
return json.data as DashboardLayout;
},
// Keep the cache warm — the dashboard page mounts a query against
// the same key on first paint, so this hook just piggy-backs.
staleTime: 60_000,
// Skip the fetch entirely when the caller doesn't pass a chartKey
// (mini-mode / ad-hoc chart usage). Saves a wasted GET on pages like
// /insights that mount charts without per-chart persistence.
enabled: Boolean(chartKey),
});
const prefs = useMemo<ChartOverlayPrefs>(() => {
if (!chartKey) return DEFAULT_CHART_OVERLAY_PREFS;
return layout?.chartOverlayPrefs?.[chartKey] ?? DEFAULT_CHART_OVERLAY_PREFS;
}, [layout, chartKey]);
const mutation = useMutation({
mutationFn: async (next: ChartOverlayPrefs): Promise<void> => {
// No-op when no chartKey — the setter is wired up but the hook
// is in "ad-hoc render" mode where overlays don't persist.
if (!chartKey) return;
const res = await fetch("/api/dashboard/chart-overlay-prefs", {
method: "PUT",
headers: { "content-type": "application/json" },
body: JSON.stringify({ chartKey, prefs: next }),
});
if (!res.ok) {
throw new Error("Failed to save chart overlay prefs");
}
},
onMutate: async (next) => {
if (!chartKey) return { previous: undefined };
await queryClient.cancelQueries({ queryKey: ["dashboard-layout"] });
const previous = queryClient.getQueryData<DashboardLayout>([
"dashboard-layout",
]);
if (previous) {
const nextLayout: DashboardLayout = {
...previous,
chartOverlayPrefs: {
...(previous.chartOverlayPrefs ?? {}),
[chartKey]: next,
} as ChartOverlayPrefsMap,
};
queryClient.setQueryData(["dashboard-layout"], nextLayout);
}
return { previous };
},
onError: (_err, _next, ctx) => {
const context = ctx as { previous?: DashboardLayout } | undefined;
if (context?.previous) {
queryClient.setQueryData(["dashboard-layout"], context.previous);
}
},
onSettled: () => {
queryClient.invalidateQueries({ queryKey: ["dashboard-layout"] });
},
});
const setPrefs = useCallback(
(next: ChartOverlayPrefs): void => {
mutation.mutate(next);
},
[mutation],
);
return {
prefs,
setPrefs,
isSaving: mutation.isPending,
};
}