Label every ingest source instead of leaking its enum value

Settings, Sources listed GOOGLE_HEALTH, OURA, POLAR and STRAVA by their
internal name: the catalogue was written for seven sources and every
provider added since fell through to the raw value. The measurement list
carried the same fall-through in its source filter and its table column,
and the medication intake history read "via APPLE_HEALTH" for a dose
synced from Apple Health.

The three catalogues move into one module keyed by the enum, so an
omission is a type error rather than a raw value on screen, and the sleep
discrepancy tooltip drops the private brand-name map it had grown to work
around the gap.
This commit is contained in:
Marc-André Bombeck committed 2026-09-02 12:08:31 +02:00
1 parent 72117104d5
commit 0548c0265e
15 files changed
+269 -78

No files matched your search

@@ -10,6 +10,7 @@ import {
} from "@/components/ui/tooltip";
import { useTranslations } from "@/lib/i18n/context";
import { formatDurationMinutes } from "@/lib/i18n/duration";
import { MEASUREMENT_SOURCE_SETTINGS_LABEL_KEYS } from "@/lib/i18n/source-labels";
/**
* v1.28.x — the discreet "sources disagree" marker, extracted from
@@ -30,35 +31,6 @@ export interface SleepSourceDiscrepancyDto {
}[];
}
/**
* Localised label keys per `MeasurementSource` — the same map the
* source-priority settings surface uses (`sources-section.tsx`), so both
* surfaces humanise a source identically.
*/
const SOURCE_LABEL_KEYS: Record<string, string> = {
WITHINGS: "settings.sections.sources.sourceLabels.WITHINGS",
APPLE_HEALTH: "settings.sections.sources.sourceLabels.APPLE_HEALTH",
MANUAL: "settings.sections.sources.sourceLabels.MANUAL",
IMPORT: "settings.sections.sources.sourceLabels.IMPORT",
WHOOP: "settings.sections.sources.sourceLabels.WHOOP",
COMPUTED: "settings.sections.sources.sourceLabels.COMPUTED",
FITBIT: "settings.sections.sources.sourceLabels.FITBIT",
};
/**
* Brand names for sources without a localised settings label — brand
* spellings are locale-invariant, so no i18n key is needed for them.
*/
const SOURCE_BRAND_NAMES: Record<string, string> = {
OURA: "Oura",
POLAR: "Polar",
GOOGLE_HEALTH: "Google Health",
NIGHTSCOUT: "Nightscout",
STRAVA: "Strava",
TELEGRAM: "Telegram",
MCP: "MCP",
};
/**
* Localised device-type label keys (`Measurement.deviceType`), reused from
* the source-priority settings surface. Appended in parentheses so two
@@ -79,8 +51,13 @@ export function sourceDisplayName(
deviceType: string | null,
t: (key: string) => string,
): string {
const labelKey = SOURCE_LABEL_KEYS[source];
const base = labelKey ? t(labelKey) : (SOURCE_BRAND_NAMES[source] ?? source);
// The wire carries whatever the row's `source` column held, so an
// unknown value still has to render as itself rather than as a missing
// key. The catalogue covers every enum member the guard knows about.
const labelKey = MEASUREMENT_SOURCE_SETTINGS_LABEL_KEYS[
source as keyof typeof MEASUREMENT_SOURCE_SETTINGS_LABEL_KEYS
] as string | undefined;
const base = labelKey ? t(labelKey) : source;
const deviceKey = deviceType ? DEVICE_TYPE_LABEL_KEYS[deviceType] : undefined;
return deviceKey ? `${base} (${t(deviceKey)})` : base;
}
@@ -67,6 +67,7 @@ import { createPortal } from "react-dom";
import { toast } from "sonner";
import { formatDateOrRelative, formatDateTime } from "@/lib/format";
import { useTranslations, useFormatters } from "@/lib/i18n/context";
import { MEASUREMENT_SOURCE_LIST_LABEL_KEYS } from "@/lib/i18n/source-labels";
import { CUMULATIVE_DAY_SUM_TYPES } from "@/lib/measurements/cumulative-day-sum";
import { rawDisplayFractionDigits } from "@/lib/measurements/display-transform";
import { getUnitForType } from "@/lib/validations/measurement";
@@ -217,23 +218,22 @@ function toDateTimeLocalValue(isoString: string): string {
}
/**
* Render the `MeasurementSource` enum (`MANUAL` / `WITHINGS` / `IMPORT`
* / `APPLE_HEALTH`) using the existing `measurements.source*`
* translation keys instead of leaking the SCREAMING_SNAKE enum into
* the table cell.
* Render the `MeasurementSource` enum using the `measurements.source*`
* translation keys instead of leaking the SCREAMING_SNAKE enum into the
* table cell or the filter dropdown. The catalogue is keyed by the enum,
* so a provider added without its label is a type error rather than a row
* that reads `GOOGLE_HEALTH`.
*/
function formatMeasurementSource(
source: string,
t: ReturnType<typeof useTranslations>["t"],
) {
if (source === "WITHINGS") return t("measurements.sourceWithings");
if (source === "IMPORT") return t("measurements.sourceImport");
if (source === "MANUAL") return t("measurements.sourceManual");
if (source === "APPLE_HEALTH") return t("measurements.sourceAppleHealth");
if (source === "COMPUTED") return t("measurements.sourceComputed");
if (source === "WHOOP") return t("measurements.sourceWhoop");
if (source === "FITBIT") return t("measurements.sourceFitbit");
return source;
const key = MEASUREMENT_SOURCE_LIST_LABEL_KEYS[
source as keyof typeof MEASUREMENT_SOURCE_LIST_LABEL_KEYS
] as string | undefined;
// A row written by a server the client does not know yet still renders
// as its own value rather than as a missing translation key.
return key ? t(key) : source;
}
/**
@@ -18,6 +18,10 @@ import {
import { useAuth } from "@/hooks/use-auth";
import { useIsMobile } from "@/hooks/use-is-mobile";
import { useTranslations, useFormatters } from "@/lib/i18n/context";
import {
INTAKE_SOURCE_LABEL_KEYS,
type IntakeSourceValue,
} from "@/lib/i18n/source-labels";
import { queryKeys } from "@/lib/query-keys";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
@@ -71,23 +75,22 @@ import { apiGet } from "@/lib/api/api-fetch";
* outline SkipForward + "Übersprungen" for skipped rows. The
* component never labels a row both at once.
* - Source badge surfaces where the intake was logged — WEB → "Web",
* API → "iOS App", REMINDER → "Reminder", IMPORT → "Import" — wrapped
* in a muted "via {origin}" caption next to the status. The labels map
* the coarse `IntakeSource` enum to the practical client per the
* existing `medications.source*` keys; there is no finer client field.
* API → "iOS App", REMINDER → "Reminder", IMPORT → "Import",
* APPLE_HEALTH → "Apple Health" — wrapped in a muted "via {origin}"
* caption next to the status. The labels map the coarse `IntakeSource`
* enum to the practical client per the existing `medications.source*`
* keys; there is no finer client field.
* - Empty state copy + CTA back to the daily intake page.
* - No edit-in-row, no delete buttons.
*/
type IntakeSource = "WEB" | "API" | "REMINDER" | "IMPORT";
export interface IntakeEvent {
id: string;
medicationId: string;
scheduledFor: string;
takenAt: string | null;
skipped: boolean;
source: IntakeSource;
source: IntakeSourceValue;
createdAt: string;
}
@@ -205,13 +208,14 @@ export function IntakeHistoryListV2({
enabled: isAuthenticated,
});
const sourceLabels: Record<IntakeSource, string> = useMemo(
() => ({
WEB: t("medications.sourceWeb"),
API: t("medications.sourceApi"),
REMINDER: t("medications.sourceReminder"),
IMPORT: t("medications.sourceImport"),
}),
const sourceLabels: Record<IntakeSourceValue, string> = useMemo(
() =>
Object.fromEntries(
Object.entries(INTAKE_SOURCE_LABEL_KEYS).map(([source, key]) => [
source,
t(key),
]),
) as Record<IntakeSourceValue, string>,
[t],
);
+2 -14
View File
@@ -48,6 +48,7 @@ import { EmptyState } from "@/components/ui/empty-state";
import { SettingsCard } from "@/components/settings/settings-card";
import { SettingsCardHeader } from "@/components/settings/_card-header";
import { useTranslations } from "@/lib/i18n/context";
import { MEASUREMENT_SOURCE_SETTINGS_LABEL_KEYS } from "@/lib/i18n/source-labels";
import { queryKeys, refetchInactiveDailyReads } from "@/lib/query-keys";
import {
DEFAULT_DEVICE_TYPE_PRIORITY,
@@ -96,19 +97,6 @@ const METRIC_LABEL_KEYS: Record<SourcePriorityMetricKey, string> = {
sleepDebt: "settings.sections.sources.metrics.sleepDebt",
};
const SOURCE_LABEL_KEYS: Record<string, string> = {
WITHINGS: "settings.sections.sources.sourceLabels.WITHINGS",
APPLE_HEALTH: "settings.sections.sources.sourceLabels.APPLE_HEALTH",
MANUAL: "settings.sections.sources.sourceLabels.MANUAL",
IMPORT: "settings.sections.sources.sourceLabels.IMPORT",
// v1.11.0 — WHOOP native source + the COMPUTED proxy (surfaces in the
// `recovery` ladder for native-vs-derived ordering).
WHOOP: "settings.sections.sources.sourceLabels.WHOOP",
COMPUTED: "settings.sections.sources.sourceLabels.COMPUTED",
// v1.12.0 — Fitbit/Pixel native source.
FITBIT: "settings.sections.sources.sourceLabels.FITBIT",
};
const DEVICE_TYPE_LABEL_KEYS: Record<DeviceType, string> = {
watch: "settings.sections.sources.deviceLabels.watch",
band: "settings.sections.sources.deviceLabels.band",
@@ -355,7 +343,7 @@ export function SourcesSection() {
{index + 1}.
</span>
<span className="flex-1 text-sm">
{t(SOURCE_LABEL_KEYS[source] ?? source)}
{t(MEASUREMENT_SOURCE_SETTINGS_LABEL_KEYS[source])}
</span>
{/* v1.4.27 R3d MB2 — stack up/down vertically on
narrow viewports so each button keeps the