Files
HealthLog/src/components/settings/score-composition-notice.tsx
T
Marc-André Bombeck e4880dcdc1 feat: the score says when a pillar stopped counting
A pillar can leave the set behind the number without anybody choosing it:
a window rolls past its floor, a module goes off, a source goes quiet. The
delta already refuses to narrate that as a health event, which stops a
false "you dropped six points" and puts nothing in its place. The level
moves, the panel shows a different number, and the cause is nowhere on the
surface. Under the adaptive minimum this matters more rather than less,
because compositions are smaller now and sit closer to their floors.

The report carries `compositionNotice` when today's set differs from the
last local day the account has a stored score for. Optional and nullable,
the additive shape the score's wire has used six times, so a payload
cached before it existed stays valid. It names what left and what joined
as pillar ids; the surfaces already own the labels, and the reason a
departed pillar stopped counting comes from that pillar's own row in the
same report rather than from a second vocabulary.

Two changes deliberately do not raise it. A method move and a recipe
change each already have their own note, and telling someone twice about
one event teaches them to ignore both.

The key is `health-score:v<version>:composition:<hash of the sorted set>`
and rides the existing dismissed-item ledger, so a dismissal holds until
the set moves again rather than until tomorrow. The read excludes today on
purpose: the row for today is written by the same request that computed
the report, so comparing against the newest row would find today's own
composition on the second load of the same page and the note would vanish
between two views of it.

This gives `HealthScoreRecord` its first production reader. Since it
shipped it has been written on every scored read and consumed by nothing
but backup and restore, which is the write-only shape the contributor
notes call a liability and which nothing in the gate can notice, because
every check proves one end. The round trip is proved against real
Postgres: raise the note, wipe the table, restore it through the admin
route, get the same note back, and hold the dismissal across it.

Rendering is one muted line on the score card and a card of its own on the
settings page, beside the recipe note rather than inside it. Deliberately
not a push.

Four fake Prisma clients gain the new read. They are doubles of a client
the reader really does call now; leaving them short and guarding the call
site instead would have been a shim for a caller that does not exist.
2026-08-30 16:16:57 +02:00

101 lines
3.3 KiB
TypeScript

"use client";
/**
* "A pillar stopped counting" — the settings half of the composition note.
*
* It sits beside `<ScoreChangeNotice>` rather than inside it because the two
* answer different questions with different remedies. That one says the
* rules or the recipe moved and the person is one tap from the rows that
* decide the recipe. This one says the DATA moved: nobody changed anything,
* a window rolled past its floor or a source went quiet, and the row for
* that pillar on this page already shows what it is waiting for.
*
* Reasons are deliberately not repeated here. The insights panel states
* them beside the number they explain, and the pillar rows below this card
* carry the server's own verdict per pillar; a third copy on this surface
* would be a third place to drift.
*
* Dismissal is per key and the key is the resulting SET, so acknowledging
* this note holds until the set moves again — not until tomorrow.
*/
import { Info, Loader2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import { SettingsCard } from "@/components/settings/settings-card";
import { SettingsCardHeader } from "@/components/settings/_card-header";
import { useTranslations } from "@/lib/i18n/context";
import { SCORE_PILLAR_LABEL_KEYS } from "@/lib/score-config/labels";
import type { ScorePillarId } from "@/lib/analytics/score/types";
export interface ScoreCompositionNoticeProps {
/** Pillars that counted on the last stored day and do not count now. */
left: ScorePillarId[];
/** Pillars that count now and did not on the last stored day. */
joined: ScorePillarId[];
onDismiss: () => void;
dismissing?: boolean;
}
export function ScoreCompositionNotice({
left,
joined,
onDismiss,
dismissing = false,
}: ScoreCompositionNoticeProps) {
const { t } = useTranslations();
function names(ids: ScorePillarId[]): string {
return ids.map((id) => t(SCORE_PILLAR_LABEL_KEYS[id])).join(", ");
}
// Written out rather than assembled from a discriminator so the i18n
// call-site guard can see every key this component can reach.
const leftLine =
left.length > 0
? t("settings.sections.score.notice.composition.left", {
pillars: names(left),
})
: null;
const joinedLine =
joined.length > 0
? t("settings.sections.score.notice.composition.joined", {
pillars: names(joined),
})
: null;
return (
<SettingsCard
as="section"
data-slot="score-composition-notice"
aria-labelledby="score-composition-notice-title"
>
<SettingsCardHeader
icon={Info}
titleId="score-composition-notice-title"
title={t("settings.sections.score.notice.composition.title")}
/>
<p data-slot="score-composition-notice-body" className="text-sm">
{[leftLine, joinedLine].filter(Boolean).join(" ")}
</p>
<Button
type="button"
variant="outline"
size="sm"
onClick={onDismiss}
disabled={dismissing}
data-slot="score-composition-notice-dismiss"
className="min-h-11 sm:min-h-9"
>
{dismissing ? (
<Loader2
aria-hidden="true"
className="size-3.5 animate-spin motion-reduce:animate-none"
/>
) : null}
{t("settings.sections.score.notice.dismiss")}
</Button>
</SettingsCard>
);
}