Newsletter: social bar and event countdown parts, full-width builder

- Social bar: a colored band of the site's platform icons, linked;
  no links added shows the footer's (src/data/socialLinks.ts, shared)
- Icons are white and ink PNGs in public/email/, made from
  PlatformIcon.tsx by scripts/email-icons.mjs (no SVG in Gmail/Outlook)
- Event countdown: a picture drawn by /api/countdown/<id>.png when the
  email is opened (days, hours, minutes to local midnight in
  America/Chicago; then happening now; then over), with a fixed line
  under it as of copying
- The builder takes the whole window (useWideAdminPage('full'))

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-10-02 00:43:28 -05:00
parent a943ecd3a3
commit 00170ca912
29 changed files with 322 additions and 27 deletions

View file

@ -3,7 +3,8 @@
The admin column is max-w-5xl. The record editor wants more (its
section nav sits to the left of a form that keeps that width), and
so does the admin home (three cards across).
so does the admin home (three cards across): "wide". The newsletter
builder takes the whole window, its editor beside a preview: "full".
Like the title (adminTitle.tsx), the page asks and the layout
decides, so AdminLayout stays the one place that sets <main>'s
width.
@ -14,14 +15,16 @@
import { createContext, useContext, useEffect } from "react";
export const AdminWidthContext = createContext<((wide: boolean) => void) | null>(null);
export type AdminWidth = "normal" | "wide" | "full";
export function useWideAdminPage() {
const setWide = useContext(AdminWidthContext);
export const AdminWidthContext = createContext<((width: AdminWidth) => void) | null>(null);
export function useWideAdminPage(width: Exclude<AdminWidth, "normal"> = "wide") {
const setWidth = useContext(AdminWidthContext);
useEffect(() => {
if (!setWide) return undefined;
setWide(true);
return () => setWide(false);
}, [setWide]);
if (!setWidth) return undefined;
setWidth(width);
return () => setWidth("normal");
}, [setWidth, width]);
}

View file

@ -14,6 +14,12 @@
bar, and the button label
footer the closing lines (blank: the site's name and
address) and the rule above them
social a band of the site's platform icons, each linked
(PNGs in public/email/: scripts/email-icons.mjs)
countdown a picture of the time left until a picked event,
drawn by the API when the email is opened
(server/src/routes/countdown.js), with a fixed line
under it, as of copying, for when images don't load
any block drawn as its email-safe equivalent: headings,
paragraphs (with **bold**, *italic* and [links](url)),
callouts, quotes, lists, link and button rows, images,
@ -31,6 +37,7 @@
═══════════════════════════════════════════════════════════════ */
import { readableOn } from './color.ts'
import { PLATFORMS } from './platforms.ts'
export type NewsletterItem = {
title: string
@ -70,12 +77,25 @@ export type NewsletterPart = {
rule?: string | null
/** The bar down the left of a picks section's cards. */
bar?: string | null
/** A social bar's links, in order. */
socials?: Array<{ platform?: string | null; url?: string | null }>
/** A countdown's event. */
event?: string | null
}
/* What a countdown needs of its event. */
export type CountdownEvent = { title: string; starts_on?: string | null; href: string }
export type Newsletter = {
parts: NewsletterPart[]
/** A picks part's items, ready to draw (picked, published, in order). */
itemsFor: (part: NewsletterPart) => NewsletterItem[]
/** A countdown's event, if it's one that can be counted to. */
eventFor: (id: string) => CountdownEvent | null
/** Now, for the countdown's fixed line. */
now: number
/** A social bar with no links of its own shows these (the footer's). */
defaultSocials: Array<{ platform: string; url: string }>
/** The site's address, for the header link and footer. */
origin: string
siteName: string
@ -296,6 +316,53 @@ function footerHtml(part: NewsletterPart, n: Newsletter): string {
</div></td></tr>`
}
/* The site's platform icons as PNGs, white or ink to read on the band. */
const platformLabel = (id: string) => PLATFORMS.find((p) => p.id === id)?.label ?? id
const socialsOf = (part: NewsletterPart, n: Newsletter) =>
(part.socials?.length ? part.socials : n.defaultSocials).filter((link) => link.platform && link.url)
const iconUrl = (n: Newsletter, platform: string, band: string | null) =>
`${n.origin}/email/${encodeURIComponent(platform)}-${band && readableOn(band) === '#ffffff' ? 'white' : 'ink'}.png`
function socialHtml(part: NewsletterPart, n: Newsletter): string {
const band = colorOf(part.color, COLOR.body)
const links = socialsOf(part, n)
.map((link) => ({ platform: link.platform!, url: safeHref(link.url!, n) }))
.filter((link) => link.url)
if (links.length === 0) return ''
const icons = links
.map(
(link) =>
`<a href="${escape(link.url!)}" style="display:inline-block;margin:0 8px;"><img src="${escape(iconUrl(n, link.platform, band))}" width="24" height="24" alt="${escape(platformLabel(link.platform))}" style="display:block;width:24px;height:24px;border:0;"></a>`,
)
.join('')
return `<tr><td style="padding:24px 24px 0;"><div style="padding:16px 16px;text-align:${alignOf(part)};${band ? `background:${band};` : ''}">${icons}</div></td></tr>`
}
/* "12 days to go", as of `now`: an event starts at its date's
midnight. Null once it has, or with no date. */
export function countdownLine(event: CountdownEvent, now: number): string | null {
if (!event.starts_on) return null
const [y, m, d] = event.starts_on.split('-').map(Number)
const days = Math.ceil((new Date(y, m - 1, d).getTime() - now) / 86_400_000)
if (days <= 0) return null
return days === 1 ? '1 day to go' : `${days} days to go`
}
function countdownHtml(part: NewsletterPart, n: Newsletter): string {
const event = part.event ? n.eventFor(part.event) : null
if (!event || !part.event) return ''
const band = colorOf(part.color, COLOR.body) ?? COLOR.page
const label = part.text?.trim() || event.title
const query = new URLSearchParams({ bg: band.slice(1), fg: readableOn(band).slice(1), t: label })
const src = `${n.origin}/api/countdown/${encodeURIComponent(part.event)}.png?${query}`
const line = countdownLine(event, n.now) ?? 'Happening now'
return `<tr><td style="padding:24px 24px 0;text-align:${alignOf(part)};">
<a href="${escape(event.href)}">${imageTag(src, `${label}: ${line}`)}</a>
<p style="margin:8px 0 0;font-family:${FONT};font-size:15px;line-height:22px;font-weight:700;color:${COLOR.ink};">${escape(line)}: <a href="${escape(event.href)}" style="color:${COLOR.teal};">${escape(event.title)}</a></p>
</td></tr>`
}
function partHtml(part: NewsletterPart, n: Newsletter): string {
switch (part.type) {
case 'header':
@ -306,6 +373,10 @@ function partHtml(part: NewsletterPart, n: Newsletter): string {
return picksHtml(part, n)
case 'footer':
return footerHtml(part, n)
case 'social':
return socialHtml(part, n)
case 'countdown':
return countdownHtml(part, n)
default: {
const html = blockHtml(part, n)
return html ? `<tr><td style="padding:0 24px;text-align:${alignOf(part)};">${html}</td></tr>` : ''
@ -395,6 +466,15 @@ function partText(part: NewsletterPart, n: Newsletter): string[] {
}
case 'footer':
return ['--', ...(part.text?.trim() ? [part.text.trim()] : [n.siteName, n.origin]), '']
case 'social': {
const lines = socialsOf(part, n).map((link) => `${platformLabel(link.platform!)}: ${link.url}`)
return lines.length ? [...lines, ''] : []
}
case 'countdown': {
const event = part.event ? n.eventFor(part.event) : null
if (!event) return []
return [`${countdownLine(event, n.now) ?? 'Happening now'}: ${event.title}`, event.href, '']
}
default:
return blockText(part, n)
}