diff --git a/src/app/(site)/[locale]/news/[id]/page.tsx b/src/app/(site)/[locale]/news/[id]/page.tsx new file mode 100644 index 0000000..1703143 --- /dev/null +++ b/src/app/(site)/[locale]/news/[id]/page.tsx @@ -0,0 +1,39 @@ +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 +
+ + {n.place &&

{n.place}

} + {image && {n.title}} + {(n.body || n.summary || t.tbc).split(/\n\s*\n/).map((text, index) =>

{text}

)} + {source &&

{t.sections.source}

} + {t.news.title} +
+
+} diff --git a/src/app/(site)/[locale]/news/page.tsx b/src/app/(site)/[locale]/news/page.tsx new file mode 100644 index 0000000..eb5c45b --- /dev/null +++ b/src/app/(site)/[locale]/news/page.tsx @@ -0,0 +1,30 @@ +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' + +export async function generateMetadata({ params }: { params: Promise<{ locale: string }> }) { + const { locale } = await params + if (!isLocale(locale)) notFound() + const t = getDict(locale) + return { title: `${t.news.title} — ${t.siteName}`, description: t.metaDescription, alternates: { languages: Object.fromEntries(locales.map(l => [l, `/${l}/news`])) } } +} +export default async function Page({ params }: { params: Promise<{ locale: string }> }) { + const { locale } = await params + if (!isLocale(locale)) notFound() + const t = getDict(locale) + const news = visibleNews(await loadNews(locale)) + if (!news.length) notFound() + const fmt = new Intl.DateTimeFormat(locale, { dateStyle: 'long' }) + return + + +} diff --git a/src/app/(site)/[locale]/page.tsx b/src/app/(site)/[locale]/page.tsx index b0e09a4..4dfb291 100644 --- a/src/app/(site)/[locale]/page.tsx +++ b/src/app/(site)/[locale]/page.tsx @@ -101,7 +101,7 @@ export default async function Home({ params }: { params: Promise<{ locale: strin
{n.kind === 'event' ? t.news.event : t.news.news}
-

{n.title}

+

{n.title}

{n.summary &&

{n.summary}

} {n.place &&

{n.place}

}
diff --git a/src/app/(site)/[locale]/projects/ermek-serkebayev/page.tsx b/src/app/(site)/[locale]/projects/ermek-serkebayev/page.tsx index 4e1e6a6..4865e58 100644 --- a/src/app/(site)/[locale]/projects/ermek-serkebayev/page.tsx +++ b/src/app/(site)/[locale]/projects/ermek-serkebayev/page.tsx @@ -5,6 +5,7 @@ import { notFound } from 'next/navigation' import { Header } from '@/components/Header' import { Footer } from '@/components/Footer' import { getDict, isLocale } from '@/i18n' +import { loadContent } from '@/lib/content' import { bookUrl } from '@/lib/home-model' const path = '/projects/ermek-serkebayev' @@ -20,9 +21,10 @@ export default async function BookProject({ params }: { params: Promise<{ locale const { locale } = await params if (!isLocale(locale)) notFound() const t = getDict(locale) + const { news } = await loadContent(locale) return <> -
-
+
0} path={path} /> +
{t.projects.title}
{t.book.cover} diff --git a/src/collections/News.ts b/src/collections/News.ts index ecc182c..25a0ba9 100644 --- a/src/collections/News.ts +++ b/src/collections/News.ts @@ -7,6 +7,9 @@ export const News: CollectionConfig = { access: { read: () => true }, fields: [ { name: 'title', label: 'Заголовок', type: 'text', required: true, localized: true }, + { name: 'body', label: 'Полный текст', type: 'textarea', localized: true }, + { name: 'imageUrl', label: 'Адрес фотографии (https)', type: 'text' }, + { name: 'sourceUrl', label: 'Ссылка на публикацию', type: 'text', localized: true }, { name: 'summary', label: 'Краткое описание', type: 'textarea', localized: true }, { name: 'kind', diff --git a/src/components/LegalPage.tsx b/src/components/LegalPage.tsx index 16c56b9..625d9b7 100644 --- a/src/components/LegalPage.tsx +++ b/src/components/LegalPage.tsx @@ -6,7 +6,7 @@ import { Footer } from '@/components/Footer' import { Header } from '@/components/Header' import { Tbc } from '@/components/Tbc' import { getDict, isLocale, locales } from '@/i18n' -import { loadLegal } from '@/lib/content' +import { loadContent, loadLegal } from '@/lib/content' import { buildLegalModel, type LegalKind } from '@/lib/legal-model' type Props = { params: Promise<{ locale: string }> } @@ -29,13 +29,14 @@ export async function LegalPage({ params, kind }: Props & { kind: LegalKind }) { const { locale } = await params if (!isLocale(locale)) notFound() const t = getDict(locale) + const { news } = await loadContent(locale) const m = buildLegalModel(await loadLegal(locale), kind, t) const fmt = new Intl.DateTimeFormat(locale, { day: 'numeric', month: 'long', year: 'numeric' }) return ( <> -
-
+
0} path={`/${kind}`} /> +
← {t.legal.back}

{m.title}

{m.updated && ( diff --git a/src/lib/content.ts b/src/lib/content.ts index 5add362..b4a4d05 100644 --- a/src/lib/content.ts +++ b/src/lib/content.ts @@ -1,5 +1,6 @@ import config from '@payload-config' import { getPayload } from 'payload' +import { visibleNews } from './news-model' import type { Locale } from '@/i18n' import type { CmsHome, CmsNews } from './home-model' import type { CmsLegal } from './legal-model' @@ -10,9 +11,9 @@ export async function loadContent(locale: Locale): Promise<{ home: CmsHome | nul const payload = await getPayload({ config }) const [home, news] = await Promise.all([ payload.findGlobal({ slug: 'home', locale, fallbackLocale: false }), - payload.find({ collection: 'news', locale, sort: '-date', limit: 3 }), + loadNews(locale), ]) - return { home: home as unknown as CmsHome, news: news.docs as unknown as CmsNews[] } + return { home: home as unknown as CmsHome, news: visibleNews(news).slice(0, 3) } } catch (e) { console.error('content load failed', e) return { home: null, news: [] } @@ -29,3 +30,16 @@ export async function loadLegal(locale: Locale): Promise { return null } } + +/** Все новости текущего языка; не ограничиваем каталог тремя карточками главной. */ +export async function loadNews(locale: Locale): Promise { + const payload = await getPayload({ config }) + const items: CmsNews[] = [] + let page = 1 + while (true) { + const result = await payload.find({ collection: 'news', locale, fallbackLocale: false, sort: '-date', limit: 100, page }) + items.push(...result.docs as unknown as CmsNews[]) + if (!result.hasNextPage) return items + page++ + } +} diff --git a/src/lib/home-model.ts b/src/lib/home-model.ts index e8ebc3b..d9ee98e 100644 --- a/src/lib/home-model.ts +++ b/src/lib/home-model.ts @@ -1,3 +1,4 @@ +import { visibleNews } from './news-model' import type { Dict } from '@/i18n' export type CmsItem = { title?: string | null; text?: string | null } @@ -8,7 +9,7 @@ export type CmsHome = { collaboration?: string | null contactEmail?: string | null } -export type CmsNews = { id: string | number; title: string; summary?: string | null; kind?: string | null; date: string; place?: string | null } +export type CmsNews = { id: string | number; title: string; summary?: string | null; kind?: string | null; date: string; place?: string | null; body?: string | null; imageUrl?: string | null; sourceUrl?: string | null } export type Card = { title: string; text: string | null; tbc: boolean } export type HomeModel = { @@ -38,7 +39,7 @@ export function buildHomeModel(cms: CmsHome | null, news: CmsNews[], t: Dict): H mission: { text: mission ?? t.mission.text, tbc: false }, directions, projects, - news, + news: visibleNews(news), collaboration: { text: clean(cms?.collaboration) ?? t.collaboration.text, email: clean(cms?.contactEmail) ?? foundationEmail }, } } diff --git a/src/lib/news-model.test.ts b/src/lib/news-model.test.ts new file mode 100644 index 0000000..1988aa1 --- /dev/null +++ b/src/lib/news-model.test.ts @@ -0,0 +1,16 @@ +import { describe, it, expect } from 'vitest' +import { visibleNews, safeWebUrl } from './news-model' + +describe('Материалы новостей', () => { + it('Материал опубликован: сохраняет дату и полный текст', () => { + const item = { id: 1, title: 'Презентация книги', date: '2026-11-17', body: 'Полный текст' } + expect(visibleNews([item])).toEqual([item]) + }) + it('Нет перевода: скрывает материал без заголовка текущего языка', () => { + expect(visibleNews([{ id: 1, title: '', date: '2026-11-17' }])).toEqual([]) + }) + it('Пустой раздел остаётся пустым', () => expect(visibleNews([])).toEqual([])) + it('Повреждённая дата не ломает страницу', () => expect(visibleNews([{ id: 1, title: 'Новость', date: 'bad' }])).toEqual([])) + it.each(['javascript:alert(1)', 'data:text/html,test', '/relative', 'https://user:password@example.org'])('Небезопасная ссылка %s скрыта', value => expect(safeWebUrl(value)).toBeNull()) + it('Источник открывает обычную веб-страницу', () => expect(safeWebUrl('https://example.org/article')).toBe('https://example.org/article')) +}) diff --git a/src/lib/news-model.ts b/src/lib/news-model.ts new file mode 100644 index 0000000..7d20f49 --- /dev/null +++ b/src/lib/news-model.ts @@ -0,0 +1,12 @@ +import type { CmsNews } from './home-model' + +export function safeWebUrl(value?: string | null): string | null { + if (!value?.trim()) return null + try { + const url = new URL(value) + return ['http:', 'https:'].includes(url.protocol) && !url.username && !url.password ? url.href : null + } catch { return null } +} +export function visibleNews(items: CmsNews[]): CmsNews[] { + return items.filter(item => item.title?.trim() && Number.isFinite(Date.parse(item.date))) +} diff --git a/src/migrations/20261117_170000_news_details.ts b/src/migrations/20261117_170000_news_details.ts new file mode 100644 index 0000000..0ca2474 --- /dev/null +++ b/src/migrations/20261117_170000_news_details.ts @@ -0,0 +1,13 @@ +import { type MigrateUpArgs, type MigrateDownArgs, sql } from '@payloadcms/db-sqlite' + +export async function up({ db }: MigrateUpArgs): Promise { + await db.run(sql`ALTER TABLE news ADD COLUMN image_url text`) + await db.run(sql`ALTER TABLE news_locales ADD COLUMN body text`) + await db.run(sql`ALTER TABLE news_locales ADD COLUMN source_url text`) +} + +export async function down({ db }: MigrateDownArgs): Promise { + await db.run(sql`ALTER TABLE news DROP COLUMN image_url`) + await db.run(sql`ALTER TABLE news_locales DROP COLUMN body`) + await db.run(sql`ALTER TABLE news_locales DROP COLUMN source_url`) +} diff --git a/src/migrations/index.ts b/src/migrations/index.ts index 683012f..cf0d0ad 100644 --- a/src/migrations/index.ts +++ b/src/migrations/index.ts @@ -1,9 +1,11 @@ +import * as migration_news_details from './20261117_170000_news_details'; import * as migration_20261117_120000_legal_texts from './20261117_120000_legal_texts'; import * as migration_20261116_103502_init from './20261116_103502_init'; import * as migration_20261117_045758_legal from './20261117_045758_legal'; export const migrations = [ + { up: migration_20261116_103502_init.up, down: migration_20261116_103502_init.down, @@ -15,4 +17,5 @@ export const migrations = [ name: '20261117_045758_legal' }, { up: migration_20261117_120000_legal_texts.up, down: migration_20261117_120000_legal_texts.down, name: '20261117_120000_legal_texts' }, + { up: migration_news_details.up, down: migration_news_details.down, name: '20261117_170000_news_details' }, ]; diff --git a/src/payload-types.ts b/src/payload-types.ts index 7efece0..d283973 100644 --- a/src/payload-types.ts +++ b/src/payload-types.ts @@ -156,6 +156,9 @@ export interface User { export interface News { id: number; title: string; + body?: string | null; + imageUrl?: string | null; + sourceUrl?: string | null; summary?: string | null; kind?: ('news' | 'event') | null; date: string; @@ -266,6 +269,9 @@ export interface UsersSelect { */ export interface NewsSelect { title?: T; + body?: T; + imageUrl?: T; + sourceUrl?: T; summary?: T; kind?: T; date?: T;