Files
HealthLog/src/components/charts/chart-overlay-controls.tsx
T
Marc-André Bombeck 45363edb77 Withhold the controls a shared record refuses
Nine surfaces offered a delegate an action the server declines, and the
switch had never been asked about any of them.

The Today rail's dismiss and the coach check-in's keep / let-go write
through routes that resolve the caller, so both refuse under a switch;
they now ask canManage and are absent at either grant level. A priority
action with no href and no handler is no longer rendered as an inert
button at all — the type carries that rule now.

The Vorsorge dashboard card offered the mark-done that /checkups already
withholds, and the checkups list branch inlined its own ungated copy of
the button the cards branch gates, so which view a browser last chose
decided whether the action appeared. Both bind canManage.

The chart overlay cog persists through a route that resolves the caller
and stores a preference belonging to the person rather than the record.
Gated once in the control so the three chart wrappers cannot drift.

Three query parameters opened sheets past a gated button: ?add=<TYPE> on
measurements, ?new=1 on medications, and ?edit=1 on a medication detail,
the last of which opens a wizard refused at both levels. A deep link is
the same affordance as the control that produces it.

The efficacy retarget dial rewrites a setting the owner chose. The
episode documents card offered a link and an upload the vault gates on
the same endpoint throughout. And the dose ledger built its own success
toast, missing both the Undo suppression and the "saved to" receipt its
two siblings already carry — the one a delegate met on every dose.

The Coach drawer is not mounted inside a shared record, so every button
calling askCoach() opened nothing. The launch provider now publishes no
value there and each entry point's existing null check does the rest;
the documents sheet, the one that rendered without checking, now checks.
2026-08-03 18:33:48 +02:00

287 lines
10 KiB
TypeScript

"use client";
import { useId, type ReactElement } from "react";
import { Settings2 } from "lucide-react";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Switch } from "@/components/ui/switch";
import { Label } from "@/components/ui/label";
import { Button } from "@/components/ui/button";
import { useRecordCapabilities } from "@/hooks/use-record-capabilities";
import { useTranslations } from "@/lib/i18n/context";
import {
COMPARISON_BASELINES,
type ChartOverlayPrefs,
type ComparisonBaseline,
} from "@/lib/dashboard-layout";
/**
* v1.4.18 — per-chart overlay-controls popover.
*
* The maintainer rolled back B1a's always-on chart overlays (gradient fills,
* personal-baseline reference line, target-zone shading) and asked for
* a per-chart switch surface instead. Each dashboard chart card mounts
* one of these in its top-right corner (a settings-cog dropdown) so the
* user can flip the three overlays on or off for that chart only,
* without affecting any other chart.
*
* Three toggles, each independent:
* - showTrendIndicator — the "7-day trend" overlay (moving average +
* numeric Δ chip in the header)
* - showTrendArrow — the trend regression line + arrow direction
* hint + personal-baseline reference line. the maintainer's rule: the mean
* line only paints when a trend is being displayed, so we fold
* the baseline into this toggle.
* - showTargetRange — the target-zone shading + reference lines
* (BP healthy band, BMI healthy band, medication 80 % / 100 %
* thresholds, etc.).
*
* Default state for every toggle is OFF — clean line is the new
* default and overlays are user-opt-in.
*
* The component is controlled — the parent owns the state, a render
* happens on every change. Persistence (TanStack Query → user-pref API
* → User.dashboardWidgetsJson.chartOverlayPrefs) is wired up by the
* chart wrapper that mounts this; the controls component itself is
* pure UI.
*/
/**
* Re-export the canonical type+default from `@/lib/dashboard-layout`
* so chart-side callers can keep importing them from this module.
* Single source of truth lives in dashboard-layout (the persistence
* layer); this module is just the UI.
*/
export {
DEFAULT_CHART_OVERLAY_PREFS,
type ChartOverlayPrefs,
} from "@/lib/dashboard-layout";
export interface ChartOverlayControlsProps {
prefs: ChartOverlayPrefs;
onChange: (next: ChartOverlayPrefs) => void;
/**
* Optional CSS class for the trigger button — letting the chart
* wrapper align the cog with its title row.
*/
triggerClassName?: string;
/**
* v1.4.25 W3f — when the parent chart already knows the prior period
* carries no overlay-able rows, it threads `hasComparisonData=false`
* here so the comparison-baseline buttons render in a visibly
* disabled state (reduced opacity + `aria-disabled` + tooltip)
* instead of letting the user pick a value that paints nothing on
* the chart.
*
* Default `true` (i.e. enabled) so charts that never compute the
* flag (or compute it asynchronously) keep their existing behaviour.
*/
hasComparisonData?: boolean;
}
export function ChartOverlayControls({
prefs,
onChange,
triggerClassName,
hasComparisonData = true,
}: ChartOverlayControlsProps): ReactElement | null {
const { t } = useTranslations();
// The cog is gated here rather than at each of the eight charts that mount
// it, so the three chart wrappers cannot drift apart on the same question.
// Every toggle persists through `PUT /api/dashboard/chart-overlay-prefs`,
// which resolves the caller and refuses under a switch; and the preference
// belongs to the person reading the chart, not to the record — the same
// reasoning that keeps the mood tag layout off the delegable list. So a
// delegate reads the owner's chart in its stored shape and is not offered a
// dial that would silently fail.
const { canManage } = useRecordCapabilities();
if (!canManage) return null;
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
type="button"
variant="ghost"
size="default"
className={`text-muted-foreground hover:text-foreground min-h-11 min-w-11 px-0 sm:min-h-9 sm:min-w-9 ${triggerClassName ?? ""}`.trim()}
aria-label={t("chart.overlay.controls.tooltip.openSettings")}
data-slot="chart-overlay-controls-trigger"
>
<Settings2 className="h-3.5 w-3.5" aria-hidden="true" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent
align="end"
sideOffset={4}
className="w-[240px] p-3"
data-slot="chart-overlay-controls-content"
>
<ChartOverlayControlsBody
prefs={prefs}
onChange={onChange}
hasComparisonData={hasComparisonData}
/>
</DropdownMenuContent>
</DropdownMenu>
);
}
/**
* v1.4.25 W3f — the dropdown body is extracted so unit tests can render
* it without spinning up Radix's portalled popover (which is invisible
* to `renderToStaticMarkup`). Production callers always go through
* `<ChartOverlayControls>` above; the body export is purely a test
* affordance + a future hook for in-line (non-popover) renders.
*/
export interface ChartOverlayControlsBodyProps {
prefs: ChartOverlayPrefs;
onChange: (next: ChartOverlayPrefs) => void;
hasComparisonData?: boolean;
}
export function ChartOverlayControlsBody({
prefs,
onChange,
hasComparisonData = true,
}: ChartOverlayControlsBodyProps): ReactElement {
const { t } = useTranslations();
const trendIndicatorId = useId();
const trendArrowId = useId();
const targetRangeId = useId();
// v1.4.25 W3f — when the underlying prior-period series is empty for
// BOTH lastMonth and lastYear (we only know about the currently
// selected baseline), grey out the non-"none" buttons. We can only
// verify the active baseline truthfully, so the disabled state is
// gated on `comparisonBaseline !== "none" && !hasComparisonData`:
// the user has already opted in to comparing and we discovered the
// prior period is empty. From the "none" baseline we keep all
// buttons enabled so the user can still toggle in for the chart to
// re-evaluate.
const comparisonGreyOut =
prefs.comparisonBaseline !== "none" && !hasComparisonData;
const disabledTooltip = t("chart.overlay.controls.comparisonUnavailable");
const setToggle = (
key: "showTrendIndicator" | "showTrendArrow" | "showTargetRange",
value: boolean,
): void => {
onChange({ ...prefs, [key]: value });
};
const setComparisonBaseline = (value: ComparisonBaseline): void => {
onChange({ ...prefs, comparisonBaseline: value });
};
return (
<>
<DropdownMenuLabel className="text-xs font-medium tracking-wide uppercase">
{t("chart.overlay.controls.title")}
</DropdownMenuLabel>
<DropdownMenuSeparator />
<div className="flex flex-col gap-2.5 pt-1">
<div className="flex items-center justify-between gap-3">
<Label
htmlFor={trendIndicatorId}
className="cursor-pointer text-xs font-normal"
>
{t("chart.overlay.controls.trendIndicator")}
</Label>
<Switch
id={trendIndicatorId}
checked={prefs.showTrendIndicator}
onCheckedChange={(value) =>
setToggle("showTrendIndicator", Boolean(value))
}
data-slot="chart-overlay-toggle-trend-indicator"
/>
</div>
<div className="flex items-center justify-between gap-3">
<Label
htmlFor={trendArrowId}
className="cursor-pointer text-xs font-normal"
>
{t("chart.overlay.controls.trendArrow")}
</Label>
<Switch
id={trendArrowId}
checked={prefs.showTrendArrow}
onCheckedChange={(value) =>
setToggle("showTrendArrow", Boolean(value))
}
data-slot="chart-overlay-toggle-trend-arrow"
/>
</div>
<div className="flex items-center justify-between gap-3">
<Label
htmlFor={targetRangeId}
className="cursor-pointer text-xs font-normal"
>
{t("chart.overlay.controls.targetRange")}
</Label>
<Switch
id={targetRangeId}
checked={prefs.showTargetRange}
onCheckedChange={(value) =>
setToggle("showTargetRange", Boolean(value))
}
data-slot="chart-overlay-toggle-target-range"
/>
</div>
</div>
<DropdownMenuSeparator />
<div className="flex flex-col gap-1.5 pt-1">
<Label className="text-muted-foreground text-xs font-medium">
{t("chart.overlay.controls.comparisonBaseline")}
</Label>
<div
className="grid grid-cols-3 gap-1"
data-slot="chart-overlay-comparison-baseline"
data-comparison-disabled={comparisonGreyOut ? "true" : undefined}
>
{COMPARISON_BASELINES.map((value) => {
// The "none" choice never greys out — it's the safe
// escape hatch when prior data is missing.
const isNoneOption = value === "none";
const isGreyed = comparisonGreyOut && !isNoneOption;
return (
<Button
key={value}
type="button"
variant={
prefs.comparisonBaseline === value ? "default" : "outline"
}
size="sm"
className={`min-h-11 px-2 text-xs sm:min-h-9 ${isGreyed ? "opacity-50" : ""}`.trim()}
onClick={() => setComparisonBaseline(value)}
aria-pressed={prefs.comparisonBaseline === value}
aria-disabled={isGreyed || undefined}
title={isGreyed ? disabledTooltip : undefined}
data-slot={`chart-overlay-comparison-${value}`}
data-comparison-greyed={isGreyed ? "true" : undefined}
>
{t(`comparison.baseline.${value}`)}
</Button>
);
})}
</div>
{comparisonGreyOut && (
<p
className="text-muted-foreground text-xs leading-snug"
data-slot="chart-overlay-comparison-unavailable-hint"
>
{disabledTooltip}
</p>
)}
</div>
</>
);
}