148 lines
5.6 KiB
TypeScript
148 lines
5.6 KiB
TypeScript
/* ═══════════════════════════════════════════════════════════════
|
||
COMMUNITY BLOG
|
||
|
||
The Community page's blog section (Admin → Pages → Community blog
|
||
section). Two halves, each pinned first and then newest, as
|
||
routes/blog.js decides:
|
||
|
||
our posts cards for our own /blog/:id pages
|
||
community posts featured cards (pinned by hand, or the
|
||
newest from the listed feeds), drawn by
|
||
FeaturedCards, with a button to the Substack
|
||
|
||
The section's heading and blurb come from the same record; the
|
||
page reads them through the same cached request (Community.tsx).
|
||
A half with nothing in it isn't drawn.
|
||
═══════════════════════════════════════════════════════════════ */
|
||
|
||
import { Link } from 'react-router-dom'
|
||
|
||
import FeaturedCards from '../../components/FeaturedCards.tsx'
|
||
import PlatformIcon from '../../components/PlatformIcon.tsx'
|
||
import { blogHref } from '../../lib/hrefs.ts'
|
||
import { mediaUrl } from '../../lib/media.ts'
|
||
import { preview } from '../../lib/text.ts'
|
||
import { postDate, useCommunityBlog, type BlogPostSummary } from '../../lib/useBlog.ts'
|
||
import HomeLink from './home/HomeLink.tsx'
|
||
import { accentText, BRAND } from '../../lib/color.ts'
|
||
import { useSmallImage } from '../../lib/useSmallImage.ts'
|
||
|
||
/* Headings for a half the admin didn't title. */
|
||
const DEFAULTS = {
|
||
internal: 'From NGU',
|
||
external: 'From our community',
|
||
externalLabel: 'Read more on Substack',
|
||
}
|
||
|
||
const EXCERPT_CHARS = 180
|
||
|
||
/* One post as a card; person pages list their posts with it too. */
|
||
export function PostCard({ post, accent }: { post: BlogPostSummary; accent: string }) {
|
||
const cover = useSmallImage(mediaUrl(post.cover_image))
|
||
const date = postDate(post.published_on)
|
||
const authors = post.authors.map((author) => author.name).join(', ')
|
||
|
||
return (
|
||
<li className="relative flex flex-col overflow-hidden border border-brand/15 bg-surface shadow-sm transition-shadow hover:shadow-lg">
|
||
{!cover.failed ? (
|
||
<img src={cover.src} alt="" loading="lazy" onError={cover.onError} className="aspect-video w-full object-cover" />
|
||
) : (
|
||
<div className="aspect-video w-full bg-tint" />
|
||
)}
|
||
<div className="flex flex-1 flex-col p-6">
|
||
{(date || post.tags.length > 0) && (
|
||
<p className="text-sm font-semibold" style={{ color: accentText(accent) }}>
|
||
{[date, ...post.tags].filter(Boolean).join(' · ')}
|
||
</p>
|
||
)}
|
||
<h4 className="mt-2 font-display text-xl font-bold leading-snug text-ink">
|
||
<Link
|
||
to={blogHref(post.id)}
|
||
className="after:absolute after:inset-0 after:content-[''] hover:underline"
|
||
>
|
||
{post.title}
|
||
</Link>
|
||
</h4>
|
||
{post.excerpt && (
|
||
<p className="mt-2 leading-relaxed text-muted">{preview(post.excerpt, EXCERPT_CHARS)}</p>
|
||
)}
|
||
{authors && <p className="mt-auto pt-4 text-sm font-medium text-body">By {authors}</p>}
|
||
</div>
|
||
</li>
|
||
)
|
||
}
|
||
|
||
function Subheading({ children, accent }: { children: string; accent: string }) {
|
||
return (
|
||
<h3 className="text-2xl font-bold" style={{ color: accentText(accent) }}>
|
||
{children}
|
||
</h3>
|
||
)
|
||
}
|
||
|
||
export default function CommunityBlog({ accent = BRAND }: { accent?: string }) {
|
||
const { data: blog, loading, error, reload } = useCommunityBlog()
|
||
|
||
if (!blog) {
|
||
return (
|
||
<div className="mx-auto max-w-6xl px-6 text-muted">
|
||
{loading ? (
|
||
'Loading…'
|
||
) : (
|
||
<>
|
||
Couldn’t load the blog. {error}{' '}
|
||
<button type="button" onClick={reload} className="underline" style={{ color: accentText(accent) }}>
|
||
Try again
|
||
</button>
|
||
</>
|
||
)}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
const { internal, external } = blog
|
||
const hasInternal = internal.posts.length > 0
|
||
const hasExternal = external.cards.length > 0 || Boolean(external.url)
|
||
|
||
if (!hasInternal && !hasExternal) {
|
||
return <p className="mx-auto max-w-6xl px-6 text-center text-muted">· Posts coming soon ·</p>
|
||
}
|
||
|
||
return (
|
||
<div className="mx-auto max-w-6xl space-y-16 px-6">
|
||
{hasInternal && (
|
||
<div>
|
||
<Subheading accent={accent}>{internal.title || DEFAULTS.internal}</Subheading>
|
||
<ul className="mt-6 grid gap-6 sm:grid-cols-2 lg:grid-cols-3 lg:[&>li:first-child]:col-span-2">
|
||
{internal.posts.map((post) => (
|
||
<PostCard key={post.id} post={post} accent={accent} />
|
||
))}
|
||
</ul>
|
||
</div>
|
||
)}
|
||
|
||
{hasExternal && (
|
||
<div>
|
||
<div className="flex flex-wrap items-end gap-4">
|
||
<Subheading accent={accent}>{external.title || DEFAULTS.external}</Subheading>
|
||
{external.url && (
|
||
<HomeLink
|
||
url={external.url}
|
||
className="ml-auto inline-flex items-center gap-2 btn px-5 py-2 text-sm text-white transition-transform hover:scale-105"
|
||
style={{ background: accent }}
|
||
>
|
||
<PlatformIcon platform={/substack\.com/i.test(external.url) ? 'substack' : null} />
|
||
{external.label || DEFAULTS.externalLabel}
|
||
</HomeLink>
|
||
)}
|
||
</div>
|
||
{external.cards.length > 0 && (
|
||
<div className="mt-6">
|
||
<FeaturedCards cards={external.cards} />
|
||
</div>
|
||
)}
|
||
</div>
|
||
)}
|
||
</div>
|
||
)
|
||
}
|