mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 00:12:18 +00:00
Without a zoom in the URL the window now follows the record: one that starts inside the last three months opens on three months clamped to its first entry (at least four weeks up to today), an older one with entries there opens on three months, an older one without opens on everything, and an empty one keeps the empty state. The choice is made from the first answer before anything is drawn, so the page does not jump between windows.
154 lines
4.7 KiB
TypeScript
154 lines
4.7 KiB
TypeScript
/**
|
|
* The timeline's zoom and chosen range in the URL (v1.42):
|
|
* `?zoom=year`, `?zoom=range&from=2026-03-01&to=2026-04-15`. Back and a
|
|
* bookmark both land where the person was. Anything that does not read as
|
|
* a zoom, or a range without two valid ends, reads as no zoom at all and
|
|
* opens on the same default without a word: a stale bookmark should open
|
|
* the timeline, not an error.
|
|
*/
|
|
import {
|
|
TIMELINE_RANGE_MAX_DAYS,
|
|
TIMELINE_ZOOMS,
|
|
type TimelineZoom,
|
|
} from "@/lib/day/contract";
|
|
import { isCalendarDateKey } from "@/lib/tz/date-only";
|
|
|
|
import { dayKey, dayNumber } from "./timeline-dates";
|
|
|
|
export const ZOOM_PARAM = "zoom";
|
|
export const FROM_PARAM = "from";
|
|
export const TO_PARAM = "to";
|
|
|
|
export interface TimelineRange {
|
|
from: string;
|
|
to: string;
|
|
}
|
|
|
|
export interface TimelineUrlState {
|
|
/** The zoom the URL names, or null when it names none (or a bad one). */
|
|
zoom: TimelineZoom | null;
|
|
/** The chosen range, with `zoom` `range` only. */
|
|
range: TimelineRange | null;
|
|
}
|
|
|
|
/**
|
|
* A chosen range made honest, softly: never past today, never before the
|
|
* record's first entry (when known), never inverted, and never longer than
|
|
* the server answers. Each end moves only as far as it must.
|
|
*/
|
|
export function clampRange(
|
|
range: TimelineRange,
|
|
today: string,
|
|
dataFrom: string | null,
|
|
): TimelineRange {
|
|
let { from, to } = range;
|
|
if (to > today) to = today;
|
|
if (dataFrom && dataFrom <= to && from < dataFrom) from = dataFrom;
|
|
if (from > to) from = to;
|
|
const earliest = dayKey(dayNumber(to) - (TIMELINE_RANGE_MAX_DAYS - 1));
|
|
if (from < earliest) from = earliest;
|
|
return { from, to };
|
|
}
|
|
|
|
export function parseTimelineUrl(
|
|
params: Pick<URLSearchParams, "get">,
|
|
today: string,
|
|
): TimelineUrlState {
|
|
const raw = params.get(ZOOM_PARAM);
|
|
const zoom = (TIMELINE_ZOOMS as readonly string[]).includes(raw ?? "")
|
|
? (raw as TimelineZoom)
|
|
: null;
|
|
if (zoom !== "range") return { zoom, range: null };
|
|
const from = params.get(FROM_PARAM);
|
|
const to = params.get(TO_PARAM);
|
|
if (
|
|
!from ||
|
|
!to ||
|
|
!isCalendarDateKey(from) ||
|
|
!isCalendarDateKey(to) ||
|
|
from > to ||
|
|
from > today
|
|
) {
|
|
return { zoom: null, range: null };
|
|
}
|
|
return { zoom, range: clampRange({ from, to }, today, null) };
|
|
}
|
|
|
|
/** The zoom the page opens on when the URL names none. */
|
|
export const DEFAULT_ZOOM: TimelineZoom = "quarter";
|
|
|
|
/** The shortest default window over a young record, in days to today. */
|
|
export const YOUNG_RECORD_MIN_DAYS = 28;
|
|
|
|
/**
|
|
* What the page opens on when the URL names no zoom, decided from the
|
|
* default three months' answer (`window`, whether it holds anything, and
|
|
* the record's first entry):
|
|
*
|
|
* - a record that starts inside the three months opens on three months
|
|
* clamped to its first entry, at least four weeks up to today, so a
|
|
* few days of data neither hide in a sliver nor stretch thin
|
|
* (`from` set);
|
|
* - an older record with entries in the three months opens on them
|
|
* (`from` null);
|
|
* - an older record with none there opens on everything;
|
|
* - an empty record: null, the plain empty state.
|
|
*/
|
|
export type DefaultWindow =
|
|
{ zoom: "quarter"; from: string | null } | { zoom: "all" } | null;
|
|
|
|
export function resolveDefaultWindow(input: {
|
|
dataFrom: string | null;
|
|
window: { from: string; to: string };
|
|
windowHasData: boolean;
|
|
today: string;
|
|
}): DefaultWindow {
|
|
const { dataFrom, window, today } = input;
|
|
if (dataFrom === null) return null;
|
|
if (dataFrom >= window.from) {
|
|
const minimum = dayKey(dayNumber(today) - (YOUNG_RECORD_MIN_DAYS - 1));
|
|
const start = dataFrom < minimum ? dataFrom : minimum;
|
|
return {
|
|
zoom: "quarter",
|
|
from: start > window.from ? start : null,
|
|
};
|
|
}
|
|
return input.windowHasData
|
|
? { zoom: "quarter", from: null }
|
|
: { zoom: "all" };
|
|
}
|
|
|
|
/**
|
|
* True when a window shows nothing while the record has entries from before
|
|
* it: the page then offers everything instead of a dead end. `dataFrom` is
|
|
* the earliest date anything in the record carries.
|
|
*/
|
|
export function hasDataBeforeWindow(
|
|
dataFrom: string | null,
|
|
window: { from: string } | null,
|
|
): boolean {
|
|
return dataFrom !== null && window !== null && dataFrom < window.from;
|
|
}
|
|
|
|
/**
|
|
* The search string for a zoom (and, for `range`, its ends), keeping every
|
|
* other parameter (`day`, `add`) as it is.
|
|
*/
|
|
export function timelineSearch(
|
|
current: string,
|
|
zoom: TimelineZoom,
|
|
range: TimelineRange | null,
|
|
): string {
|
|
const params = new URLSearchParams(current);
|
|
params.set(ZOOM_PARAM, zoom);
|
|
if (zoom === "range" && range) {
|
|
params.set(FROM_PARAM, range.from);
|
|
params.set(TO_PARAM, range.to);
|
|
} else {
|
|
params.delete(FROM_PARAM);
|
|
params.delete(TO_PARAM);
|
|
}
|
|
const search = params.toString();
|
|
return search ? `?${search}` : "";
|
|
}
|