fix(insights): gate the tab strip on one presence read

The tab strip on every Insights page read the whole overview (22 KB),
a workouts page, the ECG list and the analytics summaries to learn which
pills to show. GET /api/insights/presence answers just that, for the
record on screen: the reading count per type after the record's module
switches, and whether it has mood entries in the last 90 days, a tracked
scheduled medication, workouts and ECG recordings. A flag whose page a
switched-off module hides is false and its rows are not read. The strip
now reads this and the saved layout; each page keeps loading its own data.
This commit is contained in:
Marc-André Bombeck committed 2026-11-12 12:56:16 +01:00
1 parent 05b5eb6942
commit 367fcd5332
12 files changed
+497 -129

No files matched your search

+8 -10
View File
@@ -19,9 +19,8 @@
* `summaries[METRIC].count` from the analytics endpoint.
* - `BMI` is derived from `WEIGHT` so it inherits the weight count.
* - Event-driven metrics (`MOOD`, `MEDICATION`) read the boolean
* `hasMood` / `hasMedication` flags the caller threads in from
* `/api/insights/comprehensive` (mood summary count > 0,
* medications array length > 0).
* `hasMood` / `hasMedication` flags the caller threads in (the tab
* strip from `/api/insights/presence`).
*
* Auto-light-up behaviour: once the iOS native client uploads its
* first Apple-Health measurement, `summaries[METRIC].count` flips
@@ -29,7 +28,6 @@
* pill, sub-page, and dashboard tile — no separate feature flag
* needed. (F18.)
*/
import type { DataSummary } from "@/lib/analytics/trends";
/**
* Metric keys the gating helper distinguishes. Mirrors the
@@ -133,14 +131,14 @@ export type InsightMetric =
| "NUTRIENTS";
/**
* Inputs the gating helper consumes. The `summaries` shape mirrors
* the `/api/analytics` response (`Record<MeasurementType, DataSummary>`)
* — undefined means the caller hasn't loaded analytics yet, which
* the helper treats as "not available" so loading shells aren't
* accidentally lit up.
* Inputs the gating helper consumes. `summaries` is keyed by
* `MeasurementType` and only its `count` is read, so an analytics
* summaries map and the presence read's `metrics` both fit. Undefined
* means the caller hasn't loaded it yet, which the helper treats as
* "not available" so loading shells aren't accidentally lit up.
*/
export interface InsightInputs {
summaries: Record<string, DataSummary> | undefined;
summaries: Readonly<Record<string, { count: number }>> | undefined;
/** Whether the user has logged at least one mood entry. */
hasMood: boolean;
/** Whether the user has at least one active medication. */
+24
View File
@@ -22,6 +22,7 @@ import {
import {
insightsCardsResponse,
insightsComprehensiveResponse,
insightsPresenceResponse,
metricStatusQuery,
metricStatusResponse,
biomarkerAssessmentQuery,
@@ -126,6 +127,29 @@ export const insightsPaths: NonNullable<ZodOpenApiObject["paths"]> = {
},
},
},
"/api/insights/presence": {
get: {
tags: ["Insights"],
summary: "Which Insights pages have data",
description:
"What the Insights tab strip gates its pills on, for the record on screen: the reading count per measurement type (after the record's module switches) and whether the record has mood entries in the last 90 days, a tracked scheduled medication, workouts and ECG recordings. A flag whose module is off is false. Counts and flags only; each page reads its own data. No provider is called and no AI switch refuses it. Delegable at MANAGE level over the whole record. Cookie or Bearer auth.",
responses: {
"200": {
description: "The presence answer.",
content: {
"application/json": {
schema: dataEnvelope(
insightsPresenceResponse,
"InsightsPresenceEnvelope",
),
},
},
},
...stdResponses,
...recordRefusal(),
},
},
},
"/api/insights/cards": {
get: {
tags: ["Insights"],
@@ -1691,6 +1691,36 @@ export const ecgListResponse = z
"Metadata-only ECG recording list. Never carries the waveform — the per-recording strip is fetched on demand from GET /api/insights/ecg/{id}. Read-only; no LLM call. Reflects only the device's own classification, never a HealthLog interpretation.",
});
export const insightsPresenceResponse = z
.object({
metrics: z
.record(z.string(), z.object({ count: z.number().int() }))
.describe(
"Keyed by `MeasurementType`: the reading count of every type with at least one reading, after the record's module switches. The same counts `GET /api/analytics?slice=summaries` reports.",
),
hasMood: z
.boolean()
.describe(
"A mood entry in the last 90 days. False while the mood module is off.",
),
hasMedication: z
.boolean()
.describe(
"An active medication with a schedule whose intake is tracked. False while the medications module is off.",
),
hasWorkouts: z
.boolean()
.describe(
"At least one workout. False while the workouts module is off.",
),
hasEcgRecordings: z.boolean().describe("At least one ECG recording."),
})
.meta({
id: "InsightsPresence",
description:
"Which Insights pages have something to show for the record: what the tab strip gates its pills on. Counts and flags only, no values.",
});
export const ecgDetailQuery = z
.object({
full: z
+2
View File
@@ -6,6 +6,8 @@
export const insightsKeys = {
insightsRoot: () => ["insights"] as const,
insightsComprehensive: () => ["insights", "comprehensive"] as const,
/** Which Insights pages have data (`/api/insights/presence`), for the tab strip. */
insightsPresence: () => ["insights", "presence"] as const,
insightsTargets: () => ["insights", "targets"] as const,
/**
* Shared cache key for the rich `/api/insights/generate` advisor