/* ═══════════════════════════════════════════════════════════════ BLOG POST: /blog/:id One of our own posts: cover, date and tags, the content-block body, then its authors as person tiles. Authors are published people (linked to their pages) and, when the post names one, a guest with no people record (a tile that links nowhere). There's no /blog index on purpose: the Community page's blog section is where posts are listed, so "back" goes there. Tab blocks (079) split the body into tabs (contents.ts, splitTabs): the blocks before the first are an intro above them, and the contents list follows the open tab. A link to a heading in another tab (#tab-…) opens that tab. Printed (Download PDF) or in reading mode, every tab shows, each under its title. ═══════════════════════════════════════════════════════════════ */ import { useEffect, useId, useState, type KeyboardEvent } from "react" import { Link, useLocation, useParams } from "react-router-dom" import PageShell, { type ShellSection } from "../components/PageShell.tsx" import PageState from "../components/PageState.tsx" import ContentBlocks from "../components/ContentBlocks.tsx" import PeopleTiles from "../components/PeopleTiles.tsx" import { mediaUrl } from "../lib/media.ts" import { postDate, useBlogPost } from "../lib/useBlog.ts" import { accentText, BRAND, readableOn, withAlpha } from "../lib/color.ts" import { useReadingMode } from "../lib/readingMode.ts" import { jumpTo } from "../lib/focus.ts" import PostContents from "../components/PostContents.tsx" import { headingAnchors, splitTabs, type PostTab } from "../lib/contents.ts" import QrCode from "../components/QrCode.tsx" import RequestLink from "../components/RequestLink.tsx" import { siteUrl } from "../lib/site.ts" import { FilePdf } from "@phosphor-icons/react" const BODY = "var(--color-muted)" /* Where "back" goes: the section on the Community page. */ const BACK = { to: "/community#blog", label: "Community Blog" } export default function BlogPost() { const { id } = useParams() const { data: post, loading, error, notFound, reload } = useBlogPost(id) const [tabIndex, setTabIndex] = useState(0) const reading = useReadingMode() const { hash } = useLocation() // A link into another tab (#tab-…) opens it, then goes to the heading. useEffect(() => { if (!post || !hash.startsWith("#tab-")) return const target = decodeURIComponent(hash.slice(1)) const found = splitTabs(post.blocks).tabs const index = found.findIndex((tab) => target === tab.id || target.startsWith(tab.prefix)) if (index < 0) return setTabIndex(index) // The tab's own id is its printed title, hidden on screen: opening // it is enough. A heading inside it is scrolled to once it shows. if (target !== found[index].id) requestAnimationFrame(() => jumpTo(target)) }, [post, hash]) if (!post) { return ( ) } const { intro, tabs } = splitTabs(post.blocks) const current = Math.min(tabIndex, Math.max(tabs.length - 1, 0)) // Two headings or more make a list worth showing: the intro's and // the open tab's. const contents = post.show_contents ? tabs.length ? [...headingAnchors(intro).entries, ...headingAnchors(tabs[current].blocks, tabs[current].prefix).entries] : headingAnchors(post.blocks).entries : [] const withContents = contents.length >= 2 const postUrl = siteUrl(`/blog/${post.id}`) const date = postDate(post.published_on) const cover = mediaUrl(post.cover_image) const byline = [ ...post.authors.map((author) => author.name), ...(post.guest ? [post.guest.name] : []), ].join(", ") const authors = [ ...post.authors.map((author) => ({ id: author.id, name: author.name, tagline: author.tagline, pronouns: author.pronouns, photo: mediaUrl(author.photo), })), ...(post.guest ? [ { name: post.guest.name, title: "Guest author", photo: mediaUrl(post.guest.photo), }, ] : []), ] const sections: ShellSection[] = [ { id: "post", title: "Post", accent: BRAND, content: (
{withContents && }
{date && ( )} {byline && By {byline}} {post.tags.map((tag) => ( {tag} ))} {BACK.label}
{cover && ( {post.cover_alt )} {tabs.length ? ( <> ) : ( )}
Have a story to share?
{/* Only on paper (Download PDF): where the post lives online, as a link and as a code to scan. */}

Read this post online at
{postUrl}

), }, ] if (authors.length > 0) { sections.push({ id: "authors", title: authors.length === 1 ? "About the author" : "About the authors", accent: BRAND, content: (
), }) } return ( ) } /* The post's tabs: a row of buttons, then the open tab's blocks. The others stay in the page, hidden, so print shows them all; `all` (reading mode) shows every tab, each under its title. */ function PostTabs({ tabs, current, onSelect, all, }: { tabs: PostTab[] current: number onSelect: (index: number) => void all: boolean }) { const baseId = useId() // Arrows move one tab and wrap; Home and End jump to the ends. const onKey = (e: KeyboardEvent) => { const last = tabs.length - 1 const next = e.key === "ArrowRight" ? (current + 1) % tabs.length : e.key === "ArrowLeft" ? (current + last) % tabs.length : e.key === "Home" ? 0 : e.key === "End" ? last : null if (next === null) return e.preventDefault() onSelect(next) document.getElementById(`${baseId}-tab-${next}`)?.focus() } return (
{!all && (
{tabs.map((tab, index) => { const selected = index === current return ( ) })}
)} {tabs.map((tab, index) => (

{tab.title}

))}
) }