diff --git a/src/render/agenda.test.ts b/src/render/agenda.test.ts new file mode 100644 index 0000000..a67cb4e --- /dev/null +++ b/src/render/agenda.test.ts @@ -0,0 +1,58 @@ +import { describe, it, expect } from "vitest"; +import { renderAgenda } from "./agenda"; +import type { Show, Preferences } from "../types"; + +const babShow: Show = { + slug: "bowl-after-bowl", + name: "Bowl After Bowl", + shortDescription: "The flagship Small Group talk show.", + websiteUrl: "https://bowlafterbowl.com", + feedUrl: "", + accentColor: "#f2b705", + hosts: ["Spencer", "DuhLaurien"], + isSmallGroup: true, + isVcr: true, + isActive: true, + schedule: { day: "tuesday", startTime: "22:00", endTime: "01:00+1", timezone: "America/New_York", recurrence: "weekly" }, +}; + +const vcrPrefs: Preferences = { tz: "eastern", theme: "system", mode: "vcr", week: "2026-W45", setCookies: [] }; + +describe("renderAgenda", () => { + it("returns HTML with agenda class", () => { + const html = renderAgenda([babShow], vcrPrefs); + expect(html).toContain("agenda"); + }); + + it("groups shows under day headings", () => { + const html = renderAgenda([babShow], vcrPrefs); + expect(html).toContain("Tuesday"); + }); + + it("renders show entries as links", () => { + const html = renderAgenda([babShow], vcrPrefs); + expect(html).toContain('href="/show/bowl-after-bowl'); + }); + + it("includes time display", () => { + const html = renderAgenda([babShow], vcrPrefs); + expect(html).toMatch(/10:00\s*PM/i); + }); + + it("omits days with no shows", () => { + const html = renderAgenda([babShow], vcrPrefs); + expect(html).not.toContain("Wednesday"); + expect(html).not.toContain("Thursday"); + }); + + it("applies accent color as border style", () => { + const html = renderAgenda([babShow], vcrPrefs); + expect(html).toContain("#f2b705"); + }); + + it("lists overnight shows under their start day", () => { + const html = renderAgenda([babShow], vcrPrefs); + expect(html).toContain("Tuesday"); + expect(html).not.toMatch(/Wednesday.*Bowl After Bowl/s); + }); +}); diff --git a/src/render/agenda.ts b/src/render/agenda.ts new file mode 100644 index 0000000..499ae71 --- /dev/null +++ b/src/render/agenda.ts @@ -0,0 +1,177 @@ +import { TIMEZONE_CONFIG, convertTime } from "../time/zones"; +import { getWeekBounds } from "../time/week"; +import type { Show, Preferences } from "../types"; + +const DAY_ORDER = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"] as const; +const DAY_LABELS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; + +type DayKey = (typeof DAY_ORDER)[number]; + +interface AgendaEntry { + show: Show; + displayDay: DayKey; + startDisplay: string; + endDisplay: string; + startMin: number; + isLive: boolean; +} + +export function renderAgenda(shows: Show[], prefs: Preferences): string { + const filtered = shows.filter((s) => { + if (!s.isActive) return false; + return prefs.mode === "vcr" ? s.isVcr : true; + }); + + const targetZone = TIMEZONE_CONFIG[prefs.tz].iana; + const weekBounds = getWeekBounds(prefs.week, prefs.tz); + const now = new Date(); + + const byDay = new Map(); + + for (const show of filtered) { + const entry = buildAgendaEntry(show, weekBounds.start, targetZone, now); + if (!entry) continue; + + const existing = byDay.get(entry.displayDay) ?? []; + existing.push(entry); + byDay.set(entry.displayDay, existing); + } + + for (const entries of byDay.values()) { + entries.sort((a, b) => a.startMin - b.startMin); + } + + let html = '
'; + + for (let i = 0; i < 7; i++) { + const day = DAY_ORDER[i]; + const entries = byDay.get(day); + if (!entries || entries.length === 0) continue; + + const dayDate = new Date(weekBounds.start); + dayDate.setUTCDate(dayDate.getUTCDate() + i); + const dateLabel = dayDate.toLocaleDateString("en-US", { month: "short", day: "numeric", timeZone: "UTC" }); + + html += '
'; + html += `

${DAY_LABELS[i]}, ${dateLabel}

`; + + for (const { show, startDisplay, endDisplay, isLive } of entries) { + const liveClass = isLive ? " is-live" : ""; + const href = `/show/${encodeURIComponent(show.slug)}?week=${encodeURIComponent(prefs.week)}&tz=${encodeURIComponent(prefs.tz)}`; + const hosts = show.hosts.map((h) => escapeHtml(h)).join(", "); + + html += `"; + } + + html += "
"; + } + + html += "
"; + return html; +} + +function buildAgendaEntry( + show: Show, + weekStart: Date, + targetZone: string, + now: Date +): AgendaEntry | null { + const sourceDayIdx = DAY_ORDER.indexOf(show.schedule.day); + const refDate = new Date(weekStart); + refDate.setUTCDate(refDate.getUTCDate() + sourceDayIdx); + + const [startH, startM] = parseHourMinute(show.schedule.startTime); + const convertedStart = convertTime(startH, startM, show.schedule.timezone, targetZone, refDate); + + const isOvernightSource = show.schedule.endTime.includes("+1"); + const endRaw = show.schedule.endTime.replace("+1", ""); + const [endH, endM] = parseHourMinute(endRaw); + const endRef = new Date(refDate); + if (isOvernightSource) { + endRef.setUTCDate(endRef.getUTCDate() + 1); + } + const convertedEnd = convertTime(endH, endM, show.schedule.timezone, targetZone, endRef); + + const displayDayIdx = (sourceDayIdx + convertedStart.dayOffset + 7) % 7; + const displayDay = DAY_ORDER[displayDayIdx]; + + const startDisplay = formatTime12(convertedStart.hour, convertedStart.minute); + const endDisplay = formatTime12(convertedEnd.hour, convertedEnd.minute); + const startMin = convertedStart.hour * 60 + convertedStart.minute; + const isLive = checkIsLive(show, now); + + return { + show, + displayDay, + startDisplay, + endDisplay, + startMin, + isLive, + }; +} + +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, """); +}