diff --git a/src/render/week-grid.test.ts b/src/render/week-grid.test.ts new file mode 100644 index 0000000..10b3ed2 --- /dev/null +++ b/src/render/week-grid.test.ts @@ -0,0 +1,96 @@ +import { describe, it, expect } from "vitest"; +import { renderWeekGrid } from "./week-grid"; +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: "https://feed.bowlafterbowl.com/feed.xml", + 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 hsoShow: Show = { + slug: "hyper-spaceout", + name: "Hyper SpaceOut", + shortDescription: "Biweekly game night.", + websiteUrl: "https://hyperspaceout.com", + feedUrl: "", + accentColor: "#c98fff", + hosts: ["MakeHeroism", "Mary-Kate Ultra"], + isSmallGroup: true, + isVcr: false, + isActive: true, + schedule: { day: "sunday", startTime: "19:00", endTime: "22:00", timezone: "America/New_York", recurrence: "biweekly" }, +}; + +const vcrPrefs: Preferences = { tz: "eastern", theme: "system", mode: "vcr", week: "2026-W45", setCookies: [] }; +const allPrefs: Preferences = { tz: "eastern", theme: "system", mode: "all", week: "2026-W45", setCookies: [] }; + +describe("renderWeekGrid", () => { + it("returns HTML containing week-grid class", () => { + const html = renderWeekGrid([babShow], vcrPrefs); + expect(html).toContain("week-grid"); + }); + + it("renders show blocks as links to detail pages", () => { + const html = renderWeekGrid([babShow], vcrPrefs); + expect(html).toContain('href="/show/bowl-after-bowl'); + expect(html).toContain("Bowl After Bowl"); + }); + + it("applies accent color as inline style", () => { + const html = renderWeekGrid([babShow], vcrPrefs); + expect(html).toContain("#f2b705"); + }); + + it("includes day headers (Sun through Sat)", () => { + const html = renderWeekGrid([babShow], vcrPrefs); + expect(html).toContain("Sun"); + expect(html).toContain("Mon"); + expect(html).toContain("Tue"); + expect(html).toContain("Wed"); + expect(html).toContain("Thu"); + expect(html).toContain("Fri"); + expect(html).toContain("Sat"); + }); + + it("filters out non-VCR shows in VCR mode", () => { + const html = renderWeekGrid([babShow, hsoShow], vcrPrefs); + expect(html).toContain("Bowl After Bowl"); + expect(html).not.toContain("Hyper SpaceOut"); + }); + + it("includes all active shows in all mode", () => { + const html = renderWeekGrid([babShow, hsoShow], allPrefs); + expect(html).toContain("Bowl After Bowl"); + expect(html).toContain("Hyper SpaceOut"); + }); + + it("shows VCR badge in all mode for VCR shows", () => { + const html = renderWeekGrid([babShow, hsoShow], allPrefs); + expect(html).toContain("vcr-badge"); + }); + + it("does not show VCR badge in VCR mode", () => { + const html = renderWeekGrid([babShow], vcrPrefs); + expect(html).not.toContain("vcr-badge"); + }); + + it("shows bi-weekly label for biweekly shows", () => { + const html = renderWeekGrid([hsoShow], allPrefs); + expect(html).toContain("bi-weekly"); + }); + + it("includes time labels in the gutter", () => { + const html = renderWeekGrid([babShow], vcrPrefs); + // Should have at least some hour labels + expect(html).toMatch(/\d{1,2}:\d{2}/); + }); +}); diff --git a/src/render/week-grid.ts b/src/render/week-grid.ts new file mode 100644 index 0000000..11af7a5 --- /dev/null +++ b/src/render/week-grid.ts @@ -0,0 +1,244 @@ +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_SHORT = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; + +type DayKey = (typeof DAY_ORDER)[number]; + +interface PlacedShow { + show: Show; + gridColumn: number; + startRow: number; + endRow: number; + startMin: number; + endMin: number; + startDisplay: string; + endDisplay: string; + isLive: boolean; +} + +export function renderWeekGrid(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 todayKey = getTodayDateKey(now, targetZone); + + let gridStartMin = Number.POSITIVE_INFINITY; + let gridEndMin = Number.NEGATIVE_INFINITY; + const placed: PlacedShow[] = []; + + for (const show of filtered) { + const block = computeShowPlacement(show, weekBounds.start, targetZone, now); + if (!block) continue; + + gridStartMin = Math.min(gridStartMin, block.startMin); + gridEndMin = Math.max(gridEndMin, block.endMin); + placed.push(block); + } + + if (!Number.isFinite(gridStartMin)) { + gridStartMin = 8 * 60; + gridEndMin = 22 * 60; + } + + gridStartMin = Math.floor(gridStartMin / 30) * 30; + gridEndMin = Math.ceil(gridEndMin / 30) * 30; + if (gridEndMin <= gridStartMin) { + gridEndMin = gridStartMin + 30; + } + + for (const block of placed) { + block.startRow = minutesToRow(block.startMin, gridStartMin); + block.endRow = minutesToRow(block.endMin, gridStartMin); + } + + const slotCount = (gridEndMin - gridStartMin) / 30; + + let html = '
'; + + html += '
'; + for (let i = 0; i < 7; i++) { + const dayDate = new Date(weekBounds.start); + dayDate.setUTCDate(dayDate.getUTCDate() + i); + const dateNum = dayDate.getUTCDate(); + const todayClass = dateKeyFromUtcDate(dayDate) === todayKey ? " day-today" : ""; + html += `
${DAY_SHORT[i]}
${dateNum}
`; + } + + for (let slot = 0; slot < slotCount; slot++) { + const slotMin = gridStartMin + slot * 30; + const hour = Math.floor(slotMin / 60) % 24; + const minute = slotMin % 60; + const gutterLabel = minute === 0 ? formatTime12(hour, minute) : ""; + html += `
${gutterLabel}
`; + for (let col = 0; col < 7; col++) { + html += '
'; + } + } + + for (const block of placed) { + const { show, gridColumn, startRow, endRow, startDisplay, endDisplay, isLive } = block; + const bg = hexToRgba(show.accentColor, 0.1); + const liveClass = isLive ? " is-live" : ""; + const href = `/show/${encodeURIComponent(show.slug)}?week=${encodeURIComponent(prefs.week)}&tz=${encodeURIComponent(prefs.tz)}`; + + let inner = `${escapeHtml(show.name)}`; + inner += `${startDisplay} – ${endDisplay}`; + if (prefs.mode === "all" && show.isVcr) { + inner += 'VCR'; + } + if (show.schedule.recurrence === "biweekly") { + inner += '(bi-weekly)'; + } + + html += `${inner}`; + } + + html += "
"; + return html; +} + +function computeShowPlacement( + show: Show, + weekStart: Date, + targetZone: string, + now: Date +): PlacedShow | 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 gridColumn = displayDayIdx + 2; + + let startMin = convertedStart.hour * 60 + convertedStart.minute; + let endMin = convertedEnd.hour * 60 + convertedEnd.minute; + + const endDayIdx = (sourceDayIdx + convertedEnd.dayOffset + 7) % 7; + if (isOvernightSource || endDayIdx !== displayDayIdx || endMin <= startMin) { + if (endDayIdx === displayDayIdx && endMin <= startMin) { + endMin += 24 * 60; + } else if (endDayIdx !== displayDayIdx) { + endMin += 24 * 60; + } else if (isOvernightSource) { + endMin += 24 * 60; + } + } + + const startDisplay = formatTime12(convertedStart.hour, convertedStart.minute); + const endDisplay = formatTime12(convertedEnd.hour, convertedEnd.minute); + const isLive = checkIsLive(show, now); + + return { + show, + gridColumn, + startRow: 0, + endRow: 0, + startMin, + endMin, + startDisplay, + endDisplay, + isLive, + }; +} + +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); + const b = parseInt(hex.slice(5, 7), 16); + 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, + year: "numeric", + month: "2-digit", + day: "2-digit", + }).formatToParts(now); + const y = parts.find((p) => p.type === "year")!.value; + const m = parts.find((p) => p.type === "month")!.value; + const d = parts.find((p) => p.type === "day")!.value; + return `${y}-${m}-${d}`; +} + +function dateKeyFromUtcDate(date: Date): string { + const y = date.getUTCFullYear(); + const m = String(date.getUTCMonth() + 1).padStart(2, "0"); + const d = String(date.getUTCDate()).padStart(2, "0"); + 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; +}