mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-08 12:32:43 +00:00
The per-user date order reached the entry form and nothing else. A reporter switched away from MM/DD/YYYY, the date picker followed, and the dashboard chart axes and the measurements list kept the old order. `makeFormatters` took `timeFormat` and `dateFormat` as optional parameters defaulting to "AUTO", so six of the seven constructions in the tree silently rendered the locale default and nothing anywhere said the setting had been dropped. Drop both defaults. The compiler then names every caller: the chart bucket labels, the legacy `@/lib/format` helpers the measurements list renders through, the medication compliance and efficacy charts, the doctor-report PDF and the clinician share view. Thread the preference: - `makeBucketLabelFormatters` takes the order; the health and mood charts read `useDateFormatPreference()`. The UTC pin decides which day the label names, the preference decides how it is spelled, and the two are separate questions. - `format.ts` reads `readStoredDateFormat()` beside the timezone and hour-cycle mirrors it already read. - The PDF takes `dateFormat` as a required render option, alongside `timeFormat`, which was optional for the same reason. - The clinician share view spells the OWNER's dates the owner's way. It also picked up the owner's hour cycle, which it had been ignoring while the PDF route beside it already honoured it. Both resolve through the new `resolveUserFormatPreferences`, the counterpart to `resolveUserTimezone`. Textual months stay on the UI locale. The preference pins numeric field order by rendering through de-DE / en-US / en-CA, and a textual month through those would change the month name's language. Add `date-order-explicit-formatter-guard`: a raw numeric date renderer must take its locale from the preference or carry a written reason, and a formatter construction may not hard-code "AUTO" for the order. Its allowlist holds thirteen calendar-day key builders, each with the reason it has no order to get wrong. The sibling timezone guard passes every one of them, because naming a calendar says nothing about spelling a date. Refs #922
52 lines
1.9 KiB
TypeScript
52 lines
1.9 KiB
TypeScript
/**
|
|
* The per-user display preferences that are NOT the timezone: the hour cycle
|
|
* and the date order.
|
|
*
|
|
* `resolveUserTimezone()` already answers "which calendar does this user read
|
|
* in". These two answer "how is a clock and a date spelled for them", and
|
|
* every server surface that renders a date on a user's behalf needs all
|
|
* three. They lived as an inline `prisma.user.findUnique` in the share-link
|
|
* download path and nowhere else, which is part of why the clinician page
|
|
* rendered the owner's dates in the display default while the PDF beside it
|
|
* rendered them in the owner's hour cycle (issue #922).
|
|
*
|
|
* Deliberately NOT cached: unlike the timezone (read on nearly every request
|
|
* and invalidated by hand on profile write), these are read on the handful of
|
|
* server-rendered document surfaces, so a cache would add an invalidation
|
|
* obligation to every write path in exchange for nothing measurable.
|
|
*/
|
|
import { prisma } from "@/lib/db";
|
|
import type {
|
|
DateFormatPreference,
|
|
TimeFormatPreference,
|
|
} from "@/lib/format-locale";
|
|
|
|
export interface UserFormatPreferences {
|
|
timeFormat: TimeFormatPreference;
|
|
dateFormat: DateFormatPreference;
|
|
}
|
|
|
|
/**
|
|
* Read `users.time_format` + `users.date_format`. An unknown id, a null
|
|
* column, or an unreachable database all resolve to AUTO — the same
|
|
* "follow the locale" answer the columns' own default carries, so a
|
|
* document still renders rather than failing on a preference lookup.
|
|
*/
|
|
export async function resolveUserFormatPreferences(
|
|
userId: string,
|
|
): Promise<UserFormatPreferences> {
|
|
if (!userId) return { timeFormat: "AUTO", dateFormat: "AUTO" };
|
|
try {
|
|
const row = await prisma.user.findUnique({
|
|
where: { id: userId },
|
|
select: { timeFormat: true, dateFormat: true },
|
|
});
|
|
return {
|
|
timeFormat: row?.timeFormat ?? "AUTO",
|
|
dateFormat: row?.dateFormat ?? "AUTO",
|
|
};
|
|
} catch {
|
|
return { timeFormat: "AUTO", dateFormat: "AUTO" };
|
|
}
|
|
}
|