Files
react-website/src/site/blog/ArticlePage.tsx
T
MehboobKandClaude Sonnet 5 410463e0ed Fix admin studio article preview always showing "moved" error
ArticlePage and NewsGrid called useContent() directly, bypassing the
draft content SiteApp resolves for admin previews. Since /admin is
rendered outside ContentProvider, this always returned an empty post
list, so every article in the studio looked unpublished.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-13 12:18:13 +05:00

187 lines
6.9 KiB
TypeScript

import { useEffect, useMemo } from 'react'
import { useNavigate } from 'react-router-dom'
import { formatPostDate, publishedPosts, useContent } from '../../cms/store'
import { applyJsonLd, applyMeta, articleJsonLd } from '../../cms/seo'
import { categoryMeta, splitAuthor } from './NewsGrid'
import type { Post, SiteContent } from '../../cms/types'
/**
* A single article at /blog/<slug>. Rendered into the #pg-article container inside
* the site markup, so it inherits the real nav, footer and enquiry modal — and the
* white-section rule that makes the body copy readable.
*/
export default function ArticlePage({
slug,
suppressMeta = false,
content: contentProp,
}: {
slug: string
suppressMeta?: boolean
/** Admin preview: use the editor's draft rather than the published context. */
content?: SiteContent
}) {
const publishedContent = useContent()
const content = contentProp || publishedContent
const navigate = useNavigate()
// Drafts are reachable by direct link so they can be reviewed before going live,
// but they never appear in listings or the sitemap.
const post = useMemo(() => content.posts.find((p) => p.slug === slug), [content, slug])
const related = useMemo(() => {
const others = publishedPosts(content).filter((p) => p.slug !== slug)
const sameCategory = others.filter((p) => p.category === post?.category)
return [...sameCategory, ...others.filter((p) => p.category !== post?.category)].slice(0, 3)
}, [content, post, slug])
useEffect(() => {
if (suppressMeta) return // the admin preview leaves the document head alone
if (!post) {
applyMeta({
title: 'Article not found | BlackDice Cyber',
description: 'The article you are looking for is no longer available.',
path: `/blog/${slug}`,
siteUrl: content.settings.siteUrl,
robots: 'noindex, follow',
})
applyJsonLd(null)
return
}
applyMeta({
title: post.metaTitle || `${post.title} | BlackDice Cyber`,
description: post.metaDescription || post.excerpt,
path: `/blog/${post.slug}`,
image: post.hero || undefined,
type: 'article',
siteUrl: content.settings.siteUrl,
robots: post.status === 'draft' ? 'noindex, nofollow' : 'index, follow',
})
applyJsonLd(post.status === 'draft' ? null : articleJsonLd(post, content.settings.siteUrl))
}, [post, slug, content.settings.siteUrl, suppressMeta])
if (!post) {
return (
<section className="bd-sect t">
<div className="bd-si" style={{ maxWidth: 720 }}>
<span className="ey">NEWSROOM</span>
<h1 className="h1" style={{ fontSize: 'clamp(28px,3vw,40px)', marginBottom: 16 }}>
That article has moved.
</h1>
<p style={{ color: 'var(--t2)', fontSize: 16, marginBottom: 24 }}>
We could not find an article at <code>/blog/{slug}</code>. It may have been renamed or unpublished.
</p>
<button className="btn-p" onClick={() => navigate('/news')}>
Back to the newsroom
</button>
</div>
</section>
)
}
const cat = categoryMeta(post.category)
const { name, credit } = splitAuthor(post.author)
return (
<>
<section className="bd-hero-sh bd-article-hero">
<div className="bd-hero-gl" />
<div className="bd-si" style={{ position: 'relative', zIndex: 1 }}>
<button className="bd-article-back" onClick={() => navigate('/news')}>
← Newsroom
</button>
<span className={`news-tag ${cat.cls}`} style={{ marginBottom: 16, display: 'inline-block' }}>
{cat.label.toUpperCase()}
</span>
{post.status === 'draft' ? <span className="bd-news-draft" style={{ marginLeft: 8 }}>DRAFT — NOT PUBLISHED</span> : null}
<h1 className="h1 bd-article-title">{post.title}</h1>
<p className="bd-article-meta">
{formatPostDate(post.date)}
{name ? ` · ${name}` : ''}
</p>
</div>
</section>
<section className="bd-sect t">
<div className="bd-si bd-article-wrap">
{post.hero ? (
<figure className="bd-article-hero-img">
<img src={post.hero} alt={post.title} />
{credit ? <figcaption>{credit}</figcaption> : null}
</figure>
) : null}
<div className="bd-article-body" dangerouslySetInnerHTML={{ __html: post.body }} />
<ArticleShare post={post} siteUrl={content.settings.siteUrl} />
</div>
</section>
<section className="bd-sect t">
<div className="bd-si">
<div className="sect-hd">
<span className="ey">KEEP READING</span>
<h2 className="h2">More from BlackDice</h2>
</div>
<div className="news-grid">
{related.map((other) => (
<article key={other.id} className="news-card bd-news-card" onClick={() => navigate(`/blog/${other.slug}`)}>
<span className={`news-tag ${categoryMeta(other.category).cls}`}>
{categoryMeta(other.category).label.toUpperCase()}
</span>
<h3>{other.title}</h3>
<p className="bd-news-meta">{formatPostDate(other.date)}</p>
<p>{other.excerpt}</p>
<span className="bd-news-more">Read more →</span>
</article>
))}
</div>
</div>
</section>
<div className="cta-band">
<div className="cta-band-in">
<div>
<h2 className="h2">
Want to see this working on your network? <em>Talk to us.</em>
</h2>
</div>
<div>
<p>
Tell us about your subscriber base and we will show you the intelligence BlackDice surfaces from within
your own infrastructure.
</p>
<div className="btn-row">
<button className="btn-p" onClick={() => (window as { showDemo?: () => void }).showDemo?.()}>
Request a demonstration
</button>
</div>
</div>
</div>
</div>
</>
)
}
function ArticleShare({ post, siteUrl }: { post: Post; siteUrl: string }) {
const url = `${siteUrl.replace(/\/+$/, '')}/blog/${post.slug}`
const share = [
{ label: 'LinkedIn', href: `https://www.linkedin.com/sharing/share-offsite/?url=${encodeURIComponent(url)}` },
{ label: 'X', href: `https://twitter.com/intent/tweet?url=${encodeURIComponent(url)}&text=${encodeURIComponent(post.title)}` },
]
return (
<div className="bd-article-share">
<span className="ey">SHARE THIS ARTICLE</span>
<div>
{share.map((s) => (
<a key={s.label} href={s.href} target="_blank" rel="noopener">
{s.label}
</a>
))}
<button
onClick={() => {
navigator.clipboard?.writeText(url)
}}>
Copy link
</button>
</div>
</div>
)
}