- บทความจากหลังบ้าน: /for-film-lovers, /for-cinemas (ArticlePage + ArticleEditor แบบบล็อกย่อหน้า/รูป) - แชร์: ปุ่มบนการ์ดและท้ายหน้ารายละเอียด · ลิงก์ย่อ /c/<slug> (302) · ธีมแก้วตาม DESIGN.md - PWA: manifest/sw/offline + ถอดการล็อกซูม (WCAG 1.4.4) + touch target ≥44px - แอนิเมชัน reveal: IntersectionObserver + gate ด้วย html.fx-motion · เคารพ prefers-reduced-motion - ค้นหา: normalize/ตัดวรรณยุกต์ + AND หลายคำ + จัดอันดับ · รองรับ /?q= จาก SearchAction - SEO: pageMeta (canonical/OG/Twitter) + JSON-LD (WebSite/Organization/BreadcrumbList/MovieTheater/Article) - ไม่ฝังโดเมนในโค้ด: origin มาจาก request (x-forwarded-host/proto) ทั้ง canonical, sitemap, robots, ลิงก์แชร์ - กัน AI: robots.txt (Nitro route, บล็อก AI 21 ตัว) + ai.txt + OG image 1200x630 - เอกสาร: README สรุปโปรเจกต์, DESIGN.md (motion/PWA/SEO/ข้อมูล), architecture.html, admin-guide - tests: 135 ผ่าน (16 ไฟล์)
305 lines
11 KiB
TypeScript
305 lines
11 KiB
TypeScript
import { useState } from "react";
|
|
|
|
import {
|
|
inputClass,
|
|
labelClass,
|
|
primaryButtonClass,
|
|
secondaryButtonClass,
|
|
} from "@/components/admin/form-styles";
|
|
import type { AboutContent, AboutSection } from "@/lib/content-types";
|
|
import type { Locale } from "@/lib/i18n/messages";
|
|
|
|
const LOCALE_TABS: Array<{ code: Locale; label: string }> = [
|
|
{ code: "th", label: "ไทย" },
|
|
{ code: "en", label: "English" },
|
|
];
|
|
|
|
type AboutEditorProps = {
|
|
value: Record<Locale, AboutContent>;
|
|
defaults: Record<Locale, AboutContent>;
|
|
onChange: (next: Record<Locale, AboutContent>) => void;
|
|
onSave: () => void;
|
|
saving: boolean;
|
|
};
|
|
|
|
/**
|
|
* ตัวแก้ไขหน้า "เกี่ยวกับเรา" — แยกแท็บตามภาษา มีตัวอย่างหน้าเว็บให้ดูสด ๆ
|
|
* ฟิลด์ที่เว้นว่างจะถูกเซิร์ฟเวอร์คืนเป็นข้อความตั้งต้นให้อัตโนมัติ (sanitizeAbout)
|
|
*/
|
|
export function AboutEditor({
|
|
value,
|
|
defaults,
|
|
onChange,
|
|
onSave,
|
|
saving,
|
|
}: AboutEditorProps) {
|
|
const [locale, setLocale] = useState<Locale>("th");
|
|
const current = value[locale];
|
|
|
|
function patch(next: Partial<AboutContent>) {
|
|
onChange({ ...value, [locale]: { ...current, ...next } });
|
|
}
|
|
|
|
function patchSection(index: number, next: Partial<AboutSection>) {
|
|
patch({
|
|
sections: current.sections.map((section, i) =>
|
|
i === index ? { ...section, ...next } : section,
|
|
),
|
|
});
|
|
}
|
|
|
|
function moveSection(index: number, direction: -1 | 1) {
|
|
const target = index + direction;
|
|
if (target < 0 || target >= current.sections.length) return;
|
|
const sections = [...current.sections];
|
|
const [moved] = sections.splice(index, 1);
|
|
sections.splice(target, 0, moved);
|
|
patch({ sections });
|
|
}
|
|
|
|
function addSection() {
|
|
patch({
|
|
sections: [
|
|
...current.sections,
|
|
{ heading: `หัวข้อที่ ${current.sections.length + 1}`, body: "" },
|
|
],
|
|
});
|
|
}
|
|
|
|
function removeSection(index: number) {
|
|
patch({ sections: current.sections.filter((_, i) => i !== index) });
|
|
}
|
|
|
|
function restoreDefaults() {
|
|
onChange({ ...value, [locale]: defaults[locale] });
|
|
}
|
|
|
|
const titleMissing = current.title.trim() === "";
|
|
const leadTooLong = current.lead.length > 400;
|
|
|
|
return (
|
|
<div className="px-6 py-5">
|
|
<div className="mb-4 flex flex-wrap items-center justify-between gap-3">
|
|
<div
|
|
role="tablist"
|
|
aria-label="ภาษา"
|
|
className="flex items-center gap-1 rounded-lg border border-[#242424] bg-[#141414] p-1"
|
|
>
|
|
{LOCALE_TABS.map((tab) => (
|
|
<button
|
|
key={tab.code}
|
|
type="button"
|
|
role="tab"
|
|
aria-selected={locale === tab.code}
|
|
onClick={() => setLocale(tab.code)}
|
|
className={`min-h-9 rounded-md px-4 text-sm transition ${
|
|
locale === tab.code
|
|
? "bg-[#242424] font-semibold text-[#D47272]"
|
|
: "text-[#999] hover:text-white"
|
|
}`}
|
|
>
|
|
{tab.label}
|
|
</button>
|
|
))}
|
|
</div>
|
|
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
<button
|
|
type="button"
|
|
className={secondaryButtonClass}
|
|
onClick={restoreDefaults}
|
|
>
|
|
คืนค่าตั้งต้น ({locale.toUpperCase()})
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className={primaryButtonClass}
|
|
onClick={onSave}
|
|
disabled={saving}
|
|
>
|
|
{saving ? "กำลังบันทึก…" : "บันทึกหน้าเกี่ยวกับเรา"}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid gap-6 lg:grid-cols-[minmax(0,1fr)_360px]">
|
|
<div className="flex flex-col gap-4">
|
|
<label className={labelClass}>
|
|
หัวข้อหลัก (H1)
|
|
<input
|
|
className={inputClass}
|
|
value={current.title}
|
|
onChange={(event) => patch({ title: event.target.value })}
|
|
aria-describedby="about-title-help"
|
|
aria-invalid={titleMissing}
|
|
/>
|
|
<span
|
|
id="about-title-help"
|
|
className={titleMissing ? "text-[#D47272]" : "text-[#8A8A8A]"}
|
|
>
|
|
{titleMissing
|
|
? "ต้องมีหัวข้อ — ถ้าเว้นว่าง ระบบจะใช้ข้อความตั้งต้น"
|
|
: `แสดงบนสุดของหน้า · ${current.title.length}/80 ตัวอักษร`}
|
|
</span>
|
|
</label>
|
|
|
|
<label className={labelClass}>
|
|
ย่อหน้าแนะนำ (ใต้หัวข้อ)
|
|
<textarea
|
|
className={`${inputClass} min-h-24 leading-[1.7]`}
|
|
value={current.lead}
|
|
onChange={(event) => patch({ lead: event.target.value })}
|
|
aria-describedby="about-lead-help"
|
|
aria-invalid={leadTooLong}
|
|
/>
|
|
<span
|
|
id="about-lead-help"
|
|
className={leadTooLong ? "text-[#D47272]" : "text-[#8A8A8A]"}
|
|
>
|
|
{leadTooLong
|
|
? "ยาวเกิน 400 ตัวอักษร — บนมือถือจะอ่านยาก"
|
|
: `${current.lead.length}/400 ตัวอักษร`}
|
|
</span>
|
|
</label>
|
|
|
|
<fieldset className="flex flex-col gap-3">
|
|
<legend className="mb-1 text-xs text-[#999]">
|
|
ส่วนย่อย ({current.sections.length} ส่วน)
|
|
</legend>
|
|
|
|
{current.sections.map((section, index) => (
|
|
<div
|
|
key={index}
|
|
className="rounded-xl border border-[#242424] bg-[#141414] p-4"
|
|
>
|
|
<div className="mb-2 flex items-center justify-between gap-2">
|
|
<span className="text-xs text-[#8A8A8A]">
|
|
ส่วนที่ {index + 1}
|
|
</span>
|
|
<div className="flex items-center gap-1">
|
|
<button
|
|
type="button"
|
|
className="size-8 rounded-md text-[#999] transition hover:bg-[#242424] hover:text-white disabled:opacity-40"
|
|
onClick={() => moveSection(index, -1)}
|
|
disabled={index === 0}
|
|
aria-label={`เลื่อนส่วนที่ ${index + 1} ขึ้น`}
|
|
>
|
|
↑
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className="size-8 rounded-md text-[#999] transition hover:bg-[#242424] hover:text-white disabled:opacity-40"
|
|
onClick={() => moveSection(index, 1)}
|
|
disabled={index === current.sections.length - 1}
|
|
aria-label={`เลื่อนส่วนที่ ${index + 1} ลง`}
|
|
>
|
|
↓
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className="min-h-8 rounded-md px-2 text-xs text-[#D47272] transition hover:bg-[#242424]"
|
|
onClick={() => removeSection(index)}
|
|
>
|
|
ลบ
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<label className={labelClass}>
|
|
หัวข้อย่อย
|
|
<input
|
|
className={inputClass}
|
|
value={section.heading}
|
|
onChange={(event) =>
|
|
patchSection(index, { heading: event.target.value })
|
|
}
|
|
/>
|
|
</label>
|
|
<label className={`${labelClass} mt-2`}>
|
|
เนื้อหา
|
|
<textarea
|
|
className={`${inputClass} min-h-24 leading-[1.75]`}
|
|
value={section.body}
|
|
onChange={(event) =>
|
|
patchSection(index, { body: event.target.value })
|
|
}
|
|
/>
|
|
</label>
|
|
</div>
|
|
))}
|
|
|
|
<button
|
|
type="button"
|
|
className={`${secondaryButtonClass} self-start`}
|
|
onClick={addSection}
|
|
>
|
|
+ เพิ่มส่วน
|
|
</button>
|
|
</fieldset>
|
|
|
|
<div className="rounded-xl border border-[#A92626]/40 bg-[#141414] p-4">
|
|
<p className="mb-3 text-xs text-[#999]">กล่องติดต่อ (ท้ายหน้า)</p>
|
|
<label className={labelClass}>
|
|
หัวข้อ
|
|
<input
|
|
className={inputClass}
|
|
value={current.contactHeading}
|
|
onChange={(event) =>
|
|
patch({ contactHeading: event.target.value })
|
|
}
|
|
/>
|
|
</label>
|
|
<label className={`${labelClass} mt-2`}>
|
|
ข้อความ
|
|
<textarea
|
|
className={`${inputClass} min-h-20 leading-[1.75]`}
|
|
value={current.contactNote}
|
|
onChange={(event) => patch({ contactNote: event.target.value })}
|
|
/>
|
|
</label>
|
|
</div>
|
|
</div>
|
|
|
|
<aside className="lg:sticky lg:top-24 lg:self-start">
|
|
<p className="mb-2 text-xs text-[#8A8A8A]">ตัวอย่างหน้าเว็บ (สด)</p>
|
|
<div className="rounded-xl border border-[#242424] bg-[#0D0D0D] p-4">
|
|
<h3 className="text-xl font-bold leading-[1.3]">
|
|
{current.title || "—"}
|
|
</h3>
|
|
<p className="mt-2 text-sm leading-[1.7] text-[#B3B3B3]">
|
|
{current.lead || "—"}
|
|
</p>
|
|
<div className="mt-3 flex flex-col gap-2">
|
|
{current.sections.map((section, index) => (
|
|
<div
|
|
key={index}
|
|
className="rounded-lg border border-[#242424] bg-[#141414] p-3"
|
|
>
|
|
<p className="text-sm font-semibold">{section.heading}</p>
|
|
<p className="mt-1 text-xs leading-[1.7] text-[#B3B3B3]">
|
|
{section.body}
|
|
</p>
|
|
</div>
|
|
))}
|
|
</div>
|
|
<div className="mt-3 rounded-lg border border-[#A92626]/50 bg-gradient-to-b from-[#8F0808]/20 to-transparent p-3">
|
|
<p className="text-sm font-semibold">{current.contactHeading}</p>
|
|
<p className="mt-1 text-xs leading-[1.7] text-[#B3B3B3]">
|
|
{current.contactNote}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<a
|
|
href={`/about?lang=${locale}`}
|
|
target="_blank"
|
|
rel="noreferrer"
|
|
className="mt-3 inline-flex min-h-11 items-center text-xs text-[#D47272] underline-offset-4 hover:underline"
|
|
>
|
|
เปิดหน้า /about?lang={locale} ↗
|
|
</a>
|
|
</aside>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|