Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@kentcdodds/pie-night

Pie night sign-ups: hosts start an event, guests add the pies they are bringing and watch the list fill in live. Installable, with optional push notifications.

FollowForkOpen app

Files

page.ts

257 lines · 15.6 KB · TypeScript
/** Server-rendered pages. The event list and forms are drawn by public/app.js from the boot JSON; home and create by public/home.js. */

import { HERO_SVG } from './art.ts'
import { THEMES } from './pies.ts'

const DESCRIPTION_FALLBACK = "Let us know what pie you're bringing and see what everyone else is bringing."

function escapeHtml(value: string) {
	return value.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#39;')
}

function bootJson(value: unknown) {
	return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029')
}

type Shared = { appBase: string; assetBase: string; site: string; assetVersion: string; limits: Record<string, number> }

type PublicEvent = { slug: string; code: string; title: string; date: string; time: string; place: string; blurb: string; theme: string; version: number; when: { day: string; time: string; short: string; medium: string } }

function head(input: Shared & { title: string; desc: string; ogTitle?: string; ogUrl: string; ogImage: string; ogAlt: string; manifest: string }) {
	const assets = escapeHtml(input.assetBase)
	const v = encodeURIComponent(input.assetVersion)
	return `<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
<meta name="color-scheme" content="light dark" />
<meta name="theme-color" content="#f6c177" media="(prefers-color-scheme: light)" />
<meta name="theme-color" content="#24160e" media="(prefers-color-scheme: dark)" />
<title>${escapeHtml(input.title)}</title>
<meta name="description" content="${escapeHtml(input.desc)}" />
<meta name="robots" content="noindex" />
<meta property="og:type" content="website" />
<meta property="og:site_name" content="Pie Night" />
<meta property="og:title" content="${escapeHtml(input.ogTitle ?? input.title)}" />
<meta property="og:description" content="${escapeHtml(input.desc)}" />
<meta property="og:url" content="${escapeHtml(input.ogUrl)}" />
<meta property="og:image" content="${escapeHtml(input.ogImage)}" />
<meta property="og:image:type" content="image/png" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta property="og:image:alt" content="${escapeHtml(input.ogAlt)}" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content="${escapeHtml(input.ogTitle ?? input.title)}" />
<meta name="twitter:description" content="${escapeHtml(input.desc)}" />
<meta name="twitter:image" content="${escapeHtml(input.ogImage)}" />
<link rel="canonical" href="${escapeHtml(input.ogUrl)}" />
<link rel="icon" href="${escapeHtml(input.appBase)}/favicon.svg" type="image/svg+xml" />
<link rel="icon" href="${assets}/icon-192.png" sizes="192x192" type="image/png" />
<link rel="apple-touch-icon" href="${assets}/apple-touch-icon.png" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-title" content="Pie Night" />
<link rel="manifest" href="${escapeHtml(input.manifest)}" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Fredoka:wght@500;600;700&family=Nunito:wght@500;700;800&display=swap" />
<link rel="stylesheet" href="${assets}/styles.css?v=${v}" />`
}

/** Update banner, install button and toast shared by every page (driven by the scripts). */
function chrome() {
	return `<div id="update-bar" class="update-bar" role="status" hidden><span>A fresh batch of Pie Night is ready.</span><button type="button" id="update-btn">Refresh</button></div>
<div id="toast" class="toast" role="status" aria-live="polite" hidden></div>`
}

function script(input: Shared, name: string) {
	return `<script src="${escapeHtml(input.assetBase)}/${name}?v=${encodeURIComponent(input.assetVersion)}" defer></script>`
}

export function eventPageHtml(input: Shared & {
	event: PublicEvent
	pies: unknown[]
	mode: 'guest' | 'admin' | 'not-admin' | 'sign-in'
	visitor: { username: string } | null
	isHost: boolean
	loginUrl: string
	pushKey: string
}) {
	const { event } = input
	const eventPath = `${input.appBase}/e/${event.slug}`
	const where = event.place ? ` ${event.place}.` : ''
	const desc = `${event.when.day} at ${event.when.time}.${where} ${DESCRIPTION_FALLBACK}`
	const ogImage = `${input.site}/e/${event.slug}/og.png?v=${event.version}`
	const theme = event.theme in THEMES ? event.theme : 'harvest'
	return `<!doctype html>
<html lang="en">
<head>
${head({ ...input, title: `${event.title} 🥧 ${event.when.day}`, ogTitle: `${event.title} 🥧 ${event.when.medium} · ${event.when.time}`, desc, ogUrl: `${input.site}/e/${event.slug}`, ogImage, ogAlt: `A pumpkin pie with whipped cream beside the words ${event.title}, ${event.when.medium}, ${event.when.time}${event.place ? `, ${event.place}` : ''}`, manifest: `${eventPath}/manifest.webmanifest` })}
</head>
<body data-mode="${input.mode}" data-theme="${escapeHtml(theme)}">
<div class="sprinkles" aria-hidden="true"></div>
<main class="wrap">
	<header class="hero">
		<div class="hero-pie" aria-hidden="true">${HERO_SVG}</div>
		<p class="kicker">You're invited to</p>
		<h1 id="event-title">${escapeHtml(event.title)}</h1>
		<div class="when">
			<span class="chip chip-day">📅 <span id="event-day">${escapeHtml(event.when.day)}</span></span>
			<span class="chip chip-time">⏰ <span id="event-time">${escapeHtml(event.when.time)}</span></span>
			<span class="chip chip-where" id="event-where-chip"${event.place ? '' : ' hidden'}>🏡 <span id="event-where">${escapeHtml(event.place)}</span></span>
		</div>
		<p class="blurb" id="event-blurb">${escapeHtml(event.blurb)}</p>
		<p class="count" id="pie-count" aria-live="polite"></p>
		<div id="presence" class="presence" hidden>
			<div class="presence-people" id="presence-people"></div>
			<p class="presence-caption"><span id="presence-caption"></span> <span id="live-badge" class="live-badge" hidden>Live</span></p>
			<p class="typing-caption" id="typing-caption" aria-live="polite"></p>
		</div>
	</header>

	<section id="admin" class="card admin-card" hidden></section>

	<section class="card form-card" aria-labelledby="form-title">
		<h2 id="form-title">I'm bringing pie! <span aria-hidden="true">🥧</span></h2>
		<form id="pie-form" novalidate autocomplete="on">
			<label class="field">
				<span>Your name or family</span>
				<input name="who" maxlength="${input.limits.who}" placeholder="The Smith family" autocomplete="name" enterkeyhint="next" required />
			</label>
			<div id="pie-rows" class="pie-rows" data-pie-max="${input.limits.pie}" data-note-max="${input.limits.note}"></div>
			<button type="button" class="add-row-btn" id="add-row"><span aria-hidden="true">+</span> Add another pie</button>
			<label class="hp" aria-hidden="true">Leave this empty<input name="website" tabindex="-1" autocomplete="off" /></label>
			<p class="form-error" role="alert" hidden></p>
			<button class="big-btn" type="submit" id="submit-btn"><span id="submit-label">Add my pie</span> <span aria-hidden="true">🥧</span></button>
		</form>
		<div id="form-done" class="form-done" hidden>
			<div class="done-pies" aria-hidden="true">🥧</div>
			<p class="done-title" id="done-title">Yum! Your pie is on the list.</p>
			<p class="done-sub" id="done-sub"></p>
			<button type="button" class="big-btn alt" id="add-more">Bringing another? Add one more</button>
		</div>
	</section>

	<section id="notify" class="notify-card" hidden aria-live="polite"></section>

	<section class="list-section" aria-labelledby="list-title">
		<h2 id="list-title">Pies on the way</h2>
		<ul id="pie-list" class="pie-list"></ul>
	</section>

	<footer class="foot">
		<p><button type="button" id="install-btn" class="install-btn" hidden>📲 Add to home screen</button></p>
		<p>Made with butter and love. <a href="${escapeHtml(eventPath)}/admin" class="host-link">Hosts</a> · <a href="${escapeHtml(input.appBase)}/new" class="host-link">Plan your own pie night</a></p>
	</footer>
</main>

<dialog id="edit-dialog" class="sheet" aria-labelledby="edit-title">
	<form id="edit-form" method="dialog" novalidate>
		<h2 id="edit-title">Change this pie</h2>
		<label class="field"><span>Name or family</span><input name="who" maxlength="${input.limits.who}" required /></label>
		<label class="field"><span>What pie?</span><input name="pie" maxlength="${input.limits.pie}" required /></label>
		<label class="field"><span>A little note <em>(optional)</em></span><input name="note" maxlength="${input.limits.note}" /></label>
		<label class="hp" aria-hidden="true">Leave this empty<input name="website" tabindex="-1" autocomplete="off" /></label>
		<p class="form-error" role="alert" hidden></p>
		<div class="sheet-actions">
			<button type="button" class="ghost-btn" data-close>Cancel</button>
			<button type="submit" class="big-btn small">Save</button>
		</div>
	</form>
</dialog>

${chrome()}
<script type="application/json" id="boot">${bootJson({ appBase: input.appBase, assetBase: input.assetBase, slug: event.slug, api: `${input.appBase}/api/e/${event.slug}`, eventPath, event, pies: input.pies, mode: input.mode, visitor: input.visitor ? { username: input.visitor.username } : null, isHost: input.isHost, loginUrl: input.loginUrl, pushKey: input.pushKey, limits: input.limits })}</script>
${script(input, 'pwa.js')}
${script(input, 'app.js')}
</body>
</html>`
}

function simplePage(input: Shared & { title: string; desc: string; body: string; boot: unknown; ogUrl: string; scriptName?: string }) {
	return `<!doctype html>
<html lang="en">
<head>
${head({ ...input, ogUrl: input.ogUrl, ogImage: `${publicAssetUrl(input.site, input.appBase, input.assetBase)}/og.png?v=${encodeURIComponent(input.assetVersion)}`, ogAlt: 'A pumpkin pie with whipped cream beside the words Pie Night', manifest: `${input.appBase}/manifest.webmanifest` })}
</head>
<body data-theme="harvest">
<div class="sprinkles" aria-hidden="true"></div>
<main class="wrap">
	<header class="hero hero-small">
		<div class="hero-pie" aria-hidden="true">${HERO_SVG}</div>
		<h1>Pie Night</h1>
	</header>
	${input.body}
</main>
${chrome()}
<script type="application/json" id="boot">${bootJson(input.boot)}</script>
${script(input, 'pwa.js')}
${script(input, 'home.js')}
</body>
</html>`
}

export function homePageHtml(input: Shared & { visitor: { username: string } | null; mine: PublicEvent[]; loginUrl: string }) {
	const mine = input.mine.length
		? `<section class="card"><h2>Your pie nights</h2><ul class="event-list">${input.mine
				.map((e) => `<li><a href="${escapeHtml(input.appBase)}/e/${escapeHtml(e.slug)}"><b>${escapeHtml(e.title)}</b><span>${escapeHtml(e.when.medium)} · ${escapeHtml(e.when.time)} · <code>${escapeHtml(e.slug)}</code></span></a></li>`)
				.join('')}</ul></section>`
		: ''
	const body = `<p class="blurb">Gather your people for pie. Everyone says what they are bringing, sees what is coming, and nobody needs an account.</p>
	<section class="card">
		<h2>Got a pie night code?</h2>
		<form id="code-form" class="code-form"><label class="field"><span>Event code</span><input name="code" placeholder="smith-pie-night-2026" autocomplete="off" autocapitalize="none" required /></label><button class="big-btn small" type="submit">Go</button></form>
	</section>
	<section class="card">
		<h2>Hosting?</h2>
		<p>Start a pie night in a minute. You sign in with Kody so you can change the details later; your guests never need to.</p>
		<a class="big-btn" href="${escapeHtml(input.appBase)}/new">Start a pie night 🥧</a>
	</section>
	${mine}`
	return simplePage({ ...input, title: 'Pie Night', desc: 'Plan a pie night: guests say what pie they are bringing and see everyone else’s.', body, ogUrl: `${input.site}/`, boot: { appBase: input.appBase, page: 'home' } })
}

export function newEventPageHtml(input: Shared & { visitor: { username: string } | null; loginUrl: string }) {
	const themes = Object.entries(THEMES)
		.map(([k, t], i) => `<label class="theme-pick" data-theme="${k}"><input type="radio" name="theme" value="${k}"${i === 0 ? ' checked' : ''} /><span>${escapeHtml(t.label)}</span></label>`)
		.join('')
	const body = input.visitor
		? `<section class="card form-card">
		<h2>Start a pie night 🥧</h2>
		<form id="new-form" novalidate>
			<label class="field"><span>Title</span><input name="title" maxlength="${input.limits.title}" placeholder="Pie Night" required /></label>
			<label class="field"><span>Event code <em>(goes in the link)</em></span><input name="code" maxlength="${input.limits.code}" placeholder="smith-pie-night-2026" autocapitalize="none" autocomplete="off" /></label>
			<p class="hint" id="code-preview"></p>
			<div class="row"><label class="field"><span>Date</span><input type="date" name="date" required /></label><label class="field"><span>Time</span><input type="time" name="time" value="18:00" required /></label></div>
			<label class="field"><span>Where <em>(a friendly label, not an address)</em></span><input name="place" maxlength="${input.limits.place}" placeholder="At our house" /></label>
			<label class="field"><span>Message</span><textarea name="blurb" rows="3" maxlength="${input.limits.blurb}" placeholder="Thanks for helping with pies! Let us know what you're bringing."></textarea></label>
			<fieldset class="field themes"><legend>Theme</legend>${themes}</fieldset>
			<label class="field"><span>Co-hosts <em>(Kody usernames, optional)</em></span><input name="hosts" placeholder="username, another-username" autocapitalize="none" autocomplete="off" /></label>
			<label class="hp" aria-hidden="true">Leave this empty<input name="website" tabindex="-1" autocomplete="off" /></label>
			<p class="form-error" role="alert" hidden></p>
			<button class="big-btn" type="submit">Create pie night</button>
			<p class="hint">Signed in as <b>${escapeHtml(input.visitor.username)}</b>. You will be the host.</p>
		</form>
	</section>`
		: `<section class="card">
		<h2>Hosts sign in first 🔑</h2>
		<p>Sign in with Kody to start a pie night. You need it to change the details later. Guests never sign in.</p>
		<a class="big-btn" href="${escapeHtml(input.loginUrl)}">Sign in with Kody</a>
	</section>`
	return simplePage({ ...input, title: 'Start a pie night', desc: 'Plan a pie night: guests say what pie they are bringing and see everyone else’s.', body, ogUrl: `${input.site}/new`, boot: { appBase: input.appBase, page: 'new' } })
}

export function notFoundHtml(input: Shared & { code: string }) {
	const body = `<section class="card"><h2>No pie night here 🥧</h2><p>${input.code ? `We could not find a pie night with the code <code>${escapeHtml(input.code)}</code>.` : 'That page is not on the menu.'} Double-check the link, or try the code again.</p>
	<form id="code-form" class="code-form"><label class="field"><span>Event code</span><input name="code" autocomplete="off" autocapitalize="none" required /></label><button class="big-btn small" type="submit">Go</button></form></section>`
	return simplePage({ ...input, title: 'Pie night not found', desc: 'Pie Night', body, ogUrl: `${input.site}/`, boot: { appBase: input.appBase, page: 'missing' } })
}

export function offlineHtml(input: Shared) {
	const body = `<section class="card"><h2>You're offline 🥧</h2><p>This pie night page has not been saved on this device yet. Connect to the internet and try again. Pages you have opened before still work offline.</p></section>`
	return simplePage({ ...input, title: 'Offline', desc: 'Pie Night', body, ogUrl: `${input.site}/`, boot: { appBase: input.appBase, page: 'offline' } })
}

function publicAssetUrl(site: string, appBase: string, assetBase: string) {
	if (/^https?:\/\//.test(assetBase)) return assetBase
	const origin = appBase && site.endsWith(appBase) ? site.slice(0, site.length - appBase.length) : site
	return origin + assetBase
}