feat: HTML page shell with controls bar, timezone picker, and theme toggle
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
78
src/render/page.test.ts
Normal file
78
src/render/page.test.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { renderPage } from "./page";
|
||||
import type { Preferences } from "../types";
|
||||
|
||||
const defaultPrefs: Preferences = {
|
||||
tz: "eastern",
|
||||
theme: "system",
|
||||
mode: "vcr",
|
||||
week: "2026-W45",
|
||||
setCookies: [],
|
||||
};
|
||||
|
||||
describe("renderPage", () => {
|
||||
it("returns valid HTML with doctype", () => {
|
||||
const html = renderPage("Test", "<p>hello</p>", defaultPrefs);
|
||||
expect(html).toContain("<!DOCTYPE html>");
|
||||
expect(html).toContain("</html>");
|
||||
});
|
||||
|
||||
it("sets data-theme attribute from preferences", () => {
|
||||
const html = renderPage("Test", "", { ...defaultPrefs, theme: "dark" });
|
||||
expect(html).toContain('data-theme="dark"');
|
||||
});
|
||||
|
||||
it("includes inlined CSS in a style tag", () => {
|
||||
const html = renderPage("Test", "", defaultPrefs);
|
||||
expect(html).toContain("<style>");
|
||||
expect(html).toContain("</style>");
|
||||
});
|
||||
|
||||
it("includes timezone picker with all 5 zones", () => {
|
||||
const html = renderPage("Test", "", defaultPrefs);
|
||||
expect(html).toContain("Eastern");
|
||||
expect(html).toContain("Central");
|
||||
expect(html).toContain("Pacific");
|
||||
expect(html).toContain("UTC");
|
||||
expect(html).toContain("Amsterdam");
|
||||
});
|
||||
|
||||
it("highlights the current timezone", () => {
|
||||
const html = renderPage("Test", "", { ...defaultPrefs, tz: "pacific" });
|
||||
expect(html).toContain('aria-current="true"');
|
||||
});
|
||||
|
||||
it("includes mode toggle links", () => {
|
||||
const html = renderPage("Test", "", defaultPrefs);
|
||||
expect(html).toContain("VCR");
|
||||
expect(html).toContain("All Shows");
|
||||
});
|
||||
|
||||
it("includes theme toggle links", () => {
|
||||
const html = renderPage("Test", "", defaultPrefs);
|
||||
expect(html).toContain("Light");
|
||||
expect(html).toContain("Dark");
|
||||
expect(html).toContain("System");
|
||||
});
|
||||
|
||||
it("includes week navigation", () => {
|
||||
const html = renderPage("Test", "", defaultPrefs);
|
||||
expect(html).toContain("W44"); // prev
|
||||
expect(html).toContain("W46"); // next
|
||||
});
|
||||
|
||||
it("renders body content inside main", () => {
|
||||
const html = renderPage("Test", "<p>calendar here</p>", defaultPrefs);
|
||||
expect(html).toContain("<p>calendar here</p>");
|
||||
});
|
||||
|
||||
it("includes reset link", () => {
|
||||
const html = renderPage("Test", "", defaultPrefs);
|
||||
expect(html).toContain("reset");
|
||||
});
|
||||
|
||||
it("contains no script tags", () => {
|
||||
const html = renderPage("Test", "", defaultPrefs);
|
||||
expect(html).not.toContain("<script");
|
||||
});
|
||||
});
|
||||
87
src/render/page.ts
Normal file
87
src/render/page.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
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, """);
|
||||
}
|
||||
Reference in New Issue
Block a user