Files
web-alterflixhub/app/lib/content-seed.ts
T
2026-11-01 17:24:44 +07:00

116 lines
3.1 KiB
TypeScript

import { CINEMAS } from "@/lib/cinemas";
import type { Locale } from "@/lib/i18n/messages";
import { MESSAGES } from "@/lib/i18n/messages";
import type {
AboutContent,
ContentStore,
FooterItem,
LocaleEntry,
TermEntry,
} from "@/lib/content-types";
import type { Messages } from "@/lib/i18n/messages";
export const DEFAULT_LOCALES: LocaleEntry[] = [
{ code: "th", label: "ไทย" },
{ code: "en", label: "English" },
];
function flatten(
value: unknown,
prefix: string,
out: Record<string, string>,
): void {
if (typeof value === "string") {
out[prefix] = value;
return;
}
if (value && typeof value === "object") {
for (const [key, child] of Object.entries(value as Record<string, unknown>)) {
flatten(child, prefix ? `${prefix}.${key}` : key, out);
}
}
}
/** รวมข้อความทุกภาษาเป็นรายการคำศัพท์ (ใช้เป็นค่าเริ่มต้นของหน้า admin) */
export function seedTerms(): TermEntry[] {
const perLocale: Record<string, Record<string, string>> = {};
for (const locale of Object.keys(MESSAGES) as Locale[]) {
const flat: Record<string, string> = {};
flatten(MESSAGES[locale], "", flat);
perLocale[locale] = flat;
}
const keys = Object.keys(perLocale.th ?? perLocale.en ?? {});
return keys.map((key) => ({
key,
values: Object.fromEntries(
Object.entries(perLocale).map(([locale, flat]) => [locale, flat[key] ?? ""]),
),
}));
}
export const DEFAULT_FOOTER: FooterItem[] = [
{
id: "facebook",
icon_name: "IconBrandFacebook",
label: "Facebook",
href: "https://www.facebook.com/",
},
{
id: "instagram",
icon_name: "IconBrandInstagram",
label: "Instagram",
href: "https://www.instagram.com/",
},
{
id: "linkedin",
icon_name: "IconBrandLinkedin",
label: "LinkedIn",
href: "https://www.linkedin.com/",
},
{
id: "youtube",
icon_name: "IconBrandYoutube",
label: "YouTube",
href: "https://www.youtube.com/",
},
{
id: "telegram",
icon_name: "IconBrandTelegram",
label: "Telegram",
href: "https://t.me/",
},
];
/** เนื้อหาหน้า "เกี่ยวกับเรา" เริ่มต้น — ดึงจากข้อความ i18n เพื่อไม่ต้องดูแลสองที่ */
export function seedAbout(): Record<Locale, AboutContent> {
return {
th: toAboutContent(MESSAGES.th.about),
en: toAboutContent(MESSAGES.en.about),
};
}
function toAboutContent(source: Messages["about"]): AboutContent {
return {
title: source.title,
lead: source.lead,
sections: source.sections.map((section) => ({
heading: section.heading,
body: section.body,
})),
contactHeading: source.contactHeading,
contactNote: source.contactNote,
};
}
export function seedContent(): ContentStore {
return {
cinemas: CINEMAS,
site: {
locales: DEFAULT_LOCALES,
terms: seedTerms(),
footer: DEFAULT_FOOTER,
},
about: seedAbout(),
};
}