mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-14 22:50:51 +00:00
fix(coach): keep stored plans and conversations reachable with the Coach off
The page gate matched /coach/plans and /coach/conversations to the Coach module by the nav prefix, so with the Coach switched off by the operator or hidden by the person both pages were replaced by the module notice and never reached their read-only mode. Both are now stored-record pages the gate leaves alone. With the Coach off nothing in the navigation led there either. Settings now carries a plans card, shown when plans exist, and the conversations card links to the full list.
This commit is contained in:
1 parent
4ae231d757
commit
03ecb5bd52
14 files changed
+265
-31
No files matched your search
@@ -62,7 +62,22 @@ const CONVERSATION = {
|
||||
attachments: [],
|
||||
};
|
||||
|
||||
function client(stored: { facts: unknown[]; conversations: unknown[] }) {
|
||||
type Stored = { facts: unknown[]; conversations: unknown[]; plans?: unknown[] };
|
||||
|
||||
const PLAN = {
|
||||
id: "plan-1",
|
||||
metric: "steps",
|
||||
ifCue: "after lunch",
|
||||
thenAction: "walk ten minutes",
|
||||
target: null,
|
||||
status: "active",
|
||||
reviewDate: null,
|
||||
sourceConversationId: null,
|
||||
createdAt: "2026-09-01T08:00:00.000Z",
|
||||
updatedAt: "2026-09-01T08:00:00.000Z",
|
||||
};
|
||||
|
||||
function client(stored: Stored) {
|
||||
const c = new QueryClient({
|
||||
defaultOptions: { queries: { retry: 0, staleTime: Infinity } },
|
||||
});
|
||||
@@ -71,13 +86,12 @@ function client(stored: { facts: unknown[]; conversations: unknown[] }) {
|
||||
pages: [{ conversations: stored.conversations, nextCursor: null }],
|
||||
pageParams: [null],
|
||||
});
|
||||
if (stored.plans)
|
||||
c.setQueryData(queryKeys.coachPlans("scope:all"), stored.plans);
|
||||
return c;
|
||||
}
|
||||
|
||||
function render(
|
||||
node: React.ReactNode,
|
||||
stored: { facts: unknown[]; conversations: unknown[] },
|
||||
) {
|
||||
function render(node: React.ReactNode, stored: Stored) {
|
||||
return renderToStaticMarkup(
|
||||
<QueryClientProvider client={client(stored)}>
|
||||
<I18nProvider initialLocale="en">{node}</I18nProvider>
|
||||
@@ -142,3 +156,43 @@ describe("<StoredCoachMemory> (Settings → AI while the Coach page is out of re
|
||||
expect(html).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("the way to /coach/plans and /coach/conversations with the Coach off", () => {
|
||||
it("links to the stored plans from Settings → AI when plans exist", () => {
|
||||
const html = render(<StoredCoachMemory isAuthenticated />, {
|
||||
facts: [],
|
||||
conversations: [],
|
||||
plans: [PLAN],
|
||||
});
|
||||
expect(html).toContain('data-testid="settings-coach-plans-card"');
|
||||
expect(html).toContain('href="/coach/plans"');
|
||||
});
|
||||
|
||||
it("links to the stored plans from Settings → Coach with Hide Coach on", () => {
|
||||
authState.disableCoach = true;
|
||||
const html = render(<CoachSection />, {
|
||||
facts: [],
|
||||
conversations: [],
|
||||
plans: [PLAN],
|
||||
});
|
||||
expect(html).toContain('href="/coach/plans"');
|
||||
});
|
||||
|
||||
it("shows no plans card without plans", () => {
|
||||
const html = render(<StoredCoachMemory isAuthenticated />, {
|
||||
facts: [],
|
||||
conversations: [],
|
||||
plans: [],
|
||||
});
|
||||
expect(html).not.toContain("settings-coach-plans-card");
|
||||
});
|
||||
|
||||
it("links to the full conversation list from the conversations card", () => {
|
||||
const html = render(<StoredCoachMemory isAuthenticated />, {
|
||||
facts: [],
|
||||
conversations: [CONVERSATION],
|
||||
});
|
||||
expect(html).toContain('data-slot="settings-coach-conversations-open-all"');
|
||||
expect(html).toContain('href="/coach/conversations"');
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { useMemo, useState } from "react";
|
||||
import Link from "next/link";
|
||||
import { MessagesSquare, Trash2 } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
@@ -8,6 +9,7 @@ import { Button } from "@/components/ui/button";
|
||||
import { QueryErrorRow } from "@/components/ui/query-error-row";
|
||||
import { SettingsCard } from "@/components/settings/settings-card";
|
||||
import { SettingsCardHeader } from "@/components/settings/_card-header";
|
||||
import { SettingsCardActions } from "@/components/settings/_card-actions";
|
||||
import { ConversationTranscript } from "@/components/insights/coach-panel/conversation-transcript";
|
||||
import {
|
||||
useCoachConversationHistory,
|
||||
@@ -26,6 +28,10 @@ import { formatRelativeTime } from "@/lib/i18n/relative-time";
|
||||
* conversation list uses). Reading and deleting never ask the Coach; the
|
||||
* routes behind them are data routes.
|
||||
*
|
||||
* The card links to `/coach/conversations`, which stays reachable and
|
||||
* read-only with the Coach off, so the full list and its search are one step
|
||||
* away even when no navigation entry leads there.
|
||||
*
|
||||
* `hideWhenEmpty`: the card renders nothing when there is nothing stored.
|
||||
* Settings → AI mounts it that way while the Coach is off, so it appears only
|
||||
* when there is something to see.
|
||||
@@ -121,18 +127,35 @@ export function CoachConversationsMemoryCard({
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
{history.hasNextPage ? (
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="min-h-11 self-start sm:min-h-9"
|
||||
disabled={history.isFetchingNextPage}
|
||||
onClick={() => void history.fetchNextPage()}
|
||||
>
|
||||
{t("settings.ai.coachConversations.more")}
|
||||
</Button>
|
||||
) : null}
|
||||
<SettingsCardActions align="start">
|
||||
{history.hasNextPage ? (
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="min-h-11 sm:min-h-9"
|
||||
disabled={history.isFetchingNextPage}
|
||||
onClick={() => void history.fetchNextPage()}
|
||||
>
|
||||
{t("settings.ai.coachConversations.more")}
|
||||
</Button>
|
||||
) : null}
|
||||
{visible.length > 0 ? (
|
||||
<Button
|
||||
asChild
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="min-h-11 sm:min-h-9"
|
||||
>
|
||||
<Link
|
||||
href="/coach/conversations"
|
||||
data-slot="settings-coach-conversations-open-all"
|
||||
>
|
||||
{t("settings.ai.coachConversations.openAll")}
|
||||
</Link>
|
||||
</Button>
|
||||
) : null}
|
||||
</SettingsCardActions>
|
||||
</SettingsCard>
|
||||
);
|
||||
}
|
||||
@@ -53,6 +53,7 @@ import { toast } from "sonner";
|
||||
import { apiDelete, apiGet } from "@/lib/api/api-fetch";
|
||||
import { CoachConversationsMemoryCard } from "@/components/settings/coach-conversations-memory-card";
|
||||
import { CoachRemindersSection } from "@/components/settings/coach-reminders-section";
|
||||
import { CoachPlansMemoryCard } from "@/components/settings/coach-plans-memory-card";
|
||||
|
||||
/** Closed enum mirrored from the server `CoachFact.category` column. */
|
||||
const FACT_CATEGORIES = [
|
||||
@@ -312,6 +313,7 @@ export function StoredCoachMemory({
|
||||
isAuthenticated={isAuthenticated}
|
||||
hideWhenEmpty
|
||||
/>
|
||||
<CoachPlansMemoryCard isAuthenticated={isAuthenticated} />
|
||||
<CoachRemindersSection isAuthenticated={isAuthenticated} hideWhenEmpty />
|
||||
</>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { Target } from "lucide-react";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { SettingsCard } from "@/components/settings/settings-card";
|
||||
import { SettingsCardHeader } from "@/components/settings/_card-header";
|
||||
import { SettingsCardActions } from "@/components/settings/_card-actions";
|
||||
import { useCoachPlans } from "@/hooks/use-coach-plans";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
|
||||
/**
|
||||
* The way to the Coach's stored plans from Settings.
|
||||
*
|
||||
* Plans are the person's own record: `/coach/plans` stays reachable and turns
|
||||
* read-only while the Coach is unavailable, but with the Coach off nothing in
|
||||
* the navigation leads there any more. This card is that way in. It renders
|
||||
* only when plans exist, so a person who never agreed a plan sees nothing.
|
||||
* Reads the same `?scope=all` slot the plans page reads, so the count and the
|
||||
* page cannot disagree.
|
||||
*/
|
||||
export function CoachPlansMemoryCard({
|
||||
isAuthenticated,
|
||||
}: {
|
||||
isAuthenticated: boolean;
|
||||
}) {
|
||||
const { t } = useTranslations();
|
||||
const query = useCoachPlans({
|
||||
filter: { scope: "all" },
|
||||
enabled: isAuthenticated,
|
||||
});
|
||||
const count = query.data?.length ?? 0;
|
||||
if (count === 0) return null;
|
||||
|
||||
return (
|
||||
<SettingsCard
|
||||
as="section"
|
||||
aria-labelledby="settings-coach-plans-title"
|
||||
data-testid="settings-coach-plans-card"
|
||||
>
|
||||
<SettingsCardHeader
|
||||
icon={Target}
|
||||
titleId="settings-coach-plans-title"
|
||||
title={t("settings.ai.coachPlans.title")}
|
||||
description={t("settings.ai.coachPlans.description")}
|
||||
/>
|
||||
<SettingsCardActions align="start">
|
||||
<Button
|
||||
asChild
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="min-h-11 sm:min-h-9"
|
||||
>
|
||||
<Link href="/coach/plans" data-slot="settings-coach-plans-open">
|
||||
{t("settings.ai.coachPlans.open")}
|
||||
</Link>
|
||||
</Button>
|
||||
</SettingsCardActions>
|
||||
</SettingsCard>
|
||||
);
|
||||
}
|
||||
@@ -34,6 +34,7 @@ import { DisableCoachCard } from "@/components/settings/ai/disable-coach-card";
|
||||
import { CoachMemorySection } from "@/components/settings/coach-memory-section";
|
||||
import { CoachConversationsMemoryCard } from "@/components/settings/coach-conversations-memory-card";
|
||||
import { CoachRemindersSection } from "@/components/settings/coach-reminders-section";
|
||||
import { CoachPlansMemoryCard } from "@/components/settings/coach-plans-memory-card";
|
||||
import { CoachNudgeCard } from "@/components/settings/coach-nudge-card";
|
||||
import { CoachPrefsSection } from "@/components/settings/coach-prefs-section";
|
||||
import { useAuth } from "@/hooks/use-auth";
|
||||
@@ -70,6 +71,8 @@ export function CoachSection() {
|
||||
|
||||
<CoachConversationsMemoryCard isAuthenticated={authed} />
|
||||
|
||||
<CoachPlansMemoryCard isAuthenticated={authed} />
|
||||
|
||||
<CoachRemindersSection
|
||||
isAuthenticated={authed}
|
||||
hideWhenEmpty={!coachEnabled}
|
||||
|
||||
Reference in new issue
Block a user