feat(settings): summarise what the Coach sees beside the link to its settings

Settings → Coach pointed at the gear with a muted paragraph. It now says
how many data areas the Coach reads and how far back it looks, from the
saved preferences, next to an Open Coach settings button that lands on
that section.
This commit is contained in:
Marc-André Bombeck committed 2026-10-05 08:11:10 +02:00
1 parent 816269b3ed
commit eda0b5831d
9 files changed
+144 -22

No files matched your search

@@ -0,0 +1,76 @@
/**
* Settings → Coach says what the Coach reads at the saved setting, in one
* sentence, beside the link into the Coach's own settings: how many data
* areas are on, out of how many, and how far back it looks.
*/
import { renderToStaticMarkup } from "react-dom/server";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { describe, expect, it, vi } from "vitest";
vi.mock("@/hooks/use-ai-capability", () => ({
useAiCapability: () => ({
available: true,
reason: null,
onDeviceAllowed: false,
}),
}));
vi.mock("next/navigation", () => ({
useRouter: () => ({ push: vi.fn(), replace: vi.fn() }),
usePathname: () => "/settings/coach",
}));
import { I18nProvider } from "@/lib/i18n/context";
import { queryKeys } from "@/lib/query-keys";
import {
DEFAULT_COACH_CLUSTERS,
DEFAULT_COACH_PREFS,
coachDataClusterEnum,
type CoachPrefs,
} from "@/lib/validations/coach-prefs";
import { CoachPrefsSection } from "../coach-prefs-section";
function summary(prefs: CoachPrefs): string {
const client = new QueryClient({
defaultOptions: { queries: { retry: 0, staleTime: Infinity } },
});
client.setQueryData(queryKeys.coachPrefs(), prefs);
const html = renderToStaticMarkup(
<QueryClientProvider client={client}>
<I18nProvider initialLocale="en">
<CoachPrefsSection isAuthenticated />
</I18nProvider>
</QueryClientProvider>,
);
const match = html.match(
/data-slot="coach-prefs-sources-summary"[^>]*>([^<]*)</,
);
expect(match, "summary rendered").not.toBeNull();
return match![1];
}
const TOTAL = coachDataClusterEnum.options.length;
describe("Settings → Coach sources summary", () => {
it("counts the default areas and says the whole history is read", () => {
expect(summary(DEFAULT_COACH_PREFS)).toBe(
`The Coach sees ${DEFAULT_COACH_CLUSTERS.length} of ${TOTAL} data areas and your whole history.`,
);
});
it("names a bounded lookback in the picker's words", () => {
expect(
summary({
...DEFAULT_COACH_PREFS,
dataClusters: ["cardio", "sleep"],
defaultWindow: "last90days",
}),
).toBe(`The Coach sees 2 of ${TOTAL} data areas and looks back 90 days.`);
expect(
summary({
...DEFAULT_COACH_PREFS,
dataClusters: [],
defaultWindow: "lastYear",
}),
).toBe(`The Coach sees 0 of ${TOTAL} data areas and looks back 12 months.`);
});
});
+54 -15
View File
@@ -41,8 +41,11 @@ import { useTranslations } from "@/lib/i18n/context";
import { useCoachPrefs, useSaveCoachPrefs } from "@/hooks/use-coach-prefs";
import { useAiCapability } from "@/hooks/use-ai-capability";
import { COACH_FOLLOW_UP_UI_KEYS } from "@/lib/ai/coach/dialog-keys";
import { HISTORY_REACH_DAYS, lookbackText } from "@/lib/ai/coach/history-reach";
import {
DEFAULT_COACH_CLUSTERS,
DEFAULT_COACH_PREFS,
coachDataClusterEnum,
type CoachExcludeMetric,
type CoachPrefs,
type CoachTone,
@@ -70,6 +73,32 @@ const CONTEXT_OPTIONS: CoachExcludeMetric[] = [
"anthropometrics",
];
/**
* One sentence on what the Coach reads at the saved setting: how many of the
* data areas are on and how far back it looks, in the words the lookback
* picker uses.
*/
function sourcesSummary(
prefs: CoachPrefs,
t: ReturnType<typeof useTranslations>["t"],
): string {
const params = {
count: (prefs.dataClusters ?? DEFAULT_COACH_CLUSTERS).length,
total: coachDataClusterEnum.options.length,
};
const window = prefs.defaultWindow;
if (HISTORY_REACH_DAYS[window] === null) {
return t("insights.coach.settingsSourcesSummaryAll", params);
}
return t("insights.coach.settingsSourcesSummary", {
...params,
window: t(
`insights.coach.lookback.option.${window}`,
lookbackText(window).optionParams,
),
});
}
export interface CoachPrefsSectionProps {
isAuthenticated: boolean;
}
@@ -225,25 +254,35 @@ export function CoachPrefsSection({ isAuthenticated }: CoachPrefsSectionProps) {
{/* Data clusters + analysis window live on ONE owner: "What I
can see" behind the Coach's settings gear, which
`/coach?settings=data` opens directly. This card
used to render its own copies of both controls; two writable
surfaces for the same persisted fields is exactly the
redundancy the rail was built to end. A pointer replaces
them. Only while the Coach can open: without it the link
would bounce back to Insights and the rail is out of reach. */}
`/coach?settings=data` opens directly. This card used to render
its own copies of both controls; two writable surfaces for the
same persisted fields is exactly the redundancy the rail was
built to end. It says what the Coach sees at the saved setting
and links there. Only while the Coach can open: without it the
link would bounce back to Insights and the rail is out of
reach. */}
{coach.available ? (
<p
<div
data-slot="coach-prefs-sources-pointer"
className="text-muted-foreground border-border/60 rounded-md border border-dashed px-3 py-2 text-xs leading-relaxed"
className="flex items-start justify-between gap-3"
>
{t("insights.coach.settingsSourcesPointer")}{" "}
<Link
href="/coach?settings=data"
className="text-foreground underline underline-offset-2"
<p
data-slot="coach-prefs-sources-summary"
className="min-w-0 flex-1 text-sm"
>
{t("insights.coach.settingsSourcesPointerLink")}
</Link>
</p>
{sourcesSummary(draft, t)}
</p>
<Button
asChild
variant="outline"
size="sm"
className="min-h-11 shrink-0 sm:min-h-9"
>
<Link href="/coach?settings=data">
{t("insights.coach.settingsSourcesPointerLink")}
</Link>
</Button>
</div>
) : null}
{/* Exclude metrics — checked = excluded. */}