qazaq-legacy-foundation-web/src/lib/home-model.ts

53 lines
2.4 KiB
TypeScript

import type { Dict } from '@/i18n'
export type CmsItem = { title?: string | null; text?: string | null }
export type CmsHome = {
mission?: string | null
directions?: CmsItem[] | null
projects?: CmsItem[] | null
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 Card = { title: string; text: string | null; tbc: boolean }
export type HomeModel = {
mission: { text: string; tbc: boolean }
directions: Card[]
projects: Card[]
news: CmsNews[]
collaboration: { text: string; email: string | null }
}
export const foundationEmail = 'qazaqlegacy@gmail.com'
export const bookUrl = 'https://www.flip.kz/catalog?prod=6364653&utm_source=ig&utm_medium=social&utm_content=link_in_bio'
const clean = (v?: string | null) => (v && v.trim() ? v.trim() : null)
/** Собирает данные главной: редакторские значения дополняются подтверждёнными текстами словаря. */
export function buildHomeModel(cms: CmsHome | null, news: CmsNews[], t: Dict): HomeModel {
const cmsDirs = (cms?.directions ?? []).filter((d) => clean(d.title))
const directions: Card[] = cmsDirs.length
? cmsDirs.map((d) => ({ title: d.title!.trim(), text: clean(d.text), tbc: !clean(d.text) }))
: t.directions.items.map((d) => ({ title: d.title, text: d.text, tbc: false }))
const projects: Card[] = (cms?.projects ?? [])
.filter((p) => clean(p.title))
.map((p) => ({ title: p.title!.trim(), text: clean(p.text), tbc: !clean(p.text) }))
const mission = clean(cms?.mission)
return {
mission: { text: mission ?? t.mission.text, tbc: false },
directions,
projects,
news,
collaboration: { text: clean(cms?.collaboration) ?? t.collaboration.text, email: clean(cms?.contactEmail) ?? foundationEmail },
}
}
const escapeHtml = (s: string) => s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;')
/** Ссылка на почту в обёртке email_off: Cloudflare не подменяет адрес на «[email protected]». */
export function emailLinkHtml(email: string, className: string): string {
const e = escapeHtml(email)
return `<!--email_off--><a href="mailto:${e}" class="${escapeHtml(className)}">${e}</a><!--/email_off-->`
}