/** HTML for the single MEGA Radio page. Runtime-free (no kody:* imports) so it renders the same everywhere. */ import { DISCORD_CLIENT_ID, GUILD_INVITE, GUILD_NAME, MEMBERSHIP_DAYS, SIGNUP_URL } from './config.ts' import { CSS } from './styles.ts' import type { Origins } from './origins.ts' import { onRepeat, playsTip, type Plays } from './stats.ts' import { nn, seedView, type View } from './tracks.ts' export const esc = (s: unknown) => String(s ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c]!) export type PageState = | { kind: 'signed-out'; loginHref: string } | { kind: 'unverified' username: string logoutHref: string /** last check said "not a member" */ notMember?: { discordName: string | null } /** last membership has lapsed */ expired?: boolean error?: string } | { kind: 'member'; username: string; logoutHref: string; discordName: string | null; expiresAt: string; radio?: View; art?: Record; plays?: Record } /** Browser-tab icon (inline SVG favicon + apple-touch-icon), so no asset route is needed. */ const ICON_HEAD = `` /** * Render the page shell. * * @param base - app base path (no trailing slash) * @param body - inner HTML for `
` * @param right - header-right HTML * @returns a full HTML document */ function shell(base: string, body: string, right = ''): string { return ` MEGA Radio${ICON_HEAD}
MEGA Radio
${right}
${body}
` } type Action = { label: string; href?: string; post?: string; ghost?: boolean; danger?: boolean; newTab?: boolean; opensDialog?: string } /** * One step card (title, one-line subtitle, full-width pill * buttons, small centered fine print) in this page's own type. Every step and state uses it. * * @param id - id for the title (aria-labelledby) * @param title - short title (rendered monospace uppercase) * @param sub - one-line subtitle (HTML, already escaped) * @param actions - buttons, first one is usually the primary pill * @param fine - fine print (HTML, already escaped), optional * @returns HTML */ function stepCard(id: string, title: string, sub: string, actions: Action[], fine = ''): string { const buttons = actions .map((a) => { const cls = `pill ${a.danger ? 'danger' : a.ghost ? 'ghost' : 'kody'}` if (a.opensDialog) return `` if (a.post) return `
` return `${esc(a.label)}` }) .join('') return `` } /** Step 1: sign in with Kody. */ function signInCard(loginHref: string): string { return stepCard( 'signin-title', 'Sign in to listen', `Sign in, then confirm you're in the ${esc(GUILD_NAME)} Discord.`, [{ label: 'Sign in with Kody', href: loginHref }, { label: 'Create a Free Account', href: SIGNUP_URL, ghost: true, newTab: true }], `New accounts open Kody in a new tab; come back here and tap Sign in with Kody.
One Discord check unlocks the radio for ${MEMBERSHIP_DAYS} days.`, ) } const privacyFine = `We ask Discord once, keep only "member: yes/no" and your Discord name, and throw the token away.
A check is good for ${MEMBERSHIP_DAYS} days.` /** Step 2: confirm Discord membership (first time, lapsed, not a member, or after an error). */ function discordCard(base: string, s: Extract): string { const start = `${base}/discord/start` if (!DISCORD_CLIENT_ID) return stepCard('discord-title', 'Confirm your Discord', 'Discord sign-in is not set up yet (no Discord client ID).', []) if (s.error) return stepCard('discord-title', 'Something went wrong', esc(s.error), [{ label: 'Check with Discord', href: start }], privacyFine) if (s.notMember) { const who = s.notMember.discordName ? ` ${esc(s.notMember.discordName)}` : '' return stepCard( 'discord-title', 'Not in the Discord yet', `Your Discord account${who} isn't in the ${esc(GUILD_NAME)} server.`, [{ label: `Join the ${GUILD_NAME} Discord`, href: GUILD_INVITE, newTab: true }, { label: 'Check again', href: start, ghost: true }], `Join the server, then tap Check again.
${privacyFine}`, ) } if (s.expired) return stepCard('discord-title', 'Check your Discord again', `Your last check is more than ${MEMBERSHIP_DAYS} days old.`, [{ label: 'Check with Discord', href: start }], privacyFine) return stepCard('discord-title', 'Confirm your Discord', `We'll check you're in the ${esc(GUILD_NAME)} Discord.`, [{ label: 'Check with Discord', href: start }], privacyFine) } const MONTHS = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.'] /** * Format a date as "Nov. 27" in America/New_York ("May" has no period); the year is added only when it * differs from the current year there ("Jan. 3, 2027"). * * @param iso - an ISO timestamp * @param now - the current time (for the year comparison) * @returns the short date */ export function shortDate(iso: string, now = new Date()): string { const parts = (d: Date) => { const p = Object.fromEntries(new Intl.DateTimeFormat('en-US', { timeZone: 'America/New_York', year: 'numeric', month: 'numeric', day: 'numeric' }).formatToParts(d).map((x) => [x.type, x.value])) return { y: Number(p.year), m: Number(p.month), d: Number(p.day) } } const t = parts(new Date(iso)) const base = `${MONTHS[t.m - 1]} ${t.d}` return t.y === parts(now).y ? base : `${base}, ${t.y}` } /** Track list with players (members only). */ function trackList(base: string, radio: View, art: Record = {}, plays: Record = {}): string { const items = radio.tracks .map((t) => { const n = nn(t.n) if (!t.released) return `
  • ${n} · ${esc(t.title)}opens ${esc(t.opens ?? 'soon')}
  • ` const src = `${base}/files/radio/${n}` const p = plays[t.n] ?? { total: 0, mine: 0 } const tip = playsTip(p) const stat = `${esc(onRepeat(p.mine))}` return `
  • ${t.length ? `${esc(t.length)} · ` : ''}${stat}
  • ` }) .join('') const repeat = `` return `

    ${esc(radio.album)}

    MEGA Radio · ${radio.tracks.filter((t) => t.released).length} of ${radio.tracks.length} tracks open

    Pick a track
    ${repeat}
      ${items}
    ` } /** * Destructive "forget my Discord check" step. The pill only opens a confirm dialog. * The POST form is submitted by the dialog's Forget button; Cancel is the default (autofocus, and the dialog form's only submit button). * * @param base - app base path * @returns the step card, dialog, and the inert POST form */ function forgetCard(base: string): string { const card = stepCard( 'forget-title', 'Your Discord check', '', [{ label: 'Forget My Discord Check…', danger: true, opensDialog: 'forget-dialog' }], `Forgetting removes your saved check; you'll need to check again to listen.`, ) return `${card}

    Forget Your Discord Check?

    Your saved check will be removed. You'll need to check again with Discord before you can listen.

    ` } /** * Render the page for the visitor's current state. * * @param base - app base path * @param s - who is visiting and what we know about their membership * @returns HTML */ export function renderPage(base: string, s: PageState, now = new Date()): string { const intro = `

    The WAKE album from the ${esc(GUILD_NAME)} course, for members of the ${esc(GUILD_NAME)} Discord.

    ` if (s.kind === 'signed-out') { return shell(base, `${intro}${signInCard(s.loginHref)}`) } const right = `${esc(s.username)}Sign Out` if (s.kind === 'member') { const memberIntro = `

    The WAKE album from the ${esc(GUILD_NAME)} course, for members of the ${esc(GUILD_NAME)} Discord, which now includes you! You’re validated through ${esc(shortDate(s.expiresAt, now))}.

    ` return shell(base, `${memberIntro}${trackList(base, s.radio ?? seedView(), s.art, s.plays)}${forgetCard(base)}`, right) } return shell(base, `${intro}${discordCard(base, s)}`, right) } /** Plain error page. */ export const errorPage = (base: string, message: string) => shell(base, stepCard('error-title', 'Something went wrong', esc(message), [{ label: 'Start again', href: `${base}/` }])) /** * Put the old/new origin pair on for the move script (src/move.ts). * * @param html - a page from renderPage, errorPage or movePage * @param o - the origin pair (src/origins.ts) * @returns the page with data-old-origin, data-new-origin and data-old-base */ export function withOrigins(html: string, o: Origins): string { return html.replace(/

    Bring your MEGA Radio data

    If you used MEGA Radio at the old address (noah.kody.run/mega-radio), your repeat setting can come with you. Use Move my data on the old address, or import the file it downloaded.

    `, ) }