116 lines
4.9 KiB
TypeScript
116 lines
4.9 KiB
TypeScript
// SPDX-License-Identifier: CPAL-1.0
|
|
// Copyright (c) 2026 Marek Novák. Part of Ianitor, under the Common Public Attribution
|
|
// License 1.0: see LICENSE (its Exhibit A is this file's notice).
|
|
|
|
// Formatting only (handoff §3): times in the browser's zone, 24-hour clock, counts sentences.
|
|
|
|
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
|
const LONG = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
|
|
|
|
const two = (n: number) => String(n).padStart(2, "0");
|
|
|
|
/** "Today 14:02", "Yesterday 21:14", "5 Nov 21:14", "5 Nov 2025". */
|
|
export function when(iso: string | null | undefined, now = Date.now()): string {
|
|
if (!iso) return "";
|
|
const d = new Date(iso);
|
|
if (Number.isNaN(d.getTime())) return "";
|
|
const n = new Date(now);
|
|
const hm = `${two(d.getHours())}:${two(d.getMinutes())}`;
|
|
const sameDay = (a: Date, b: Date) => a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate();
|
|
if (sameDay(d, n)) return `Today ${hm}`;
|
|
const y = new Date(n);
|
|
y.setDate(n.getDate() - 1);
|
|
if (sameDay(d, y)) return `Yesterday ${hm}`;
|
|
if (d.getFullYear() !== n.getFullYear()) return `${d.getDate()} ${MONTHS[d.getMonth()]} ${d.getFullYear()}`;
|
|
return `${d.getDate()} ${MONTHS[d.getMonth()]} ${hm}`;
|
|
}
|
|
|
|
/** Inside a sentence: "today 14:02", "yesterday 21:14", "5 Nov 21:14". */
|
|
export function whenInline(iso: string | null | undefined, now = Date.now()): string {
|
|
return when(iso, now).replace(/^(Today|Yesterday)/, (s) => s.toLowerCase());
|
|
}
|
|
|
|
/** After "at" / "on": "at 14:03", "yesterday at 21:14", "on 5 Nov at 21:14", "on 5 Nov 2025". */
|
|
export function atTime(iso: string | null | undefined, now = Date.now()): string {
|
|
const w = when(iso, now);
|
|
if (!w) return "";
|
|
if (w.startsWith("Today ")) return `at ${w.slice(6)}`;
|
|
if (w.startsWith("Yesterday ")) return `yesterday at ${w.slice(10)}`;
|
|
const m = /^(\d+ \w+) (\d\d:\d\d)$/.exec(w);
|
|
return m ? `on ${m[1]} at ${m[2]}` : `on ${w}`;
|
|
}
|
|
|
|
/** "14 November 2026 at 14:20". */
|
|
export function longDate(iso: string | null | undefined, withTime = true): string {
|
|
if (!iso) return "";
|
|
const d = new Date(iso);
|
|
if (Number.isNaN(d.getTime())) return "";
|
|
const s = `${d.getDate()} ${LONG[d.getMonth()]} ${d.getFullYear()}`;
|
|
return withTime ? `${s} at ${two(d.getHours())}:${two(d.getMinutes())}` : s;
|
|
}
|
|
|
|
/** "2 Mar 2027". */
|
|
export function shortDate(iso: string | null | undefined): string {
|
|
if (!iso) return "";
|
|
const d = new Date(iso);
|
|
return `${d.getDate()} ${MONTHS[d.getMonth()]} ${d.getFullYear()}`;
|
|
}
|
|
|
|
/** "8 s", "6 minutes", "2 hours", "3 days". */
|
|
export function duration(secs: number): string {
|
|
if (secs < 60) return `${Math.max(0, Math.round(secs))} s`;
|
|
const m = Math.round(secs / 60);
|
|
if (m < 60) return m === 1 ? "1 minute" : `${m} minutes`;
|
|
const h = Math.round(secs / 3600);
|
|
if (h < 48) return h === 1 ? "1 hour" : `${h} hours`;
|
|
const days = Math.round(secs / 86400);
|
|
return `${days} days`;
|
|
}
|
|
|
|
export function ago(iso: string | null | undefined, now: number): string {
|
|
if (!iso) return "";
|
|
return duration((now - Date.parse(iso)) / 1000) + " ago";
|
|
}
|
|
|
|
/** "7 hosts · 5 healthy, 1 degraded, 1 down" (handoff §5.1); short (phone, §5.2): "5 healthy, 1 degraded, 1 down". */
|
|
export function countsSentence(c: { hosts: number; healthy: number; degraded: number; down: number; unknown: number }, short = false): string {
|
|
const parts = [`${c.healthy} healthy`];
|
|
if (c.degraded) parts.push(`${c.degraded} degraded`);
|
|
if (c.down) parts.push(`${c.down} down`);
|
|
if (c.unknown) parts.push(`${c.unknown} not checked`);
|
|
if (short) return parts.join(", ");
|
|
return `${c.hosts} ${c.hosts === 1 ? "host" : "hosts"} · ${parts.join(", ")}`;
|
|
}
|
|
|
|
/** Split a server message on its first sentence (handoff §7.5). */
|
|
export function firstSentence(m: string): [string, string] {
|
|
const i = m.indexOf(". ");
|
|
return i < 0 ? [m, ""] : [m.slice(0, i + 1), m.slice(i + 2)];
|
|
}
|
|
|
|
/** Field labels in sentences (handoff §6.5). */
|
|
export const FIELD_LABEL: Record<string, string> = {
|
|
hostname: "address",
|
|
upstreams: "sends traffic to",
|
|
tls: "certificate",
|
|
auth: "who can open it",
|
|
"health_check.path": "health check address",
|
|
"health_check.interval_secs": "checks every",
|
|
share_links: "share links",
|
|
device_certs: "device certificates",
|
|
access_log: "visit log",
|
|
websockets: "websockets",
|
|
headers: "header rules",
|
|
};
|
|
|
|
export function fieldValue(field: string, v: string): string {
|
|
if (v === "") return "none";
|
|
if (field === "tls") return v === "domi" ? "Domi certificate" : "Let's Encrypt";
|
|
if (field === "auth") return v === "forward-auth" ? "sign in first" : "anyone";
|
|
if (field === "access_log") return v === "truncated" ? "on" : "off";
|
|
if (v === "true") return "on";
|
|
if (v === "false") return "off";
|
|
if (field === "health_check.interval_secs") return `${v} s`;
|
|
return v;
|
|
}
|