mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 07:42:23 +00:00
The workout list, the workout detail header and the dashboard's recent workouts tile each rebuilt their own date formatter from the shared cache, with a private copy of the current-year check, and so ignored the reader's date-order preference. They now use useFormatters(), and the copies are gone. The timezone formatter guard now refuses any two-argument getDateTimeFormat call in src/components, so a component cannot build its own display formatter again. The medication card's day bucketing, the one other such call, reads userDayKey instead.
199 lines
7.6 KiB
TypeScript
199 lines
7.6 KiB
TypeScript
"use client";
|
|
|
|
import { DayLinkAt } from "@/components/day/day-link";
|
|
import Link from "next/link";
|
|
import { HeartPulse, Loader2, Map as MapIcon } from "lucide-react";
|
|
|
|
import { cn } from "@/lib/utils";
|
|
import { getNumberFormat } from "@/lib/intl/formatter-cache";
|
|
import { formatDurationMinutes } from "@/lib/i18n/duration";
|
|
import { useFormatters, useTranslations } from "@/lib/i18n/context";
|
|
import { iconForSport } from "@/lib/workouts/sport-icons";
|
|
import type { WorkoutListEntry } from "@/hooks/use-workouts";
|
|
import { Button } from "@/components/ui/button";
|
|
import { useUnitDisplay } from "@/hooks/use-unit-display";
|
|
import { formatDistance } from "@/components/insights/workout-detail/format";
|
|
|
|
/**
|
|
* v1.4.32 — workout list row primitive.
|
|
*
|
|
* Renders the canonical workouts list returned by `GET /api/workouts`.
|
|
* Each row links to `/insights/workouts/[id]`. The visual shape mirrors
|
|
* the dashboard tile primitives — single-line metadata strip with the
|
|
* sport icon, the date, the duration, and (when present) the distance
|
|
* + active-energy chips. The Apple Watch + Withings cross-source merge
|
|
* runs server-side, so the list is already deduped by the time it
|
|
* lands here.
|
|
*/
|
|
|
|
function formatEnergy(kcal: number, locale: string): string {
|
|
return getNumberFormat(locale, {
|
|
maximumFractionDigits: 0,
|
|
}).format(Math.round(kcal));
|
|
}
|
|
|
|
export interface WorkoutListProps {
|
|
workouts: WorkoutListEntry[];
|
|
className?: string;
|
|
hasNextPage?: boolean;
|
|
isFetchingNextPage?: boolean;
|
|
isFetchNextPageError?: boolean;
|
|
onLoadMore?: () => void;
|
|
}
|
|
|
|
export function WorkoutList({
|
|
workouts,
|
|
className,
|
|
hasNextPage = false,
|
|
isFetchingNextPage = false,
|
|
isFetchNextPageError = false,
|
|
onLoadMore,
|
|
}: WorkoutListProps) {
|
|
const { t, locale } = useTranslations();
|
|
// The reader's date order, hour cycle and zone, as everywhere else; the
|
|
// year shows whenever the workout is not from this year.
|
|
const fmt = useFormatters();
|
|
const { preference } = useUnitDisplay();
|
|
|
|
return (
|
|
<ul
|
|
data-slot="workout-list"
|
|
className={cn("divide-border divide-y rounded-lg border", className)}
|
|
>
|
|
{workouts.map((workout) => {
|
|
const Icon = iconForSport(workout.sportType);
|
|
const sportLabelKey = `insights.workouts.sport.${workout.sportType}`;
|
|
const sportLabel = t(sportLabelKey);
|
|
// Translation keys missing from one locale fall back to the
|
|
// canonical sport-type string so a new HK enum value never
|
|
// renders the raw `insights.workouts.sport.x` placeholder.
|
|
const sportName =
|
|
sportLabel === sportLabelKey ? workout.sportType : sportLabel;
|
|
|
|
return (
|
|
// v1.42 — the row opens the workout and its date opens the day:
|
|
// two targets, so the row is a stretched link (the sport name's
|
|
// link covers the row) and the date sits above it.
|
|
<li key={workout.id} className="relative">
|
|
<div
|
|
data-slot="workout-list-row"
|
|
className={cn(
|
|
"flex items-center gap-3 px-3 py-3 text-sm transition-colors",
|
|
"hover:bg-accent has-[a[data-slot=workout-list-link]:focus-visible]:ring-ring/50 has-[a[data-slot=workout-list-link]:focus-visible]:ring-2",
|
|
)}
|
|
>
|
|
<span
|
|
aria-hidden="true"
|
|
className="bg-muted text-muted-foreground flex size-9 shrink-0 items-center justify-center rounded-full"
|
|
>
|
|
<Icon className="size-4" />
|
|
</span>
|
|
<div className="flex min-w-0 flex-1 flex-col gap-0.5">
|
|
<span className="flex items-center gap-1.5 truncate font-medium">
|
|
<Link
|
|
href={`/insights/workouts/${encodeURIComponent(workout.id)}`}
|
|
data-slot="workout-list-link"
|
|
className="truncate after:absolute after:inset-0 after:content-[''] focus-visible:outline-none"
|
|
>
|
|
{sportName}
|
|
</Link>
|
|
{/* #67 — discreet glyphs flag which sessions open rich
|
|
(map / HR curve). Muted so they never compete with
|
|
the sport name. */}
|
|
{workout.hasRoute ? (
|
|
<MapIcon
|
|
className="text-muted-foreground size-3.5 shrink-0"
|
|
aria-hidden="true"
|
|
data-slot="workout-list-glyph-route"
|
|
/>
|
|
) : null}
|
|
{workout.hasHrSeries ? (
|
|
<HeartPulse
|
|
className="text-muted-foreground size-3.5 shrink-0"
|
|
aria-hidden="true"
|
|
data-slot="workout-list-glyph-hr"
|
|
/>
|
|
) : null}
|
|
</span>
|
|
{/* Wraps rather than truncates, so the day it opens
|
|
stays readable beside the stats column. */}
|
|
<span className="text-muted-foreground text-xs">
|
|
<DayLinkAt at={workout.startedAt} className="z-10">
|
|
{`${fmt.dateWithWeekdaySmart(workout.startedAt)}, ${fmt.time(workout.startedAt)}`}
|
|
</DayLinkAt>
|
|
</span>
|
|
</div>
|
|
<div className="flex flex-col items-end gap-0.5 text-xs">
|
|
<span className="font-medium tabular-nums">
|
|
{formatDurationMinutes(
|
|
Math.round(workout.durationSec / 60),
|
|
t,
|
|
)}
|
|
</span>
|
|
<span className="text-muted-foreground flex flex-wrap justify-end gap-x-2 tabular-nums">
|
|
{workout.distanceM != null ? (
|
|
<span data-slot="workout-list-distance">
|
|
{formatDistance(workout.distanceM, locale, preference)}
|
|
</span>
|
|
) : null}
|
|
{workout.activeEnergyKcal != null ? (
|
|
<span data-slot="workout-list-energy">
|
|
{formatEnergy(workout.activeEnergyKcal, locale)} kcal
|
|
</span>
|
|
) : null}
|
|
{workout.avgHr != null ? (
|
|
<span data-slot="workout-list-avg-hr">
|
|
{workout.avgHr} bpm
|
|
</span>
|
|
) : null}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</li>
|
|
);
|
|
})}
|
|
{hasNextPage ? (
|
|
<li
|
|
data-slot="workout-list-load-more"
|
|
className="flex flex-col items-center gap-2 p-3"
|
|
>
|
|
{isFetchNextPageError ? (
|
|
<p
|
|
id="workout-list-load-more-error"
|
|
role="alert"
|
|
className="text-destructive text-center text-sm"
|
|
>
|
|
{t("insights.workouts.loadMoreError")}
|
|
</p>
|
|
) : null}
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
className="min-h-11 min-w-44"
|
|
disabled={isFetchingNextPage}
|
|
aria-busy={isFetchingNextPage || undefined}
|
|
aria-describedby={
|
|
isFetchNextPageError ? "workout-list-load-more-error" : undefined
|
|
}
|
|
onClick={onLoadMore}
|
|
>
|
|
{isFetchingNextPage ? (
|
|
<>
|
|
<Loader2
|
|
className="size-4 animate-spin motion-reduce:animate-none"
|
|
aria-hidden="true"
|
|
/>
|
|
{t("insights.workouts.loadingMore")}
|
|
</>
|
|
) : isFetchNextPageError ? (
|
|
t("common.retry")
|
|
) : (
|
|
t("insights.workouts.loadMore")
|
|
)}
|
|
</Button>
|
|
</li>
|
|
) : null}
|
|
</ul>
|
|
);
|
|
}
|