40 lines
2.2 KiB
TypeScript
40 lines
2.2 KiB
TypeScript
import Link from 'next/link'
|
|
import { notFound } from 'next/navigation'
|
|
import { getDict, isLocale, locales } from '@/i18n'
|
|
import { loadNews } from '@/lib/content'
|
|
import { visibleNews } from '@/lib/news-model'
|
|
import { SectionPage } from '@/components/SectionPage'
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
import { safeWebUrl } from '@/lib/news-model'
|
|
import Image from 'next/image'
|
|
import { cache } from 'react'
|
|
|
|
const findNews = cache(async (locale: 'ru' | 'kk' | 'en', id: string) => visibleNews(await loadNews(locale)).find(n => String(n.id) === id))
|
|
export async function generateMetadata({ params }: { params: Promise<{ locale: string; id: string }> }) {
|
|
const { locale, id } = await params
|
|
if (!isLocale(locale)) notFound()
|
|
const n = await findNews(locale, id)
|
|
if (!n) notFound()
|
|
return { title: `${n.title} — ${getDict(locale).siteName}`, description: n.summary || n.title, alternates: { languages: Object.fromEntries(locales.map(l => [l, `/${l}/news/${id}`])) } }
|
|
}
|
|
export default async function Page({ params }: { params: Promise<{ locale: string; id: string }> }) {
|
|
const { locale, id } = await params
|
|
if (!isLocale(locale)) notFound()
|
|
const t = getDict(locale)
|
|
const n = await findNews(locale, id)
|
|
if (!n) notFound()
|
|
const image = safeWebUrl(n.imageUrl)
|
|
const source = safeWebUrl(n.sourceUrl)
|
|
return <SectionPage t={t} locale={locale} path={`/news/${id}`} title={n.title} showNews>
|
|
<article className="max-w-3xl">
|
|
<time dateTime={n.date}>{new Intl.DateTimeFormat(locale, { dateStyle: 'long' }).format(new Date(n.date))}</time>
|
|
{n.place && <p className="mt-3">{n.place}</p>}
|
|
{image && <Image unoptimized width={1200} height={800} src={image} alt={n.title} loading="lazy" className="mt-8 h-auto max-w-full" />}
|
|
{(n.body || n.summary || t.tbc).split(/\n\s*\n/).map((text, index) => <p key={index} className="mt-6 whitespace-pre-line leading-relaxed">{text}</p>)}
|
|
{source && <p className="mt-8"><a href={source} rel="noopener noreferrer" className="underline underline-offset-4">{t.sections.source}</a></p>}
|
|
<Link href={`/${locale}/news`} className="mt-10 inline-flex min-h-11 items-center underline underline-offset-4">{t.news.title}</Link>
|
|
</article>
|
|
</SectionPage>
|
|
}
|