Files
HealthLog/src/components/day/day-link.tsx
T
Marc-André Bombeck bf83d414e2 feat: open any past day as a layer over the page
A date is now a door to its day (#613). `?day=YYYY-MM-DD` opens the day
over whichever page is open: docked beside it from 1280 px, the way the
Coach's conversations panel sits, a sheet from the right below that, and a
bottom sheet on a phone that opens at a little over half the height and
grows to full height from its handle. On /coach it is always a sheet, so
there is never more than one docked panel. Back and Escape close it, the
previous and next day replace the history entry instead of adding one,
Alt with an arrow steps a day, focus moves to the day's heading on open and
returns to whatever opened it on close, and a polite region names the day.
A malformed or future date is dropped from the URL.

The day shows the value it was opened from with its comparison, what ran
through the day, the readings against the person's own usual range on a
short number line (eight curated, the rest behind "All values"), and what
happened in order of the clock, each row linking to its record. Loading,
failure, an empty day and areas a grant does not cover each read as such.
The footer offers the timeline when that module is on and capture for the
day, with the capture forms starting on that date.

Doors into it: a click on a daily point of a metric chart (a tap shows the
value and the tooltip offers the day on touch screens), the dates in the
chart's data table, a quiet row of dots under the axis for days with
entries and a ring for notable ones, the lab chart and lab readings, the
measurement, mood and intake lists, symptom and illness day headings,
episode onset and recovery, document dates, workout dates, the workout
detail, the mood and adherence heatmaps, and the days a Coach answer's
get_day calls read, shown as chips. The next planned visit carries what
happened since the last one, each date opening its day, with a doctor
report for that window.

Not here, and frozen by a guard: the dashboard's today area, mini charts
in tiles, the cycle calendar, model-written text, future dates, the
clinician share link and the doctor-report PDF.
2026-10-08 14:20:01 +02:00

189 lines
5.9 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import type { ReactNode } from "react";
import { usePathname } from "next/navigation";
import { DAY_QUERY_PARAM, type DateKey } from "@/lib/day/contract";
import { useDisplayTimezone, useTranslations } from "@/lib/i18n/context";
import {
dateOnlyKey,
isDateOnlyKey,
isNoonUtcAnchor,
} from "@/lib/tz/date-only";
import { cn } from "@/lib/utils";
import { openDay, useOpenDay, type DayFocus } from "./day-layer-controller";
import { dateKeyOfInstant, isOpenableDay } from "./day-url";
import { useTodayKey } from "./use-today-key";
/**
* A date that opens its day: the one affordance for "show me everything on
* this day" across the app.
*
* Text in the foreground colour with a quiet underline in the border colour,
* no icon, no colour of its own. It sits beside whatever the row already does:
* the row keeps opening its entry, the date opens the day. On a touch screen
* the hit area grows to 44 px without moving the line it sits in.
*
* A date in the future, or one that is not a calendar date, renders as plain
* text: a day without content is a dead end, so nothing offers it.
*
* It is a real link to `?day=` on the current page, so it can be opened in a
* new tab; a plain click opens the layer in place instead of navigating.
*/
export interface DayLinkProps {
date: DateKey;
children: ReactNode;
/** What the person was looking at, shown at the top of the day. */
focus?: Omit<DayFocus, "date">;
className?: string;
/** Size of the text; the link inherits it otherwise. */
size?: "xs" | "sm";
/**
* `-1` keeps the link out of the tab order where its surface already owns
* the keyboard (a roving-tabindex grid of cards), and offers the day on
* another keyboard path.
*/
tabIndex?: number;
}
export const DAY_LINK_CLASS = cn(
"text-foreground decoration-border relative rounded-sm underline decoration-[1.5px] underline-offset-[3px] tabular-nums",
"hover:decoration-foreground focus-visible:ring-ring/50 transition-colors focus-visible:ring-[3px] focus-visible:outline-none",
// A 44 px hit area on touch that does not change the line height.
"pointer-coarse:after:absolute pointer-coarse:after:-inset-y-3 pointer-coarse:after:inset-x-0 pointer-coarse:after:content-['']",
);
/** The open day is marked like a selected row, not underlined. */
export const DAY_LINK_OPEN_CLASS =
"bg-muted -mx-1.5 px-1.5 no-underline decoration-transparent";
export function DayLink({
date,
children,
focus,
className,
size,
tabIndex,
}: DayLinkProps) {
const { t } = useTranslations();
const pathname = usePathname();
const today = useTodayKey();
const open = useOpenDay();
if (!isOpenableDay(date, today)) {
return (
<span
data-slot="day-link-plain"
className={cn(
"tabular-nums",
size === "xs" && "text-xs",
size === "sm" && "text-sm",
className,
)}
>
{children}
</span>
);
}
const isOpen = open === date;
return (
<a
href={`${pathname}?${DAY_QUERY_PARAM}=${date}`}
data-slot="day-link"
data-day={date}
data-open={isOpen ? "true" : undefined}
tabIndex={tabIndex}
title={t("day.openDay")}
onClick={(event) => {
// A modified click opens a new tab or window, as any link does.
if (
event.defaultPrevented ||
event.button !== 0 ||
event.metaKey ||
event.ctrlKey ||
event.shiftKey ||
event.altKey
) {
return;
}
event.preventDefault();
// The row around the date opens its entry; this click is the day's.
event.stopPropagation();
openDay(date, {
focus: focus ? { ...focus, date } : null,
trigger: event.currentTarget,
});
}}
onKeyDown={(event) => {
// A row that opens on Enter must not also open under the day.
if (event.key === "Enter" || event.key === " ") event.stopPropagation();
}}
className={cn(
DAY_LINK_CLASS,
size === "xs" && "text-xs",
size === "sm" && "text-sm",
isOpen && DAY_LINK_OPEN_CLASS,
className,
)}
>
{children}
</a>
);
}
/**
* A `DayLink` for an instant (a reading's `measuredAt`, an intake's
* `takenAt`): the day is the instant's calendar day in the record's display
* zone, the zone every date on screen is printed in.
*/
export function DayLinkAt({
at,
...props
}: Omit<DayLinkProps, "date"> & { at: Date | string | number }) {
const timeZone = useDisplayTimezone();
return <DayLink date={dateKeyOfInstant(at, timeZone)} {...props} />;
}
/**
* A `DayLink` for a value that may be a stated calendar date stored at noon
* UTC (a lab draw date, a report date) or a real instant. A noon-UTC anchor
* is its UTC date in every zone; anything else is cut in the display zone,
* exactly as the surface prints it.
*/
export function DayLinkStated({
at,
...props
}: Omit<DayLinkProps, "date"> & { at: string | Date }) {
const timeZone = useDisplayTimezone();
if (typeof at === "string" && isDateOnlyKey(at)) {
return <DayLink date={at} {...props} />;
}
const instant = new Date(at);
const date = isNoonUtcAnchor(instant)
? dateOnlyKey(instant)
: dateKeyOfInstant(instant, timeZone);
return <DayLink date={date} {...props} />;
}
/**
* Put a node where a translated sentence names a value. Pass the marker as
* the parameter (`t("illness.onsetOn", { date: DAY_LINK_SLOT })`) and the
* link replaces it, so a date inside a sentence can open its day without the
* sentence being split into fragments in the bundle.
*/
export const DAY_LINK_SLOT = "⁣day⁣";
export function withDayLinkSlot(text: string, node: ReactNode): ReactNode {
const at = text.indexOf(DAY_LINK_SLOT);
if (at === -1) return text;
return (
<>
{text.slice(0, at)}
{node}
{text.slice(at + DAY_LINK_SLOT.length)}
</>
);
}