refactor: extract render utils and polish page shell
Share time/HTML/live helpers across renderers; rename mode to All Active, add footer timezone label and header emoji. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { TIMEZONE_CONFIG, convertTime } from "../time/zones";
|
||||
import { getWeekBounds } from "../time/week";
|
||||
import type { Show, Preferences } from "../types";
|
||||
import { checkIsLive, escapeHtml, formatTime12, parseHourMinute } from "./utils";
|
||||
|
||||
const DAY_ORDER = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"] as const;
|
||||
const DAY_LABELS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
|
||||
@@ -125,53 +126,3 @@ function buildAgendaEntry(
|
||||
};
|
||||
}
|
||||
|
||||
function parseHourMinute(time: string): [number, number] {
|
||||
const clean = time.replace("+1", "");
|
||||
const [h, m] = clean.split(":").map(Number);
|
||||
return [h, m];
|
||||
}
|
||||
|
||||
function formatTime12(hour: number, minute: number): string {
|
||||
const period = hour < 12 ? "AM" : "PM";
|
||||
const h = hour % 12 || 12;
|
||||
return `${h}:${String(minute).padStart(2, "0")} ${period}`;
|
||||
}
|
||||
|
||||
function checkIsLive(show: Show, now: Date): boolean {
|
||||
const dayIdx = DAY_ORDER.indexOf(show.schedule.day);
|
||||
const nowInSource = new Intl.DateTimeFormat("en-US", {
|
||||
timeZone: show.schedule.timezone,
|
||||
weekday: "long",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
hour12: false,
|
||||
}).formatToParts(now);
|
||||
|
||||
const nowDay = nowInSource.find((p) => p.type === "weekday")!.value.toLowerCase() as DayKey;
|
||||
const nowHour = parseInt(nowInSource.find((p) => p.type === "hour")!.value);
|
||||
const nowMinute = parseInt(nowInSource.find((p) => p.type === "minute")!.value);
|
||||
const nowMinutes = (nowHour === 24 ? 0 : nowHour) * 60 + nowMinute;
|
||||
|
||||
const [startH, startM] = parseHourMinute(show.schedule.startTime);
|
||||
const startMinutes = startH * 60 + startM;
|
||||
|
||||
const endRaw = show.schedule.endTime.replace("+1", "");
|
||||
const isOvernight = show.schedule.endTime.includes("+1");
|
||||
const [endH, endM] = parseHourMinute(endRaw);
|
||||
const endMinutes = endH * 60 + endM;
|
||||
|
||||
const sourceDay = DAY_ORDER[dayIdx];
|
||||
const nextDay = DAY_ORDER[(dayIdx + 1) % 7];
|
||||
|
||||
if (!isOvernight) {
|
||||
return nowDay === sourceDay && nowMinutes >= startMinutes && nowMinutes < endMinutes;
|
||||
}
|
||||
|
||||
if (nowDay === sourceDay && nowMinutes >= startMinutes) return true;
|
||||
if (nowDay === nextDay && nowMinutes < endMinutes) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
function escapeHtml(s: string): string {
|
||||
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ describe("renderPage", () => {
|
||||
it("includes mode toggle links", () => {
|
||||
const html = renderPage("Test", "", defaultPrefs);
|
||||
expect(html).toContain("VCR");
|
||||
expect(html).toContain("All Shows");
|
||||
expect(html).toContain("All Active");
|
||||
});
|
||||
|
||||
it("includes theme toggle links", () => {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { getCSS } from "../style/css";
|
||||
import { TIMEZONE_CONFIG } from "../time/zones";
|
||||
import { getWeekBounds, getAdjacentWeeks } from "../time/week";
|
||||
import type { Preferences, TimezoneKey } from "../types";
|
||||
import { escapeHtml } from "./utils";
|
||||
|
||||
export function renderPage(title: string, bodyContent: string, prefs: Preferences): string {
|
||||
const css = getCSS();
|
||||
@@ -18,7 +19,7 @@ export function renderPage(title: string, bodyContent: string, prefs: Preference
|
||||
|
||||
const modeLinks = [
|
||||
{ key: "vcr", label: "VCR" },
|
||||
{ key: "all", label: "All Shows" },
|
||||
{ key: "all", label: "All Active" },
|
||||
]
|
||||
.map(({ key, label }) => {
|
||||
const isCurrent = key === prefs.mode;
|
||||
@@ -52,7 +53,7 @@ export function renderPage(title: string, bodyContent: string, prefs: Preference
|
||||
</head>
|
||||
<body>
|
||||
<header class="site-header">
|
||||
<h1 class="site-title"><a href="/week">VCR Calendar</a></h1>
|
||||
<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">
|
||||
@@ -66,6 +67,7 @@ export function renderPage(title: string, bodyContent: string, prefs: Preference
|
||||
</header>
|
||||
<main>${bodyContent}</main>
|
||||
<footer class="site-footer">
|
||||
<p>Times shown in ${escapeHtml(TIMEZONE_CONFIG[prefs.tz].label)}</p>
|
||||
<p>voidzero Community Radio</p>
|
||||
</footer>
|
||||
</body>
|
||||
@@ -82,6 +84,3 @@ function buildLink(path: string, prefs: Record<string, string>): string {
|
||||
return qs ? `${path}?${qs}` : path;
|
||||
}
|
||||
|
||||
function escapeHtml(s: string): string {
|
||||
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { renderPage } from "./page";
|
||||
import { TIMEZONE_CONFIG, convertTime } from "../time/zones";
|
||||
import { getWeekBounds } from "../time/week";
|
||||
import type { Show, Preferences } from "../types";
|
||||
import { escapeHtml, formatTime12, parseHourMinute } from "./utils";
|
||||
|
||||
const DAY_ORDER = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"] as const;
|
||||
const DAY_LABELS = ["Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"];
|
||||
@@ -75,18 +76,3 @@ export function renderShowDetail(show: Show, prefs: Preferences): string {
|
||||
return renderPage(show.name, html, prefs);
|
||||
}
|
||||
|
||||
function parseHourMinute(time: string): [number, number] {
|
||||
const clean = time.replace("+1", "");
|
||||
const [h, m] = clean.split(":").map(Number);
|
||||
return [h, m];
|
||||
}
|
||||
|
||||
function formatTime12(hour: number, minute: number): string {
|
||||
const period = hour < 12 ? "AM" : "PM";
|
||||
const h = hour % 12 || 12;
|
||||
return `${h}:${String(minute).padStart(2, "0")} ${period}`;
|
||||
}
|
||||
|
||||
function escapeHtml(s: string): string {
|
||||
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
||||
}
|
||||
|
||||
55
src/render/utils.ts
Normal file
55
src/render/utils.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import type { Show } from "../types";
|
||||
|
||||
const DAY_ORDER = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"] as const;
|
||||
type DayKey = (typeof DAY_ORDER)[number];
|
||||
|
||||
export function parseHourMinute(time: string): [number, number] {
|
||||
const clean = time.replace("+1", "");
|
||||
const [h, m] = clean.split(":").map(Number);
|
||||
return [h, m];
|
||||
}
|
||||
|
||||
export function formatTime12(hour: number, minute: number): string {
|
||||
const period = hour < 12 ? "AM" : "PM";
|
||||
const h = hour % 12 || 12;
|
||||
return `${h}:${String(minute).padStart(2, "0")} ${period}`;
|
||||
}
|
||||
|
||||
export function escapeHtml(s: string): string {
|
||||
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
||||
}
|
||||
|
||||
export function checkIsLive(show: Show, now: Date): boolean {
|
||||
const dayIdx = DAY_ORDER.indexOf(show.schedule.day);
|
||||
const nowInSource = new Intl.DateTimeFormat("en-US", {
|
||||
timeZone: show.schedule.timezone,
|
||||
weekday: "long",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
hour12: false,
|
||||
}).formatToParts(now);
|
||||
|
||||
const nowDay = nowInSource.find((p) => p.type === "weekday")!.value.toLowerCase() as DayKey;
|
||||
const nowHour = parseInt(nowInSource.find((p) => p.type === "hour")!.value);
|
||||
const nowMinute = parseInt(nowInSource.find((p) => p.type === "minute")!.value);
|
||||
const nowMinutes = (nowHour === 24 ? 0 : nowHour) * 60 + nowMinute;
|
||||
|
||||
const [startH, startM] = parseHourMinute(show.schedule.startTime);
|
||||
const startMinutes = startH * 60 + startM;
|
||||
|
||||
const endRaw = show.schedule.endTime.replace("+1", "");
|
||||
const isOvernight = show.schedule.endTime.includes("+1");
|
||||
const [endH, endM] = parseHourMinute(endRaw);
|
||||
const endMinutes = endH * 60 + endM;
|
||||
|
||||
const sourceDay = DAY_ORDER[dayIdx];
|
||||
const nextDay = DAY_ORDER[(dayIdx + 1) % 7];
|
||||
|
||||
if (!isOvernight) {
|
||||
return nowDay === sourceDay && nowMinutes >= startMinutes && nowMinutes < endMinutes;
|
||||
}
|
||||
|
||||
if (nowDay === sourceDay && nowMinutes >= startMinutes) return true;
|
||||
if (nowDay === nextDay && nowMinutes < endMinutes) return true;
|
||||
return false;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { TIMEZONE_CONFIG, convertTime } from "../time/zones";
|
||||
import { getWeekBounds } from "../time/week";
|
||||
import type { Show, Preferences } from "../types";
|
||||
import { checkIsLive, escapeHtml, formatTime12, parseHourMinute } from "./utils";
|
||||
|
||||
const DAY_ORDER = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"] as const;
|
||||
const DAY_SHORT = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
||||
@@ -165,18 +166,6 @@ function minutesToRow(minutesFromMidnight: number, gridStartMin: number): number
|
||||
return 2 + Math.round((minutesFromMidnight - gridStartMin) / 30);
|
||||
}
|
||||
|
||||
function parseHourMinute(time: string): [number, number] {
|
||||
const clean = time.replace("+1", "");
|
||||
const [h, m] = clean.split(":").map(Number);
|
||||
return [h, m];
|
||||
}
|
||||
|
||||
function formatTime12(hour: number, minute: number): string {
|
||||
const period = hour < 12 ? "AM" : "PM";
|
||||
const h = hour % 12 || 12;
|
||||
return `${h}:${String(minute).padStart(2, "0")} ${period}`;
|
||||
}
|
||||
|
||||
function hexToRgba(hex: string, alpha: number): string {
|
||||
const r = parseInt(hex.slice(1, 3), 16);
|
||||
const g = parseInt(hex.slice(3, 5), 16);
|
||||
@@ -184,10 +173,6 @@ function hexToRgba(hex: string, alpha: number): string {
|
||||
return `rgba(${r},${g},${b},${alpha})`;
|
||||
}
|
||||
|
||||
function escapeHtml(s: string): string {
|
||||
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
||||
}
|
||||
|
||||
function getTodayDateKey(now: Date, iana: string): string {
|
||||
const parts = new Intl.DateTimeFormat("en-US", {
|
||||
timeZone: iana,
|
||||
@@ -208,37 +193,3 @@ function dateKeyFromUtcDate(date: Date): string {
|
||||
return `${y}-${m}-${d}`;
|
||||
}
|
||||
|
||||
function checkIsLive(show: Show, now: Date): boolean {
|
||||
const dayIdx = DAY_ORDER.indexOf(show.schedule.day);
|
||||
const nowInSource = new Intl.DateTimeFormat("en-US", {
|
||||
timeZone: show.schedule.timezone,
|
||||
weekday: "long",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
hour12: false,
|
||||
}).formatToParts(now);
|
||||
|
||||
const nowDay = nowInSource.find((p) => p.type === "weekday")!.value.toLowerCase() as DayKey;
|
||||
const nowHour = parseInt(nowInSource.find((p) => p.type === "hour")!.value);
|
||||
const nowMinute = parseInt(nowInSource.find((p) => p.type === "minute")!.value);
|
||||
const nowMinutes = (nowHour === 24 ? 0 : nowHour) * 60 + nowMinute;
|
||||
|
||||
const [startH, startM] = parseHourMinute(show.schedule.startTime);
|
||||
const startMinutes = startH * 60 + startM;
|
||||
|
||||
const endRaw = show.schedule.endTime.replace("+1", "");
|
||||
const isOvernight = show.schedule.endTime.includes("+1");
|
||||
const [endH, endM] = parseHourMinute(endRaw);
|
||||
const endMinutes = endH * 60 + endM;
|
||||
|
||||
const sourceDay = DAY_ORDER[dayIdx];
|
||||
const nextDay = DAY_ORDER[(dayIdx + 1) % 7];
|
||||
|
||||
if (!isOvernight) {
|
||||
return nowDay === sourceDay && nowMinutes >= startMinutes && nowMinutes < endMinutes;
|
||||
}
|
||||
|
||||
if (nowDay === sourceDay && nowMinutes >= startMinutes) return true;
|
||||
if (nowDay === nextDay && nowMinutes < endMinutes) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user