mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 23:33:29 +00:00
Replaces the inline 790-line page.tsx composition with the W3e
primitives extracted in the previous phase. The page is now ~270
lines of orchestration:
- TanStack-Query pulls the redesigned targets payload (with the
v1.4.25 consistency + summary additions).
- A second query reads /api/insights/provider-chain to derive
`aiEnabled = chainStatus?.activeProvider != null`. The flag threads
down into every <TargetCard>, which uses it to gate the per-card
Coach CTA — no broken-button state when the user has no provider
configured.
- A new `useCoachHandoff()` hook (src/hooks/use-coach-handoff.ts)
owns the drawer's open + prefill + scope triple. Per-card buttons
call `askCoach()`; the hook flips state and the drawer mounts
inline at the page level.
- Cards now render in a fixed order (BP / BP-in-target / WEIGHT /
PULSE / BMI / MOOD / COMPLIANCE / SLEEP / BODY_FAT / STEPS) — Marc
directive: stable visual hierarchy beats status-sorting because
the user's eye lands on the same metric in the same place every
visit.
- Source-citation URLs stay on the page (declarative routing) and
thread into <TargetCard> via the `sourceLink` prop.
The old inline TargetCard / RangeBar / TrendIcon / status-category
map are deleted along with the dead `Badge` + lucide imports.
Existing targets-i18n + targets-spacing tests updated to mock the
new useQueryClient + useMutation surfaces that the embedded
<CoachDrawer> needs at SSR; both suites still assert the same
visual contracts and pass.
Wave 2 deliverables also folded into this commit:
- pageSummary mounts via the W3e header above the grid
- consistency strip renders only when insufficientData === false
- recency caption ("last met goal: 2 days ago") renders only when
lastMetGoalAt !== null
- streak chip ("4-day streak") renders only when streakDays ≥ 3
53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useState } from "react";
|
|
|
|
import type { CoachScope } from "@/lib/ai/coach/types";
|
|
|
|
/**
|
|
* v1.4.25 W3e — controlled state for the Coach drawer when mounted
|
|
* outside the `/insights` page (currently `/targets`, future
|
|
* `/insights/<metric>` sub-pages).
|
|
*
|
|
* Encapsulates the open / prefill / scope triple so each new surface
|
|
* only has to mount `<CoachDrawer>` and wire the page-level CTAs to
|
|
* the returned `askCoach()` callback. The drawer itself owns no
|
|
* cross-surface state; this hook is the binding point.
|
|
*
|
|
* Reset behaviour mirrors the v1.4.20 Insights pattern: closing the
|
|
* drawer clears prefill so the next open starts blank unless a CTA
|
|
* supplies a fresh prefill.
|
|
*/
|
|
export interface CoachHandoffApi {
|
|
coachOpen: boolean;
|
|
setCoachOpen: (next: boolean) => void;
|
|
coachPrefill: string | null;
|
|
coachScope: CoachScope | null;
|
|
askCoach: (payload: { prefill: string; scope: CoachScope }) => void;
|
|
}
|
|
|
|
export function useCoachHandoff(): CoachHandoffApi {
|
|
const [coachOpen, setOpen] = useState(false);
|
|
const [coachPrefill, setPrefill] = useState<string | null>(null);
|
|
const [coachScope, setScope] = useState<CoachScope | null>(null);
|
|
|
|
const setCoachOpen = useCallback((next: boolean) => {
|
|
setOpen(next);
|
|
if (!next) {
|
|
setPrefill(null);
|
|
setScope(null);
|
|
}
|
|
}, []);
|
|
|
|
const askCoach = useCallback(
|
|
({ prefill, scope }: { prefill: string; scope: CoachScope }) => {
|
|
setPrefill(prefill);
|
|
setScope(scope);
|
|
setOpen(true);
|
|
},
|
|
[],
|
|
);
|
|
|
|
return { coachOpen, setCoachOpen, coachPrefill, coachScope, askCoach };
|
|
}
|