From 5c996f1dda9898101b424d708fa588f472db5f1f Mon Sep 17 00:00:00 2001 From: cottongin Date: Mon, 2 Nov 2026 18:42:50 -0500 Subject: [PATCH] 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 --- src/render/agenda.ts | 51 +----------------------------------- src/render/page.test.ts | 2 +- src/render/page.ts | 9 +++---- src/render/show-detail.ts | 16 +----------- src/render/utils.ts | 55 +++++++++++++++++++++++++++++++++++++++ src/render/week-grid.ts | 51 +----------------------------------- 6 files changed, 63 insertions(+), 121 deletions(-) create mode 100644 src/render/utils.ts diff --git a/src/render/agenda.ts b/src/render/agenda.ts index 499ae71..7d46659 100644 --- a/src/render/agenda.ts +++ b/src/render/agenda.ts @@ -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, """); -} diff --git a/src/render/page.test.ts b/src/render/page.test.ts index 9dd0ce3..cd2dc97 100644 --- a/src/render/page.test.ts +++ b/src/render/page.test.ts @@ -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", () => { diff --git a/src/render/page.ts b/src/render/page.ts index 11e19cd..868b458 100644 --- a/src/render/page.ts +++ b/src/render/page.ts @@ -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
${bodyContent}
+

Times shown in ${escapeHtml(TIMEZONE_CONFIG[prefs.tz].label)}

voidzero Community Radio

@@ -82,6 +84,3 @@ function buildLink(path: string, prefs: Record): string { return qs ? `${path}?${qs}` : path; } -function escapeHtml(s: string): string { - return s.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """); -} diff --git a/src/render/show-detail.ts b/src/render/show-detail.ts index c8ec49b..1695b87 100644 --- a/src/render/show-detail.ts +++ b/src/render/show-detail.ts @@ -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, """); -} diff --git a/src/render/utils.ts b/src/render/utils.ts new file mode 100644 index 0000000..80a7d43 --- /dev/null +++ b/src/render/utils.ts @@ -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, """); +} + +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; +} diff --git a/src/render/week-grid.ts b/src/render/week-grid.ts index 11af7a5..1fe0864 100644 --- a/src/render/week-grid.ts +++ b/src/render/week-grid.ts @@ -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, """); -} - 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; -}