qazaq-legacy-foundation-web/src/lib/site-model.ts
Peppa e60e7ce464
All checks were successful
peppa/lint lint: success за 2с
peppa/typecheck typecheck: success за 2с
peppa/test test: success за 1с
peppa/build build: success за 50с
ci / checks (pull_request) Successful in 1m38s
deploy / deploy (pull_request) Successful in 18s
QLFW-77: ряд фото книги в «Наших проектах» и фото из админки
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-19 17:16:07 +05:00

48 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/** Модель данных из панели управления: проекты, новости, «О фонде», контакты. Чистые функции — их проверяют тесты. */
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; bookPhotos?: { photo?: CmsMedia }[] | 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))
.sort((a, b) => (a.order ?? 100) - (b.order ?? 100) || a.title.localeCompare(b.title))
}
/** Фото книги: четыре основных, затем добавленные в админке (без подписей, порядок сохранён). */
export function bookPhotoList(s: CmsSettings, base: { src: string }[]): { src: string }[] {
const extra = (s?.bookPhotos ?? []).map((x) => mediaOf(x.photo)).filter((m): m is { url: string; alt: string } => Boolean(m)).map((m) => ({ src: m.url }))
return [...base, ...extra]
}
/** Текст «О фонде»: пусто — раздел не показывается. */
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)),
}
}