88 lines
3.4 KiB
TypeScript
88 lines
3.4 KiB
TypeScript
|
|
import { getCSS } from "../style/css";
|
||
|
|
import { TIMEZONE_CONFIG } from "../time/zones";
|
||
|
|
import { getWeekBounds, getAdjacentWeeks } from "../time/week";
|
||
|
|
import type { Preferences, TimezoneKey } from "../types";
|
||
|
|
|
||
|
|
export function renderPage(title: string, bodyContent: string, prefs: Preferences): string {
|
||
|
|
const css = getCSS();
|
||
|
|
const weekBounds = getWeekBounds(prefs.week, prefs.tz);
|
||
|
|
const { prev, next } = getAdjacentWeeks(prefs.week);
|
||
|
|
|
||
|
|
const tzLinks = (Object.entries(TIMEZONE_CONFIG) as [TimezoneKey, { iana: string; label: string }][])
|
||
|
|
.map(([key, config]) => {
|
||
|
|
const isCurrent = key === prefs.tz;
|
||
|
|
const href = buildLink("/week", { ...prefs, tz: key });
|
||
|
|
return `<a href="${href}" class="tz-link${isCurrent ? " tz-link--active" : ""}"${isCurrent ? ' aria-current="true"' : ""}>${config.label}</a>`;
|
||
|
|
})
|
||
|
|
.join(" · ");
|
||
|
|
|
||
|
|
const modeLinks = [
|
||
|
|
{ key: "vcr", label: "VCR" },
|
||
|
|
{ key: "all", label: "All Shows" },
|
||
|
|
]
|
||
|
|
.map(({ key, label }) => {
|
||
|
|
const isCurrent = key === prefs.mode;
|
||
|
|
const href = buildLink("/week", { ...prefs, mode: key });
|
||
|
|
return `<a href="${href}" class="mode-link${isCurrent ? " mode-link--active" : ""}"${isCurrent ? ' aria-current="true"' : ""}>${label}</a>`;
|
||
|
|
})
|
||
|
|
.join(" | ");
|
||
|
|
|
||
|
|
const themeLinks = [
|
||
|
|
{ key: "light", label: "☀️ Light" },
|
||
|
|
{ key: "dark", label: "🌙 Dark" },
|
||
|
|
{ key: "system", label: "⚙️ System" },
|
||
|
|
]
|
||
|
|
.map(({ key, label }) => {
|
||
|
|
const isCurrent = key === prefs.theme;
|
||
|
|
const href = buildLink("/week", { ...prefs, theme: key });
|
||
|
|
return `<a href="${href}" class="theme-link${isCurrent ? " theme-link--active" : ""}"${isCurrent ? ' aria-current="true"' : ""}>${label}</a>`;
|
||
|
|
})
|
||
|
|
.join(" · ");
|
||
|
|
|
||
|
|
const prevLink = buildLink("/week", { ...prefs, week: prev });
|
||
|
|
const nextLink = buildLink("/week", { ...prefs, week: next });
|
||
|
|
|
||
|
|
return `<!DOCTYPE html>
|
||
|
|
<html lang="en" data-theme="${prefs.theme}">
|
||
|
|
<head>
|
||
|
|
<meta charset="utf-8">
|
||
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
|
|
<title>${escapeHtml(title)} — VCR Calendar</title>
|
||
|
|
<style>${css}</style>
|
||
|
|
</head>
|
||
|
|
<body>
|
||
|
|
<header class="site-header">
|
||
|
|
<h1 class="site-title"><a href="/week">VCR Calendar</a></h1>
|
||
|
|
<nav class="controls-bar">
|
||
|
|
<div class="controls-row">${modeLinks}</div>
|
||
|
|
<div class="controls-row">
|
||
|
|
<a href="${prevLink}" class="week-nav" aria-label="Previous week">←</a>
|
||
|
|
<span class="week-label">${weekBounds.label}</span>
|
||
|
|
<a href="${nextLink}" class="week-nav" aria-label="Next week">→</a>
|
||
|
|
</div>
|
||
|
|
<div class="controls-row">${tzLinks}</div>
|
||
|
|
<div class="controls-row">${themeLinks} · <a href="/week?reset=1" class="reset-link">Reset</a></div>
|
||
|
|
</nav>
|
||
|
|
</header>
|
||
|
|
<main>${bodyContent}</main>
|
||
|
|
<footer class="site-footer">
|
||
|
|
<p>voidzero Community Radio</p>
|
||
|
|
</footer>
|
||
|
|
</body>
|
||
|
|
</html>`;
|
||
|
|
}
|
||
|
|
|
||
|
|
function buildLink(path: string, prefs: Record<string, string>): string {
|
||
|
|
const params = new URLSearchParams();
|
||
|
|
if (prefs.tz && prefs.tz !== "eastern") params.set("tz", prefs.tz);
|
||
|
|
if (prefs.theme && prefs.theme !== "system") params.set("theme", prefs.theme);
|
||
|
|
if (prefs.mode && prefs.mode !== "vcr") params.set("mode", prefs.mode);
|
||
|
|
if (prefs.week) params.set("week", prefs.week);
|
||
|
|
const qs = params.toString();
|
||
|
|
return qs ? `${path}?${qs}` : path;
|
||
|
|
}
|
||
|
|
|
||
|
|
function escapeHtml(s: string): string {
|
||
|
|
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
||
|
|
}
|