Files
FlowReader/web/src/lib/format.ts
T
Claude 5a16769eb9 fix(web): cap the article grid at 5 columns, show the year on old dates
The grid adds columns as long as 20rem fits; on wide screens that meant
6+ cards per row. The column minimum now grows with the width once 5
columns fit, so a 6th never appears.

Dates outside the current year now show the year: a feed item dated
2027 was displayed as "5 oct." and looked like an old article stuck on
top of the list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Fc1UFpB4V1MGWLxHAu3BMK
2026-10-13 10:56:17 +00:00

35 lines
1.5 KiB
TypeScript

// Formatters are built once: constructing Intl objects per render is costly in long lists.
const shortDate = new Intl.DateTimeFormat('fr-FR', { day: 'numeric', month: 'short', hour: '2-digit', minute: '2-digit' });
const shortDateYear = new Intl.DateTimeFormat('fr-FR', { day: 'numeric', month: 'short', year: 'numeric' });
/** Short date, with the year when it isn't the current one. */
function shortWithYear(d: Date) {
return d.getFullYear() === new Date().getFullYear() ? shortDate.format(d) : shortDateYear.format(d);
}
const longDate = new Intl.DateTimeFormat('fr-FR', { day: 'numeric', month: 'long', year: 'numeric' });
const relative = new Intl.RelativeTimeFormat('fr', { numeric: 'auto' });
export function formatShort(iso?: string) {
return iso ? shortWithYear(new Date(iso)) : '';
}
export function formatLong(iso?: string) {
return iso ? longDate.format(new Date(iso)) : '';
}
/** "il y a 5 min", "hier"… falling back to a short date after a week. */
export function formatRelative(iso?: string) {
if (!iso) return '';
const diff = (new Date(iso).getTime() - Date.now()) / 1000;
const abs = Math.abs(diff);
if (abs < 60) return "à l'instant";
if (abs < 3600) return relative.format(Math.round(diff / 60), 'minute');
if (abs < 86400) return relative.format(Math.round(diff / 3600), 'hour');
if (abs < 7 * 86400) return relative.format(Math.round(diff / 86400), 'day');
return shortWithYear(new Date(iso));
}
export function readingTimeLabel(minutes?: number) {
return `${Math.max(1, minutes ?? 1)} min`;
}