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:
parent
a943ecd3a3
commit
00170ca912
29 changed files with 322 additions and 27 deletions
119
server/src/routes/countdown.js
Normal file
119
server/src/routes/countdown.js
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
/* ═══════════════════════════════════════════════════════════════
|
||||
COUNTDOWN IMAGE: GET /api/countdown/:id.png
|
||||
|
||||
A newsletter's countdown block (src/lib/newsletter.ts) is this
|
||||
picture: the time left until a published one-off event starts,
|
||||
drawn when the email is opened, since an email can't run code.
|
||||
Gmail fetches images once through its proxy and keeps them, so
|
||||
there it shows the time of the first open; the block's fixed line
|
||||
under the image covers that and clients that block images.
|
||||
|
||||
?bg=003a45 the band's color (hex, no #)
|
||||
?fg=ffffff the text's color
|
||||
?t=... the line above the numbers (default: the event's title)
|
||||
|
||||
An event starts at local midnight on starts_on in TIME_ZONE (events
|
||||
carry a date, not a time) and is over when ends_on (or starts_on)
|
||||
is; the picture says which. A series has no one start to count
|
||||
to, so it isn't offered.
|
||||
|
||||
Each picture is kept for the minute it shows, a few hundred at most,
|
||||
so a newsletter opened by everyone at once draws it once a minute.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { Hono } from "hono";
|
||||
import sharp from "sharp";
|
||||
|
||||
// The organization's home time zone (Unity Village, Missouri).
|
||||
export const TIME_ZONE = "America/Chicago";
|
||||
const MAX_KEPT = 300;
|
||||
|
||||
const countdown = new Hono();
|
||||
const kept = new Map(); // key → png Buffer
|
||||
|
||||
const hex = (value, fallback) => (/^[0-9a-f]{6}$/i.test(value ?? "") ? `#${value}` : fallback);
|
||||
const xml = (value) =>
|
||||
String(value).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
||||
|
||||
/* The instant a date's local midnight is in a time zone, as ms. */
|
||||
export function zonedMidnight(date, timeZone = TIME_ZONE) {
|
||||
const [y, m, d] = date.split("-").map(Number);
|
||||
const guess = Date.UTC(y, m - 1, d);
|
||||
// How far the zone's clock is from UTC at that instant, twice so a
|
||||
// clock change between the guess and the answer is caught.
|
||||
const offset = (at) => {
|
||||
const parts = Object.fromEntries(
|
||||
new Intl.DateTimeFormat("en-US", { timeZone, hourCycle: "h23", year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric" })
|
||||
.formatToParts(at)
|
||||
.map((p) => [p.type, Number(p.value)]),
|
||||
);
|
||||
return Date.UTC(parts.year, parts.month - 1, parts.day, parts.hour, parts.minute, parts.second) - at;
|
||||
};
|
||||
const first = guess - offset(guess);
|
||||
return guess - offset(first);
|
||||
}
|
||||
|
||||
/* Days, hours and minutes left, or null once it's started. */
|
||||
export function remaining(target, now = Date.now()) {
|
||||
const left = target - now;
|
||||
if (left <= 0) return null;
|
||||
const minutes = Math.floor(left / 60_000);
|
||||
return { days: Math.floor(minutes / 1440), hours: Math.floor(minutes / 60) % 24, minutes: minutes % 60 };
|
||||
}
|
||||
|
||||
const W = 1104; // twice the email's 552px, for sharp screens
|
||||
const H = 280;
|
||||
const FONT = "Poppins, Montserrat, Helvetica, Arial, sans-serif";
|
||||
|
||||
function svg({ label, left, over, bg, fg }) {
|
||||
const top = `<text x="${W / 2}" y="68" text-anchor="middle" font-family="${FONT}" font-size="34" font-weight="700" fill="${fg}">${xml(label)}</text>`;
|
||||
if (!left) {
|
||||
return `<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H}"><rect width="100%" height="100%" fill="${bg}"/>${top}
|
||||
<text x="${W / 2}" y="190" text-anchor="middle" font-family="${FONT}" font-size="72" font-weight="700" fill="${fg}">${over ? "Thanks for joining us" : "Happening now"}</text></svg>`;
|
||||
}
|
||||
const cells = [
|
||||
[left.days, left.days === 1 ? "DAY" : "DAYS"],
|
||||
[left.hours, left.hours === 1 ? "HOUR" : "HOURS"],
|
||||
[left.minutes, left.minutes === 1 ? "MINUTE" : "MINUTES"],
|
||||
]
|
||||
.map(([n, unit], i) => {
|
||||
const x = (W / 3) * i + W / 6;
|
||||
return `<text x="${x}" y="182" text-anchor="middle" font-family="${FONT}" font-size="96" font-weight="700" fill="${fg}">${String(n).padStart(2, "0")}</text>
|
||||
<text x="${x}" y="232" text-anchor="middle" font-family="${FONT}" font-size="24" font-weight="700" letter-spacing="3" fill="${fg}" fill-opacity="0.8">${unit}</text>`;
|
||||
})
|
||||
.join("");
|
||||
return `<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H}"><rect width="100%" height="100%" fill="${bg}"/>${top}${cells}</svg>`;
|
||||
}
|
||||
|
||||
countdown.get("/countdown/:file", async (c) => {
|
||||
const id = /^(.+)\.png$/.exec(c.req.param("file"))?.[1];
|
||||
const event = id
|
||||
? c
|
||||
.get("db")
|
||||
.prepare(`SELECT title, starts_on, ends_on FROM events WHERE id = ? AND is_published = 1 AND is_series = 0 AND starts_on IS NOT NULL`)
|
||||
.get(id)
|
||||
: null;
|
||||
if (!event) return c.json({ error: "No such event" }, 404);
|
||||
|
||||
const bg = hex(c.req.query("bg"), "#003a45");
|
||||
const fg = hex(c.req.query("fg"), "#ffffff");
|
||||
const label = (c.req.query("t") ?? "").trim().slice(0, 60) || event.title.slice(0, 60);
|
||||
const left = remaining(zonedMidnight(event.starts_on));
|
||||
// Over once the last day's midnight has passed.
|
||||
const over = !left && Date.now() >= zonedMidnight(event.ends_on ?? event.starts_on) + 86_400_000;
|
||||
const key = JSON.stringify([id, bg, fg, label, left, over]);
|
||||
|
||||
let png = kept.get(key);
|
||||
if (!png) {
|
||||
png = await sharp(Buffer.from(svg({ label, left, over, bg, fg }))).png().toBuffer();
|
||||
if (kept.size >= MAX_KEPT) kept.delete(kept.keys().next().value);
|
||||
kept.set(key, png);
|
||||
}
|
||||
return c.body(png, 200, {
|
||||
"Content-Type": "image/png",
|
||||
// Asked for again at every open, where the client allows it.
|
||||
"Cache-Control": "no-cache, max-age=0, must-revalidate",
|
||||
});
|
||||
});
|
||||
|
||||
export default countdown;
|
||||
Loading…
Add table
Add a link
Reference in a new issue