Files
HealthLog/src/hooks/use-coach-prefs.ts
T
Marc-André BombeckandClaude Opus 4.7 dfffe6bb3b refactor: simplify batch (S-02 to S-04)
Three small reuse wins from the W6 simplify review:

- openapi/registry.ts swaps the lazy require() for a static import.
  The ESLint disable + as-cast carried no real benefit; the only
  callers always loaded the route table anyway.
- Extract useCoachPrefs() into a shared hook so the settings sheet
  and message thread share one queryKey + one fallback shape. Removes
  the drift between the two existing inline blocks.
- Replace the 9-deep wantsX boolean ladder in coach snapshot with a
  single CoachScopeSource -> MeasurementType[] map. Drives both the
  WHERE type IN (...) build and the appleHealthBlocks gate, so adding
  a new metric is one entry instead of three parallel edits.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 02:23:43 +02:00

37 lines
1.3 KiB
TypeScript

"use client";
import { useQuery } from "@tanstack/react-query";
import {
DEFAULT_COACH_PREFS,
type CoachPrefs,
} from "@/lib/validations/coach-prefs";
/**
* v1.4.23 W6 (S-03) — shared accessor for the per-user Coach
* preferences row. Both the Coach drawer's settings sheet and the
* message-thread evidence-disclosure default consume the same
* `/api/auth/me/coach-prefs` payload; before the extraction the two
* call sites duplicated the same `useQuery` block with subtly
* different fallback semantics (one threw on `!ok`, the other returned
* defaults). The hook centralises the cache key, the envelope unwrap,
* and the "treat fetch failure as defaults" stance so the next surface
* (insights cog, settings tab) inherits a single source of truth.
*
* The query is gated on `enabled` so the settings sheet can defer the
* fetch until the sheet actually opens. Callers that always want the
* row (message thread) leave it unset (defaults to true).
*/
export function useCoachPrefs(opts?: { enabled?: boolean }) {
return useQuery<CoachPrefs>({
queryKey: ["coach-prefs"],
queryFn: async () => {
const res = await fetch("/api/auth/me/coach-prefs");
if (!res.ok) return DEFAULT_COACH_PREFS;
const env = (await res.json()) as { data: CoachPrefs };
return env.data;
},
enabled: opts?.enabled,
});
}