import { bookProjectSlug } from './project-paths' /** Модель данных из панели управления: проекты, новости, «О фонде», контакты. Чистые функции — их проверяют тесты. */ export type CmsMedia = { url?: string | null; alt?: string | null } | number | string | null | undefined export type CmsProject = { id: string | number; slug: string; title?: string | null; summary?: string | null; body?: string | null; cover?: CmsMedia; order?: number | null } export type CmsAbout = { text?: string | null; photo?: CmsMedia } | null export type CmsSettings = { email?: string | null; phone?: string | null; address?: string | null; socials?: { label?: string | null; url?: string | null }[] | null } | null const clean = (v?: string | null) => (v && v.trim() ? v.trim() : null) /** Абзацы: разделяются пустой строкой. */ export const paragraphs = (text?: string | null): string[] => (text ?? '').split(/\n\s*\n/).map((p) => p.trim()).filter(Boolean) /** Фото как объект (с `url`) — иначе null (связь не раскрыта или файл удалён). */ export function mediaOf(m: CmsMedia): { url: string; alt: string } | null { if (!m || typeof m !== 'object' || !m.url) return null return { url: m.url, alt: m.alt?.trim() ?? '' } } /** Проекты, у которых есть название на языке страницы; по порядку, затем по названию. Чужой язык не подставляется. */ export function visibleProjects(list: CmsProject[]): (CmsProject & { title: string })[] { return list .filter((p): p is CmsProject & { title: string } => Boolean(clean(p.title)) && Boolean(p.slug) && p.slug !== bookProjectSlug) .sort((a, b) => (a.order ?? 100) - (b.order ?? 100) || a.title.localeCompare(b.title)) } /** Текст «О фонде»: пусто — раздел не показывается. */ export const aboutParagraphs = (a: CmsAbout): string[] => paragraphs(a?.text) export type Contacts = { email: string | null; phone: string | null; address: string | null; socials: { label: string; url: string }[] } /** Контакты: почта из «Контактов и соцсетей», затем из главной. Соцсети без ссылки http(s) отбрасываются. */ export function buildContacts(s: CmsSettings, homeEmail?: string | null): Contacts { return { email: clean(s?.email) ?? clean(homeEmail), phone: clean(s?.phone), address: clean(s?.address), socials: (s?.socials ?? []) .map((x) => ({ label: clean(x.label) ?? '', url: clean(x.url) ?? '' })) .filter((x) => x.label && /^https?:\/\//.test(x.url)), } }