From 2a926971f7eb2acef4135139172af238a14529d7 Mon Sep 17 00:00:00 2001 From: Zaldimmar Date: Wed, 30 Sep 2026 22:38:04 -0500 Subject: [PATCH] =?UTF-8?q?A=20newsletter=20builder=20(Tools=20=E2=86=92?= =?UTF-8?q?=20Newsletter)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tick upcoming events, published blog posts and the front page's featured cards, write a title and intro, and copy an email-ready newsletter (table layout, inline styles, the palette in hex, a plain-text copy alongside) into Mailchimp, Gmail or Church Center, or download it. Picked posts are read from their public pages, so only live posts make it in; links and images point at the live site. Nothing is sent or stored. The builder is src/lib/newsletter.ts. Co-Authored-By: Claude Opus 5.5 --- src/App.tsx | 2 + src/lib/newsletter.ts | 147 ++++++++++++++ src/pages/admin/AdminNewsletter.tsx | 292 ++++++++++++++++++++++++++++ src/pages/admin/adminNav.ts | 5 + 4 files changed, 446 insertions(+) create mode 100644 src/lib/newsletter.ts create mode 100644 src/pages/admin/AdminNewsletter.tsx diff --git a/src/App.tsx b/src/App.tsx index fc432c7..43ae74f 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -80,6 +80,7 @@ const AdminHome = lazyPage(() => import("./pages/admin/AdminHome.tsx")) const AdminFeedback = lazyPage(() => import("./pages/admin/AdminFeedback.tsx")) const AdminMedia = lazyPage(() => import("./pages/admin/AdminMedia.tsx")) const AdminClips = lazyPage(() => import("./pages/admin/AdminClips.tsx")) +const AdminNewsletter = lazyPage(() => import("./pages/admin/AdminNewsletter.tsx")) const EntityList = lazyPage(() => import("./pages/admin/EntityList.tsx")) const EntityEdit = lazyPage(() => import("./pages/admin/EntityEdit.tsx")) @@ -133,6 +134,7 @@ export default function App() { } /> } /> } /> + } /> }> } /> diff --git a/src/lib/newsletter.ts b/src/lib/newsletter.ts new file mode 100644 index 0000000..17ba14e --- /dev/null +++ b/src/lib/newsletter.ts @@ -0,0 +1,147 @@ +/* ═══════════════════════════════════════════════════════════════ + NEWSLETTER: email-ready HTML and plain text from picked items + + The admin's Newsletter tool (AdminNewsletter.tsx) gathers events, + blog posts and featured cards; this turns them into one email to + paste into Mailchimp, Gmail or Church Center. Nothing is sent or + stored here. + + Email clients ignore stylesheets, CSS variables and most layout, + so the HTML is tables with inline styles at 600px, images with + width and alt, and the palette written out as hex (the one place + the site's colors are literals: an inbox has no theme tokens). A + plain-text copy goes alongside for clients that show no HTML. + ═══════════════════════════════════════════════════════════════ */ + +export type NewsletterItem = { + title: string + /** A line under the title: dates and place, a byline. */ + meta?: string | null + text?: string | null + /** An absolute image URL. */ + image?: string | null + /** An absolute link. */ + href: string + cta: string +} + +export type NewsletterSection = { heading: string; items: NewsletterItem[] } + +export type Newsletter = { + title: string + intro?: string | null + sections: NewsletterSection[] + /** The site's address, for the header link and footer. */ + origin: string + siteName: string + /** An absolute URL of the logo for the header. */ + logo?: string | null +} + +/* The palette as an inbox needs it. */ +const COLOR = { + ink: '#001011', + body: '#003a45', + muted: '#3b5456', + teal: '#00748a', + page: '#f5f9f8', + card: '#ffffff', + rule: '#d6f5fa', +} + +const FONT = "'Poppins', Helvetica, Arial, sans-serif" +const BODY_FONT = "'Montserrat', Helvetica, Arial, sans-serif" + +const escape = (value: string) => + value.replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"') + +/* Paragraphs from text with blank lines between them. */ +const paragraphs = (text: string, style: string) => + text + .split(/\n\s*\n/) + .map((part) => part.trim()) + .filter(Boolean) + .map((part) => `

${escape(part).replace(/\n/g, '
')}

`) + .join('') + +function itemHtml(item: NewsletterItem): string { + const image = item.image + ? `` + : '' + const meta = item.meta + ? `

${escape(item.meta)}

` + : '' + const text = item.text + ? paragraphs(item.text, `margin:12px 0 0;font-family:${BODY_FONT};font-size:15px;line-height:24px;color:${COLOR.body};`) + : '' + return ` + + + ${image} + +
+

${escape(item.title)}

+ ${meta}${text} +

${escape(item.cta)}

+
+` +} + +export function newsletterHtml(n: Newsletter): string { + const sections = n.sections + .filter((section) => section.items.length > 0) + .map( + (section) => ` + +

${escape(section.heading)}

+${section.items.map(itemHtml).join('')}`, + ) + .join('') + + const logo = n.logo + ? `${escape(n.siteName)}` + : `${escape(n.siteName)}` + + return ` + + + + +${escape(n.title)} + + + + +
+ + + + ${sections} + +
${logo}
+

${escape(n.title)}

+ ${n.intro ? paragraphs(n.intro, `margin:12px 0 0;font-family:${BODY_FONT};font-size:16px;line-height:26px;color:${COLOR.body};`) : ''} +
+ ${escape(n.siteName)}
${escape(n.origin.replace(/^https?:\/\//, ''))} +
+
+ +` +} + +export function newsletterText(n: Newsletter): string { + const lines = [n.title, '='.repeat(Math.min(n.title.length, 60)), ''] + if (n.intro) lines.push(n.intro.trim(), '') + for (const section of n.sections) { + if (section.items.length === 0) continue + lines.push(section.heading.toUpperCase(), '') + for (const item of section.items) { + lines.push(item.title) + if (item.meta) lines.push(item.meta) + if (item.text) lines.push(item.text.trim()) + lines.push(`${item.cta}: ${item.href}`, '') + } + } + lines.push('--', n.siteName, n.origin) + return lines.join('\n') +} diff --git a/src/pages/admin/AdminNewsletter.tsx b/src/pages/admin/AdminNewsletter.tsx new file mode 100644 index 0000000..069f42d --- /dev/null +++ b/src/pages/admin/AdminNewsletter.tsx @@ -0,0 +1,292 @@ +/* ═══════════════════════════════════════════════════════════════ + NEWSLETTER (Tools) + + Builds an email newsletter from what's already on the site: tick + upcoming events, published blog posts and the front page's + featured cards, write a title and an intro, and copy the result, + email-ready HTML or plain text, into Mailchimp, Gmail or Church + Center (or download it as a file). Nothing is sent or saved here; + the builder is src/lib/newsletter.ts. + + Picked blog posts are read from their public pages, so only a + post visitors can open makes it in, with its excerpt and cover. + Links and images are the live site's (SITE_ORIGIN), since an + email is read far from this admin. + ═══════════════════════════════════════════════════════════════ */ + +import { Check, Copy, DownloadSimple } from '@phosphor-icons/react' +import { useEffect, useMemo, useState } from 'react' + +import { get } from '../../lib/api.ts' +import { useEvents } from '../../lib/eventData.ts' +import { useFrontPage, type FeaturedCard } from '../../lib/useFrontPage.ts' +import type { BlogPostRecord } from '../../lib/useBlog.ts' +import { blogHref, eventHref, refHref } from '../../lib/hrefs.ts' +import { mediaUrl } from '../../lib/media.ts' +import { SITE_NAME, SITE_ORIGIN, siteUrl } from '../../lib/site.ts' +import { seriesLabel } from '../../lib/eventSeries.ts' +import { useWideAdminPage } from '../../lib/adminWidth.tsx' +import { newsletterHtml, newsletterText, type NewsletterItem } from '../../lib/newsletter.ts' + +const MUTED = 'var(--color-muted)' +const input = + 'w-full border border-muted/25 bg-surface px-3 py-2 text-sm text-body outline-none focus:border-brand focus:ring-2 focus:ring-brand/25' +const button = + 'btn inline-flex items-center gap-2 border border-brand px-4 py-2 text-sm text-link transition-colors hover:bg-tint disabled:opacity-50' + +/* An address on the live site, whatever it started as. */ +const absolute = (path: string | null | undefined) => (path ? siteUrl(path) : null) +const imageFor = (file: string | null | undefined) => absolute(mediaUrl(file)) + +type PostRow = { id: string; title: string; published_on?: string | null; is_published: number; is_listed: number } + +function featuredHref(card: FeaturedCard): string | null { + if (card.kind === 'record' && card.ref) return refHref(card.ref.kind, card.ref.id, card.ref.org_kind) + return card.url +} + +export default function AdminNewsletter() { + useWideAdminPage() + const [title, setTitle] = useState('') + const [intro, setIntro] = useState('') + const [pickedEvents, setPickedEvents] = useState>(new Set()) + const [pickedPosts, setPickedPosts] = useState>(new Set()) + const [pickedCards, setPickedCards] = useState>(new Set()) + const [posts, setPosts] = useState(null) + const [postDetails, setPostDetails] = useState>({}) + const [copied, setCopied] = useState<'html' | 'text' | null>(null) + + const { events, loading: eventsLoading } = useEvents({ status: 'upcoming' }) + const { data: frontPage } = useFrontPage() + const cards = frontPage?.featured ?? [] + + useEffect(() => { + get('/admin/blog_posts?limit=200', { ttl: 0 }) + .then((data: { rows: PostRow[] }) => setPosts(data.rows.filter((row) => row.is_published && row.is_listed))) + .catch(() => setPosts([])) + }, []) + + // A picked post's public page: its excerpt and cover, and proof it's live. + useEffect(() => { + for (const id of pickedPosts) { + if (postDetails[id]) continue + get(`/blog/${encodeURIComponent(id)}`) + .then((data: { post: BlogPostRecord }) => setPostDetails((current) => ({ ...current, [id]: data.post }))) + .catch(() => {}) + } + }, [pickedPosts, postDetails]) + + const toggle = (set: Set, value: T, update: (next: Set) => void) => { + const next = new Set(set) + if (next.has(value)) next.delete(value) + else next.add(value) + update(next) + } + + const newsletter = useMemo(() => { + const eventItems: NewsletterItem[] = events + .filter((event) => pickedEvents.has(event.id)) + .map((event) => ({ + title: event.title, + meta: [seriesLabel(event.series, event.starts_on) ?? event.date_label, event.is_online && !event.location_label ? 'Online' : event.location_label] + .filter(Boolean) + .join(', '), + text: event.tagline ?? event.description?.[0] ?? null, + image: imageFor(event.card_image), + href: siteUrl(eventHref(event.id)), + cta: 'Event details', + })) + + const postItems: NewsletterItem[] = (posts ?? []) + .filter((row) => pickedPosts.has(row.id) && postDetails[row.id]) + .map((row) => { + const post = postDetails[row.id] + const byline = post.authors.map((a) => a.name).concat(post.guest ? [post.guest.name] : []).join(', ') + return { + title: post.title, + meta: byline ? `By ${byline}` : null, + text: post.excerpt ?? null, + image: imageFor(post.cover_image), + href: siteUrl(blogHref(post.id)), + cta: 'Read the post', + } + }) + + const cardItems: NewsletterItem[] = cards + .map((card, index) => ({ card, index, href: featuredHref(card) })) + .filter(({ index, href }) => pickedCards.has(index) && href) + .map(({ card, href }) => ({ + title: card.title ?? 'Featured', + meta: card.eyebrow ?? null, + text: card.blurb ?? null, + image: card.image ? imageFor(card.image.file) : null, + href: absolute(href) ?? href!, + cta: card.kind === 'video' ? 'Watch' : 'Learn more', + })) + + const content = { + title: title.trim() || `News from ${SITE_NAME}`, + intro: intro.trim() || null, + origin: SITE_ORIGIN, + siteName: SITE_NAME, + // The site's logo is an SVG, which Gmail and Outlook don't show, + // so the header is the site name in type. A PNG logo could go here. + logo: null, + sections: [ + { heading: 'Coming up', items: eventItems }, + { heading: 'From the blog', items: postItems }, + { heading: 'Featured', items: cardItems }, + ], + } + return { html: newsletterHtml(content), text: newsletterText(content), count: eventItems.length + postItems.length + cardItems.length } + }, [title, intro, events, pickedEvents, posts, pickedPosts, postDetails, cards, pickedCards]) + + async function copy(kind: 'html' | 'text') { + try { + await navigator.clipboard.writeText(kind === 'html' ? newsletter.html : newsletter.text) + setCopied(kind) + window.setTimeout(() => setCopied(null), 2000) + } catch { + setCopied(null) + } + } + + function download() { + const blob = new Blob([newsletter.html], { type: 'text/html' }) + const link = document.createElement('a') + link.href = URL.createObjectURL(blob) + link.download = `newsletter-${new Date().toISOString().slice(0, 10)}.html` + link.click() + URL.revokeObjectURL(link.href) + } + + return ( +
+
+

Newsletter

+

+ Pick what to include, then copy the email into Mailchimp, Gmail or Church Center. +

+
+ +
+
+
+ +