qazaq-legacy-foundation-web/src/lib/home-model.ts
Peppa d0cda39de0
All checks were successful
peppa/lint lint: success за 2с
peppa/typecheck typecheck: success за 6с
peppa/test test: success за 4с
peppa/build build: success за 58с
ci / checks (pull_request) Successful in 2m13s
deploy / deploy (pull_request) Successful in 21s
QLFW-12: скрыть карточки направлений и проектов без текста
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-17 17:39:37 +05:00

54 lines
2.6 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) && clean(d.text))
const directions: Card[] = cmsDirs.length
? cmsDirs.map((d) => ({ title: d.title!.trim(), text: clean(d.text), tbc: false }))
: t.directions.items.map((d) => ({ title: d.title, text: d.text, tbc: false }))
const projects: Card[] = (cms?.projects ?? [])
.filter((p) => clean(p.title) && clean(p.text))
.map((p) => ({ title: p.title!.trim(), text: clean(p.text), tbc: false }))
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-->`
}