mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 00:12:18 +00:00
feat(insights): gate sub-pages and tab pills on metric data availability
Brand-new accounts (no manual logs, no Apple Health upload yet) used to
land on chart skeletons over blank target bands when they clicked any
metric tab. Apple Health hides those surfaces entirely until the first
observation lands — match the pattern.
Introduce `src/lib/insights/metric-availability.ts` as the single
decision point. `hasMetricData(metric, inputs)` reads `summaries[METRIC].count`
for sensor metrics (PULSE / WEIGHT / BMI / BLOOD_PRESSURE_* / SLEEP_DURATION
/ VO2_MAX / STEPS / ACTIVE_ENERGY), the `hasMood` / `hasMedication`
booleans for event-driven metrics, and BMI inherits from WEIGHT.
Wire three render gates:
- `<InsightsTabStrip>` filters its pills via the new `availability`
prop. Backward compatible: undefined `availability` keeps every pill.
- `<InsightsLayoutShell>` fetches `/api/analytics` +
`/api/insights/comprehensive` once per route and threads the inputs
into the strip; both queries share their cache keys with sub-page
consumers so the payload lands single-fetch.
- Each `src/app/insights/{slug}/page.tsx` early-returns an
`<EmptyState>` with the right CTA — `/measurements/new` for sensor
metrics, `/mood` for Mood, `/medications` for Medication,
`/settings/data-sources` for Sleep.
Auto-light-up: once the iOS native client uploads its first measurement
the same gate flips on the next refetch — no per-metric feature flag.
References the new `insights.emptyState.<metric>.{title,description,cta}`
translation keys (B6 lands them in messages/*.json):
- insights.emptyState.bloodPressure.{title,description,cta}
- insights.emptyState.weight.{title,description,cta}
- insights.emptyState.pulse.{title,description,cta}
- insights.emptyState.mood.{title,description,cta}
- insights.emptyState.medication.{title,description,cta}
- insights.emptyState.bmi.{title,description,cta}
- insights.emptyState.sleep.{title,description,cta}
This commit is contained in:
1 parent
5364e347f5
commit
f3ff262451
12 files changed
+739
-26
No files matched your search
@@ -0,0 +1,171 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
|
||||
import { hasMetricData } from "../metric-availability";
|
||||
import type { DataSummary } from "@/lib/analytics/trends";
|
||||
|
||||
/**
|
||||
* v1.4.27 F17/F18/F19 — gating helper unit cover.
|
||||
*
|
||||
* The matrix covers each metric × {has data, no data, missing
|
||||
* summary entry, undefined summaries}. The `MOOD` and `MEDICATION`
|
||||
* paths read the boolean flags directly; the rest go through the
|
||||
* `summaries[METRIC].count` lookup.
|
||||
*/
|
||||
|
||||
function fakeSummary(count: number): DataSummary {
|
||||
return {
|
||||
count,
|
||||
latest: count > 0 ? 1 : null,
|
||||
min: null,
|
||||
max: null,
|
||||
mean: null,
|
||||
avg7: null,
|
||||
avg30: null,
|
||||
slope7: null,
|
||||
slope30: null,
|
||||
slope90: null,
|
||||
anomalyCount: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const emptyInputs = {
|
||||
summaries: {},
|
||||
hasMood: false,
|
||||
hasMedication: false,
|
||||
};
|
||||
|
||||
describe("hasMetricData", () => {
|
||||
it("returns false for PULSE when summaries are undefined", () => {
|
||||
expect(
|
||||
hasMetricData("PULSE", {
|
||||
summaries: undefined,
|
||||
hasMood: false,
|
||||
hasMedication: false,
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false for PULSE when the summary entry is missing", () => {
|
||||
expect(hasMetricData("PULSE", emptyInputs)).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false for PULSE when the count is zero", () => {
|
||||
expect(
|
||||
hasMetricData("PULSE", {
|
||||
...emptyInputs,
|
||||
summaries: { PULSE: fakeSummary(0) },
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("returns true for PULSE when the count is one", () => {
|
||||
expect(
|
||||
hasMetricData("PULSE", {
|
||||
...emptyInputs,
|
||||
summaries: { PULSE: fakeSummary(1) },
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true for WEIGHT when the count is positive", () => {
|
||||
expect(
|
||||
hasMetricData("WEIGHT", {
|
||||
...emptyInputs,
|
||||
summaries: { WEIGHT: fakeSummary(42) },
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("derives BMI from WEIGHT count — true when WEIGHT > 0", () => {
|
||||
expect(
|
||||
hasMetricData("BMI", {
|
||||
...emptyInputs,
|
||||
summaries: { WEIGHT: fakeSummary(3) },
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("derives BMI from WEIGHT count — false when WEIGHT is missing", () => {
|
||||
expect(hasMetricData("BMI", emptyInputs)).toBe(false);
|
||||
});
|
||||
|
||||
it("returns true for BLOOD_PRESSURE_SYS independently of DIA", () => {
|
||||
expect(
|
||||
hasMetricData("BLOOD_PRESSURE_SYS", {
|
||||
...emptyInputs,
|
||||
summaries: { BLOOD_PRESSURE_SYS: fakeSummary(5) },
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false for BLOOD_PRESSURE_DIA when only SYS has data", () => {
|
||||
expect(
|
||||
hasMetricData("BLOOD_PRESSURE_DIA", {
|
||||
...emptyInputs,
|
||||
summaries: { BLOOD_PRESSURE_SYS: fakeSummary(5) },
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("returns true for MOOD when hasMood is true", () => {
|
||||
expect(
|
||||
hasMetricData("MOOD", {
|
||||
summaries: {},
|
||||
hasMood: true,
|
||||
hasMedication: false,
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false for MOOD when hasMood is false even with a summary", () => {
|
||||
expect(
|
||||
hasMetricData("MOOD", {
|
||||
summaries: { MOOD: fakeSummary(99) },
|
||||
hasMood: false,
|
||||
hasMedication: false,
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("returns true for MEDICATION when hasMedication is true", () => {
|
||||
expect(
|
||||
hasMetricData("MEDICATION", {
|
||||
summaries: {},
|
||||
hasMood: false,
|
||||
hasMedication: true,
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false for MEDICATION when hasMedication is false", () => {
|
||||
expect(hasMetricData("MEDICATION", emptyInputs)).toBe(false);
|
||||
});
|
||||
|
||||
it("returns true for SLEEP_DURATION when the summary count is positive", () => {
|
||||
expect(
|
||||
hasMetricData("SLEEP_DURATION", {
|
||||
...emptyInputs,
|
||||
summaries: { SLEEP_DURATION: fakeSummary(8) },
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true for VO2_MAX when the summary count is positive", () => {
|
||||
expect(
|
||||
hasMetricData("VO2_MAX", {
|
||||
...emptyInputs,
|
||||
summaries: { VO2_MAX: fakeSummary(2) },
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false for VO2_MAX when summaries are undefined", () => {
|
||||
expect(
|
||||
hasMetricData("VO2_MAX", {
|
||||
summaries: undefined,
|
||||
hasMood: false,
|
||||
hasMedication: false,
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* v1.4.27 F17/F18/F19 — single decision point for "does the user have
|
||||
* any observations on this metric?".
|
||||
*
|
||||
* Three render gates consume this helper:
|
||||
*
|
||||
* 1. The routed Insights sub-pages (`/insights/{slug}/page.tsx`)
|
||||
* early-return an empty-state with a CTA when the gate is false.
|
||||
* 2. The Insights tab strip (`<InsightsTabStrip>`) filters pills so
|
||||
* a metric with zero observations doesn't surface a navigation
|
||||
* target the user can't act on.
|
||||
* 3. The dashboard tile registry (`src/app/page.tsx`) — wired in
|
||||
* bucket B1 — applies the same gate per tile.
|
||||
*
|
||||
* Source signals:
|
||||
*
|
||||
* - Sensor metrics (`PULSE`, `WEIGHT`, `BMI`, `BLOOD_PRESSURE_*`,
|
||||
* `SLEEP_DURATION`, `VO2_MAX`, `STEPS`, `ACTIVE_ENERGY`) read
|
||||
* `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).
|
||||
*
|
||||
* Auto-light-up behaviour: once the iOS native client uploads its
|
||||
* first Apple-Health measurement, `summaries[METRIC].count` flips
|
||||
* from 0 to ≥1 and the next React-Query refetch reveals the matching
|
||||
* pill, sub-page, and dashboard tile — no separate feature flag
|
||||
* needed. (F18.)
|
||||
*/
|
||||
// TODO(B1+B4 reconcile): wire per-tile availability gate using
|
||||
// hasMetricData from src/lib/insights/metric-availability.ts. Bucket
|
||||
// B1 owns `src/app/page.tsx`; this helper is the single decision
|
||||
// point both buckets should call.
|
||||
import type { DataSummary } from "@/lib/analytics/trends";
|
||||
|
||||
/**
|
||||
* Metric keys the gating helper distinguishes. Mirrors the
|
||||
* `MeasurementType` enum for sensor metrics and adds synthetic keys
|
||||
* for `MOOD`, `MEDICATION`, and `BMI` (derived from `WEIGHT`).
|
||||
*
|
||||
* Adding a new metric is a one-place change: list it here, decide
|
||||
* which branch of `hasMetricData` handles it, and the sub-page /
|
||||
* tab strip / dashboard tile gates pick it up automatically.
|
||||
*/
|
||||
export type InsightMetric =
|
||||
| "BLOOD_PRESSURE_SYS"
|
||||
| "BLOOD_PRESSURE_DIA"
|
||||
| "PULSE"
|
||||
| "WEIGHT"
|
||||
| "BMI"
|
||||
| "MOOD"
|
||||
| "MEDICATION"
|
||||
| "SLEEP_DURATION"
|
||||
| "VO2_MAX"
|
||||
| "STEPS"
|
||||
| "ACTIVE_ENERGY";
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
export interface InsightInputs {
|
||||
summaries: Record<string, DataSummary> | undefined;
|
||||
/** Whether the user has logged at least one mood entry. */
|
||||
hasMood: boolean;
|
||||
/** Whether the user has at least one active medication. */
|
||||
hasMedication: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decide whether a metric has enough observations to surface its
|
||||
* sub-page, tab pill, or dashboard tile. Returns `false` for any
|
||||
* unknown branch so a future metric added to `InsightMetric` without
|
||||
* a handler stays hidden until the gate is wired explicitly.
|
||||
*/
|
||||
export function hasMetricData(
|
||||
metric: InsightMetric,
|
||||
inputs: InsightInputs,
|
||||
): boolean {
|
||||
if (metric === "MOOD") return inputs.hasMood;
|
||||
if (metric === "MEDICATION") return inputs.hasMedication;
|
||||
if (metric === "BMI") {
|
||||
// BMI is derived from WEIGHT + the profile height. The chart
|
||||
// mounts even at one weight reading; the gate matches.
|
||||
return (inputs.summaries?.WEIGHT?.count ?? 0) > 0;
|
||||
}
|
||||
return (inputs.summaries?.[metric]?.count ?? 0) > 0;
|
||||
}
|
||||
Reference in new issue
Block a user