reboot alterflixhub
This commit is contained in:
commit
1e84023a44
188 files changed
+31629
No files matched your search
@@ -0,0 +1,300 @@
|
||||
import { useState } from "react";
|
||||
|
||||
import type { AboutContent, AboutSection } from "@/lib/content-types";
|
||||
import type { Locale } from "@/lib/i18n/messages";
|
||||
|
||||
const inputClass =
|
||||
"w-full rounded-lg border border-[#303030] bg-[#141414] px-3 py-2 text-sm text-white outline-none transition focus:border-[#A92626] focus-visible:ring-2 focus-visible:ring-[#A92626]/40";
|
||||
const labelClass = "flex flex-col gap-1.5 text-xs text-[#999]";
|
||||
const buttonClass =
|
||||
"min-h-11 rounded-lg border border-[#303030] px-3 text-sm transition hover:border-[#A92626] hover:text-[#D47272] focus-visible:outline focus-visible:outline-2 focus-visible:outline-[#A92626]";
|
||||
const primaryButtonClass =
|
||||
"min-h-11 rounded-lg bg-[#8F0808] px-4 text-sm font-medium transition hover:bg-[#a30a0a] focus-visible:outline focus-visible:outline-2 focus-visible:outline-[#D47272]";
|
||||
|
||||
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={buttonClass} 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={`${buttonClass} 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>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import { IconChevronRight } from "@tabler/icons-react";
|
||||
import type { ReactNode } from "react";
|
||||
|
||||
/**
|
||||
* หมวดการ์ดแบบพับได้ของหลังบ้าน (ใช้ <details> เนทีฟ — คีย์บอร์ดและ screen reader ทำงานได้เอง)
|
||||
* ค่าเริ่มต้น = พับเก็บ เพื่อไม่ให้หน้าแอดมินยืดยาว
|
||||
*/
|
||||
type Props = {
|
||||
title: string;
|
||||
description?: string;
|
||||
/** ข้อความสั้น ๆ เช่นจำนวนรายการ */
|
||||
badge?: string;
|
||||
defaultOpen?: boolean;
|
||||
children: ReactNode;
|
||||
};
|
||||
|
||||
export function AdminSection({
|
||||
title,
|
||||
description,
|
||||
badge,
|
||||
defaultOpen = false,
|
||||
children,
|
||||
}: Props) {
|
||||
return (
|
||||
<details
|
||||
open={defaultOpen}
|
||||
className="group overflow-hidden rounded-xl border border-[#242424] bg-[#111111]"
|
||||
>
|
||||
<summary className="flex cursor-pointer list-none items-center justify-between gap-3 px-4 py-3 transition hover:bg-[#171717] focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-[-2px] focus-visible:outline-[#A92626] [&::-webkit-details-marker]:hidden">
|
||||
<span className="flex min-w-0 items-center gap-2">
|
||||
<IconChevronRight
|
||||
size={16}
|
||||
className="shrink-0 text-[#8F8F8F] transition-transform group-open:rotate-90"
|
||||
/>
|
||||
<span className="truncate text-sm font-medium">{title}</span>
|
||||
{badge ? (
|
||||
<span className="shrink-0 rounded-full bg-[#242424] px-2 py-0.5 text-[11px] text-[#B3B3B3]">
|
||||
{badge}
|
||||
</span>
|
||||
) : null}
|
||||
</span>
|
||||
{description ? (
|
||||
<span className="hidden shrink-0 text-[11px] text-[#777] sm:block">
|
||||
{description}
|
||||
</span>
|
||||
) : null}
|
||||
</summary>
|
||||
<div className="border-t border-[#242424] px-4 py-4">{children}</div>
|
||||
</details>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
type ChipOption = {
|
||||
value: string;
|
||||
label: string;
|
||||
};
|
||||
|
||||
type ChipPickerProps = {
|
||||
options: ChipOption[];
|
||||
selected: string[];
|
||||
onToggle: (value: string) => void;
|
||||
/** ข้อความเมื่อยังไม่ได้เลือกอะไร */
|
||||
emptyHint?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* ปุ่มเลือกหลายค่าแบบชิป สำหรับหลังบ้าน (หมวดหมู่ / สิ่งอำนวยความสะดวก)
|
||||
* ค่าที่เลือกจะถูกเก็บตามลำดับ options เสมอ เพื่อให้ข้อมูลในไฟล์นิ่ง
|
||||
*/
|
||||
export function ChipPicker({
|
||||
options,
|
||||
selected,
|
||||
onToggle,
|
||||
emptyHint,
|
||||
}: ChipPickerProps) {
|
||||
const chosen = new Set(selected);
|
||||
const known = new Set(options.map((option) => option.value));
|
||||
const extras = selected.filter((value) => !known.has(value));
|
||||
|
||||
return (
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{options.map((option) => {
|
||||
const active = chosen.has(option.value);
|
||||
return (
|
||||
<button
|
||||
key={option.value}
|
||||
type="button"
|
||||
aria-pressed={active}
|
||||
onClick={() => onToggle(option.value)}
|
||||
className={`rounded-full border px-3 py-1 text-xs transition ${
|
||||
active
|
||||
? "border-[#A92626] bg-[rgba(143,8,8,0.18)] text-[#D47272]"
|
||||
: "border-[#303030] text-[#999] hover:border-[#A92626] hover:text-white"
|
||||
}`}
|
||||
>
|
||||
{option.label}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
{extras.map((value) => (
|
||||
<button
|
||||
key={value}
|
||||
type="button"
|
||||
aria-pressed
|
||||
onClick={() => onToggle(value)}
|
||||
className="rounded-full border border-[#A92626] bg-[rgba(143,8,8,0.18)] px-3 py-1 text-xs text-[#D47272] transition hover:border-[#D47272]"
|
||||
>
|
||||
{value}
|
||||
</button>
|
||||
))}
|
||||
{options.length === 0 && extras.length === 0 && emptyHint ? (
|
||||
<span className="text-xs text-[#777]">{emptyHint}</span>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* สลับค่าหนึ่งตัวในลิสต์ แล้วเรียงตามลำดับ options (ค่าที่ไม่รู้จักต่อท้าย)
|
||||
*/
|
||||
export function toggleValue(
|
||||
selected: string[],
|
||||
value: string,
|
||||
order: string[],
|
||||
): string[] {
|
||||
const next = selected.includes(value)
|
||||
? selected.filter((item) => item !== value)
|
||||
: [...selected, value];
|
||||
const rank = (item: string) => {
|
||||
const index = order.indexOf(item);
|
||||
return index < 0 ? order.length + 1 : index;
|
||||
};
|
||||
return [...next].sort((a, b) => rank(a) - rank(b));
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
import type { Cinema } from "@/lib/cinemas";
|
||||
import { DEFAULT_LOCALE, MESSAGES, type FacilityKey } from "@/lib/i18n/messages";
|
||||
|
||||
/**
|
||||
* แก้ข้อความสิ่งอำนวยความสะดวก "ของโรงนี้" (ผูกกับโรง ไม่ใช่ส่วนกลาง)
|
||||
*
|
||||
* - ชื่อหัวข้อ: ช่องเดียว (ใช้ทั้ง TH/EN) — ว่าง = ใช้ค่าเริ่มต้นจาก messages.ts
|
||||
* - คำอธิบาย TH/EN: แสดงเฉพาะโรงที่ต้องการ (hasDescriptions) เช่น เฮ้าส์ สามย่าน
|
||||
* ที่มีข้อมูลครบแล้ว โรงอื่นไม่ต้องกรอก
|
||||
*/
|
||||
|
||||
export type FacilityDetail = {
|
||||
label?: string;
|
||||
sub_th?: string;
|
||||
sub_en?: string;
|
||||
};
|
||||
|
||||
const ADMIN_MESSAGES = MESSAGES[DEFAULT_LOCALE];
|
||||
|
||||
type Props = {
|
||||
facilities: FacilityKey[];
|
||||
details: NonNullable<Cinema["facility_details"]>;
|
||||
hasDescriptions: boolean;
|
||||
onChange: (next: NonNullable<Cinema["facility_details"]>) => void;
|
||||
inputClass: string;
|
||||
labelClass: string;
|
||||
};
|
||||
|
||||
export function CinemaFacilitiesEditor({
|
||||
facilities,
|
||||
details,
|
||||
hasDescriptions,
|
||||
onChange,
|
||||
inputClass,
|
||||
labelClass,
|
||||
}: Props) {
|
||||
if (facilities.length === 0) {
|
||||
return (
|
||||
<span className="text-[11px] text-[#777]">
|
||||
เลือกหัวข้อด้านบนก่อน แล้วค่อยใส่ชื่อที่ต้องการ
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
function update(key: FacilityKey, patch: FacilityDetail) {
|
||||
const current = details[key] ?? {};
|
||||
const next: FacilityDetail = { ...current, ...patch };
|
||||
const cleaned: FacilityDetail = {};
|
||||
if (next.label?.trim()) cleaned.label = next.label;
|
||||
if (next.sub_th?.trim()) cleaned.sub_th = next.sub_th;
|
||||
if (next.sub_en?.trim()) cleaned.sub_en = next.sub_en;
|
||||
onChange({ ...details, [key]: cleaned });
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-2">
|
||||
{facilities.map((key) => {
|
||||
const fallback = ADMIN_MESSAGES.facilities[key];
|
||||
const detail = details[key] ?? {};
|
||||
return (
|
||||
<div
|
||||
key={key}
|
||||
className="grid gap-2 rounded-lg border border-[#242424] p-2 sm:grid-cols-2"
|
||||
>
|
||||
<label className={labelClass}>
|
||||
ชื่อหัวข้อ
|
||||
<input
|
||||
className={inputClass}
|
||||
value={detail.label ?? ""}
|
||||
placeholder={fallback.title}
|
||||
onChange={(event) => update(key, { label: event.target.value })}
|
||||
/>
|
||||
</label>
|
||||
{hasDescriptions ? (
|
||||
<>
|
||||
<label className={labelClass}>
|
||||
คำอธิบาย (TH)
|
||||
<input
|
||||
className={inputClass}
|
||||
value={detail.sub_th ?? ""}
|
||||
placeholder={fallback.sub}
|
||||
onChange={(event) =>
|
||||
update(key, { sub_th: event.target.value })
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<label className={labelClass}>
|
||||
คำอธิบาย (EN)
|
||||
<input
|
||||
className={inputClass}
|
||||
value={detail.sub_en ?? ""}
|
||||
placeholder={MESSAGES.en.facilities[key].sub}
|
||||
onChange={(event) =>
|
||||
update(key, { sub_en: event.target.value })
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
</>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
import {
|
||||
IconArrowDown,
|
||||
IconArrowUp,
|
||||
IconGripVertical,
|
||||
IconSortAscendingLetters,
|
||||
} from "@tabler/icons-react";
|
||||
import { useMemo, useState } from "react";
|
||||
|
||||
import type { Cinema } from "@/lib/cinemas";
|
||||
|
||||
/**
|
||||
* รายชื่อโรงหนังในหลังบ้าน — เรียง ก-ฮ และสลับตำแหน่งได้
|
||||
*
|
||||
* - โหมด "ลำดับที่จัดไว้": ลากวางสลับที่ หรือกด ↑ ↓ (ใช้เมาส์/คีย์บอร์ดได้ทั้งคู่)
|
||||
* ลำดับที่ได้คือลำดับเดียวกับที่หน้าเว็บแสดง (อาร์เรย์ cinemas ใน content.json)
|
||||
* - โหมด "ก-ฮ": เรียงตามชื่อไทยด้วย Intl.Collator("th") — ยังไม่บันทึกจนกว่าจะกด "ใช้ลำดับนี้"
|
||||
* (กันเข้าใจผิดว่าลากในโหมดเรียงได้)
|
||||
*/
|
||||
|
||||
const thaiCollator = new Intl.Collator("th", {
|
||||
sensitivity: "base",
|
||||
numeric: true,
|
||||
});
|
||||
|
||||
function sortKey(cinema: Cinema): string {
|
||||
return (cinema.name_th?.trim() || cinema.name || "").trim();
|
||||
}
|
||||
|
||||
/** เรียงชื่อโรงหนังภาษาไทยตามพจนานุกรม ก-ฮ */
|
||||
export function sortCinemasByName(cinemas: Cinema[]): Cinema[] {
|
||||
return [...cinemas].sort((a, b) => thaiCollator.compare(sortKey(a), sortKey(b)));
|
||||
}
|
||||
|
||||
/** เทียบว่าสองลิสต์มี slug เรียงเหมือนกันหรือยัง */
|
||||
export function hasSameOrder(a: Cinema[], b: Cinema[]): boolean {
|
||||
if (a.length !== b.length) return false;
|
||||
return a.every((cinema, index) => cinema.slug === b[index]?.slug);
|
||||
}
|
||||
|
||||
/** ย้ายโรงหนังจากตำแหน่งหนึ่งไปแทรกก่อน/หลังอีกตัว */
|
||||
export function moveCinema(
|
||||
cinemas: Cinema[],
|
||||
fromSlug: string,
|
||||
toSlug: string,
|
||||
position: "before" | "after" = "before",
|
||||
): Cinema[] {
|
||||
if (fromSlug === toSlug) return cinemas;
|
||||
const from = cinemas.findIndex((item) => item.slug === fromSlug);
|
||||
const target = cinemas.findIndex((item) => item.slug === toSlug);
|
||||
if (from < 0 || target < 0) return cinemas;
|
||||
|
||||
const next = [...cinemas];
|
||||
const [moved] = next.splice(from, 1);
|
||||
const insertAt = next.findIndex((item) => item.slug === toSlug);
|
||||
if (insertAt < 0) return cinemas;
|
||||
next.splice(position === "after" ? insertAt + 1 : insertAt, 0, moved);
|
||||
return next;
|
||||
}
|
||||
|
||||
/** เลื่อนขึ้น/ลงทีละหนึ่งตำแหน่ง (สำหรับปุ่มลูกศร) */
|
||||
export function shiftCinema(cinemas: Cinema[], slug: string, delta: number): Cinema[] {
|
||||
const from = cinemas.findIndex((item) => item.slug === slug);
|
||||
const to = from + delta;
|
||||
if (from < 0 || to < 0 || to >= cinemas.length) return cinemas;
|
||||
const next = [...cinemas];
|
||||
const [moved] = next.splice(from, 1);
|
||||
next.splice(to, 0, moved);
|
||||
return next;
|
||||
}
|
||||
|
||||
type SortMode = "manual" | "thai";
|
||||
|
||||
type CinemaListProps = {
|
||||
cinemas: Cinema[];
|
||||
selected: string | null;
|
||||
/** ลำดับในหน้าเว็บยังไม่ตรงกับที่บันทึกไว้หรือไม่ */
|
||||
dirty: boolean;
|
||||
saving: boolean;
|
||||
onSelect: (slug: string) => void;
|
||||
onReorder: (next: Cinema[]) => void;
|
||||
onSaveOrder: () => void;
|
||||
};
|
||||
|
||||
const modeButtonClass = (active: boolean) =>
|
||||
`flex-1 rounded-md px-2 py-1 text-[11px] transition ${
|
||||
active ? "bg-[#242424] text-[#D47272]" : "text-[#999] hover:text-white"
|
||||
}`;
|
||||
|
||||
const arrowButtonClass =
|
||||
"flex size-6 items-center justify-center rounded text-[#777] transition hover:bg-[#242424] hover:text-white disabled:pointer-events-none disabled:opacity-30";
|
||||
|
||||
export function CinemaList({
|
||||
cinemas,
|
||||
selected,
|
||||
dirty,
|
||||
saving,
|
||||
onSelect,
|
||||
onReorder,
|
||||
onSaveOrder,
|
||||
}: CinemaListProps) {
|
||||
const [mode, setMode] = useState<SortMode>("manual");
|
||||
const [dragSlug, setDragSlug] = useState<string | null>(null);
|
||||
const [overSlug, setOverSlug] = useState<string | null>(null);
|
||||
|
||||
const visible = useMemo(
|
||||
() => (mode === "thai" ? sortCinemasByName(cinemas) : cinemas),
|
||||
[cinemas, mode],
|
||||
);
|
||||
|
||||
function handleDrop(event: React.DragEvent<HTMLLIElement>, toSlug: string) {
|
||||
event.preventDefault();
|
||||
const fromSlug = dragSlug ?? event.dataTransfer.getData("text/plain");
|
||||
setOverSlug(null);
|
||||
setDragSlug(null);
|
||||
if (!fromSlug || fromSlug === toSlug) return;
|
||||
onReorder(moveCinema(cinemas, fromSlug, toSlug));
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mt-2 flex flex-col gap-2">
|
||||
<div className="flex items-center gap-1 rounded-lg border border-[#303030] p-1">
|
||||
<button
|
||||
type="button"
|
||||
className={modeButtonClass(mode === "manual")}
|
||||
aria-pressed={mode === "manual"}
|
||||
onClick={() => setMode("manual")}
|
||||
>
|
||||
ลำดับที่จัดไว้
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className={modeButtonClass(mode === "thai")}
|
||||
aria-pressed={mode === "thai"}
|
||||
onClick={() => setMode("thai")}
|
||||
>
|
||||
<span className="inline-flex items-center justify-center gap-1">
|
||||
<IconSortAscendingLetters size={13} /> ก-ฮ
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{mode === "thai" ? (
|
||||
<div className="rounded-lg border border-[#303030] bg-[#141414] p-2 text-[11px] leading-[1.7] text-[#999]">
|
||||
เรียงตามชื่อไทย ก-ฮ · โหมดนี้ลากสลับไม่ได้
|
||||
<button
|
||||
type="button"
|
||||
className="mt-1 block w-full rounded-md border border-[#303030] px-2 py-1 text-[11px] text-[#D47272] transition hover:border-[#A92626]"
|
||||
onClick={() => {
|
||||
onReorder(sortCinemasByName(cinemas));
|
||||
setMode("manual");
|
||||
}}
|
||||
>
|
||||
ใช้ลำดับ ก-ฮ นี้ (แล้วกดบันทึก)
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<p className="px-1 text-[11px] leading-[1.6] text-[#777]">
|
||||
ลากชื่อเพื่อสลับตำแหน่ง หรือกด ↑ ↓ ทีละขั้น
|
||||
</p>
|
||||
)}
|
||||
|
||||
{dirty ? (
|
||||
<button
|
||||
type="button"
|
||||
disabled={saving}
|
||||
className="rounded-lg bg-[#8F0808] px-3 py-1.5 text-[11px] font-medium transition hover:bg-[#a30a0a] disabled:opacity-60"
|
||||
onClick={onSaveOrder}
|
||||
>
|
||||
{saving ? "กำลังบันทึก…" : "บันทึกลำดับใหม่"}
|
||||
</button>
|
||||
) : null}
|
||||
|
||||
<ul className="flex max-h-[62vh] flex-col gap-1 overflow-y-auto pr-1">
|
||||
{visible.map((item, index) => {
|
||||
const isSelected = item.slug === selected;
|
||||
const isDragging = dragSlug === item.slug;
|
||||
const isOver = overSlug === item.slug && dragSlug !== item.slug;
|
||||
return (
|
||||
<li
|
||||
key={item.slug}
|
||||
draggable={mode === "manual"}
|
||||
onDragStart={(event) => {
|
||||
setDragSlug(item.slug);
|
||||
event.dataTransfer.effectAllowed = "move";
|
||||
event.dataTransfer.setData("text/plain", item.slug);
|
||||
}}
|
||||
onDragOver={(event) => {
|
||||
if (mode !== "manual") return;
|
||||
event.preventDefault();
|
||||
event.dataTransfer.dropEffect = "move";
|
||||
setOverSlug(item.slug);
|
||||
}}
|
||||
onDragLeave={() => {
|
||||
setOverSlug((current) => (current === item.slug ? null : current));
|
||||
}}
|
||||
onDrop={(event) => handleDrop(event, item.slug)}
|
||||
onDragEnd={() => {
|
||||
setDragSlug(null);
|
||||
setOverSlug(null);
|
||||
}}
|
||||
className={`group flex items-center gap-1 rounded-lg pr-1 transition ${
|
||||
isSelected ? "bg-[#242424]" : "hover:bg-[#1B1B1B]"
|
||||
} ${isDragging ? "opacity-40" : ""} ${
|
||||
isOver ? "outline outline-2 outline-offset-[-2px] outline-[#A92626]" : ""
|
||||
} ${mode === "manual" ? "cursor-grab active:cursor-grabbing" : ""}`}
|
||||
>
|
||||
<span
|
||||
aria-hidden="true"
|
||||
className={`pl-1 ${
|
||||
mode === "manual" ? "text-[#555] group-hover:text-[#999]" : "text-transparent"
|
||||
}`}
|
||||
>
|
||||
<IconGripVertical size={14} />
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => onSelect(item.slug)}
|
||||
className={`min-w-0 flex-1 py-2 text-left text-sm transition ${
|
||||
isSelected ? "text-[#D47272]" : "text-[#B3B3B3] hover:text-white"
|
||||
}`}
|
||||
>
|
||||
<span className="block truncate">{item.name_th || item.name}</span>
|
||||
<span className="block truncate text-[10px] text-[#777]">{item.slug}</span>
|
||||
</button>
|
||||
{mode === "manual" ? (
|
||||
<span className="flex flex-col">
|
||||
<button
|
||||
type="button"
|
||||
className={arrowButtonClass}
|
||||
aria-label={`เลื่อน ${item.name_th || item.name} ขึ้น`}
|
||||
disabled={index === 0}
|
||||
onClick={() => onReorder(shiftCinema(cinemas, item.slug, -1))}
|
||||
>
|
||||
<IconArrowUp size={13} />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className={arrowButtonClass}
|
||||
aria-label={`เลื่อน ${item.name_th || item.name} ลง`}
|
||||
disabled={index === visible.length - 1}
|
||||
onClick={() => onReorder(shiftCinema(cinemas, item.slug, 1))}
|
||||
>
|
||||
<IconArrowDown size={13} />
|
||||
</button>
|
||||
</span>
|
||||
) : null}
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
/**
|
||||
* ช่องกรอกหลายค่า คั่นด้วย "," (เช่น หมวด, ช่องทางชำระเงิน)
|
||||
*
|
||||
* ทำไมต้องมี draft: ถ้าผูก value เข้ากับ array ตรง ๆ แล้ว split/join ทุกคีย์
|
||||
* ตัว "," ที่เพิ่งพิมพ์จะถูกตัดทิ้งทันที ( join(", ") ไม่มีตัวคั่นให้ค่าว่าง )
|
||||
* ทำให้พิมพ์คั่นค่าไม่ได้เลย — draft เก็บข้อความดิบระหว่างพิมพ์ แล้วค่อย
|
||||
* normalize เป็น ", " เมื่อออกจากช่อง
|
||||
*/
|
||||
|
||||
/** แปลงข้อความ → รายการ (รับ , , 、 และขึ้นบรรทัดใหม่เป็นตัวคั่นด้วย) */
|
||||
export function parseCommaList(value: string): string[] {
|
||||
return value
|
||||
.replace(/[,、]/g, ",")
|
||||
.split(/[,\n]/)
|
||||
.map((item) => item.trim())
|
||||
.filter((item) => item !== "");
|
||||
}
|
||||
|
||||
export function formatList(value: string[]): string {
|
||||
return value.join(", ");
|
||||
}
|
||||
|
||||
type CommaListInputProps = {
|
||||
value: string[];
|
||||
onChangeValue: (next: string[]) => void;
|
||||
className?: string;
|
||||
placeholder?: string;
|
||||
id?: string;
|
||||
};
|
||||
|
||||
export function CommaListInput({
|
||||
value,
|
||||
onChangeValue,
|
||||
className,
|
||||
placeholder,
|
||||
id,
|
||||
}: CommaListInputProps) {
|
||||
const canonical = formatList(value);
|
||||
const [draft, setDraft] = useState<string | null>(null);
|
||||
|
||||
// ค่าถูกเปลี่ยนจากที่อื่น (เช่น เปลี่ยนโรง / ใช้ JSON / คืนค่า) → ทิ้ง draft
|
||||
useEffect(() => {
|
||||
if (draft === null) return;
|
||||
if (formatList(parseCommaList(draft)) !== canonical) setDraft(null);
|
||||
}, [canonical, draft]);
|
||||
|
||||
return (
|
||||
<input
|
||||
id={id}
|
||||
className={className}
|
||||
placeholder={placeholder}
|
||||
value={draft ?? canonical}
|
||||
spellCheck={false}
|
||||
onChange={(event) => {
|
||||
const text = event.target.value;
|
||||
setDraft(text);
|
||||
onChangeValue(parseCommaList(text));
|
||||
}}
|
||||
onBlur={() => setDraft(null)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
import { IconPlus, IconTrash } from "@tabler/icons-react";
|
||||
|
||||
import type { ContactChannel } from "@/lib/cinemas";
|
||||
|
||||
/**
|
||||
* แก้ช่องทางติดต่อของการ์ด "สอบถามข้อมูลและจองตั๋ว" ต่อโรง
|
||||
* แต่ละแถว = label / ค่าที่แสดง / ลิงก์ (ถ้าไม่มีลิงก์ หน้าเว็บแสดงเป็นข้อความธรรมดา)
|
||||
*/
|
||||
|
||||
const SUGGESTED_LABELS = [
|
||||
"Website",
|
||||
"Facebook",
|
||||
"Instagram",
|
||||
"Line",
|
||||
"Call",
|
||||
"อีเมล",
|
||||
"จองตั๋ว",
|
||||
];
|
||||
|
||||
const smallButtonClass =
|
||||
"flex min-h-9 items-center gap-1 rounded-lg border border-[#303030] px-2 text-xs text-[#999] transition hover:border-[#A92626] hover:text-[#D47272]";
|
||||
|
||||
type Props = {
|
||||
contacts: ContactChannel[];
|
||||
onChange: (next: ContactChannel[]) => void;
|
||||
inputClass: string;
|
||||
labelClass: string;
|
||||
};
|
||||
|
||||
export function ContactsEditor({ contacts, onChange, inputClass, labelClass }: Props) {
|
||||
function updateAt(index: number, patch: Partial<ContactChannel>) {
|
||||
onChange(
|
||||
contacts.map((item, i) => (i === index ? { ...item, ...patch } : item)),
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-2">
|
||||
<span className="text-xs text-[#999]">ช่องทางติดต่อ / จองตั๋ว</span>
|
||||
|
||||
{contacts.length === 0 ? (
|
||||
<span className="text-[11px] text-[#777]">
|
||||
ยังไม่มีช่องทางติดต่อ — หน้าเว็บจะขึ้น “ยังไม่ระบุช่องทางติดต่อ”
|
||||
</span>
|
||||
) : null}
|
||||
|
||||
{contacts.map((contact, index) => (
|
||||
<div
|
||||
key={index}
|
||||
className="grid gap-2 rounded-lg border border-[#242424] p-2 sm:grid-cols-[130px_minmax(0,1fr)_minmax(0,1fr)_auto]"
|
||||
>
|
||||
<label className={labelClass}>
|
||||
ป้ายชื่อ
|
||||
<input
|
||||
className={inputClass}
|
||||
value={contact.label}
|
||||
placeholder="Website"
|
||||
list="contact-label-options"
|
||||
onChange={(event) => updateAt(index, { label: event.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<label className={labelClass}>
|
||||
ข้อความที่แสดง
|
||||
<input
|
||||
className={inputClass}
|
||||
value={contact.value}
|
||||
placeholder="www.housesamyan.com"
|
||||
onChange={(event) => updateAt(index, { value: event.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<label className={labelClass}>
|
||||
ลิงก์ (ไม่บังคับ)
|
||||
<input
|
||||
className={inputClass}
|
||||
value={contact.href ?? ""}
|
||||
placeholder="https://… หรือ tel:+66…"
|
||||
onChange={(event) =>
|
||||
updateAt(index, {
|
||||
href: event.target.value.trim() === "" ? null : event.target.value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<div className="flex items-end">
|
||||
<button
|
||||
type="button"
|
||||
className={smallButtonClass}
|
||||
aria-label={`ลบช่องทางที่ ${index + 1}`}
|
||||
onClick={() => onChange(contacts.filter((_, i) => i !== index))}
|
||||
>
|
||||
<IconTrash size={14} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
|
||||
<datalist id="contact-label-options">
|
||||
{SUGGESTED_LABELS.map((label) => (
|
||||
<option key={label} value={label} />
|
||||
))}
|
||||
</datalist>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
className={`${smallButtonClass} w-fit`}
|
||||
onClick={() => onChange([...contacts, { label: "", value: "", href: null }])}
|
||||
>
|
||||
<IconPlus size={14} />
|
||||
เพิ่มช่องทางติดต่อ
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
import { IconPlus, IconTrash } from "@tabler/icons-react";
|
||||
|
||||
/**
|
||||
* แก้รายการรูปโรงหนัง (อาร์เรย์ `photos`) — ใส่เป็น URL หรือ path ในโปรเจกต์
|
||||
* รูปแรกใช้เป็นภาพหลักของแกลเลอรี · ลากสลับลำดับไม่ได้ ใช้ปุ่ม ↑/↓ แทน
|
||||
*/
|
||||
type Props = {
|
||||
photos: string[];
|
||||
onChange: (next: string[]) => void;
|
||||
inputClass: string;
|
||||
labelClass: string;
|
||||
};
|
||||
|
||||
const smallButtonClass =
|
||||
"flex min-h-9 items-center gap-1 rounded-lg border border-[#303030] px-2 text-xs text-[#999] transition hover:border-[#A92626] hover:text-[#D47272]";
|
||||
|
||||
export function PhotosEditor({ photos, onChange, inputClass, labelClass }: Props) {
|
||||
function updateAt(index: number, value: string) {
|
||||
onChange(photos.map((item, i) => (i === index ? value : item)));
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-2">
|
||||
<span className="text-xs text-[#999]">รูปภาพโรงหนัง</span>
|
||||
|
||||
{photos.length === 0 ? (
|
||||
<span className="text-[11px] text-[#777]">
|
||||
ยังไม่มีรูป — หน้าเว็บจะแสดงช่องแทน (ภาพที่ 1/2/3)
|
||||
</span>
|
||||
) : null}
|
||||
|
||||
{photos.map((photo, index) => (
|
||||
<div
|
||||
key={index}
|
||||
className="grid items-center gap-2 rounded-lg border border-[#242424] p-2 sm:grid-cols-[64px_minmax(0,1fr)_auto_auto]"
|
||||
>
|
||||
<div className="size-16 overflow-hidden rounded-md border border-[#303030] bg-[#141414]">
|
||||
{photo ? (
|
||||
// eslint-disable-next-line jsx-a11y/alt-text
|
||||
<img
|
||||
src={photo}
|
||||
alt=""
|
||||
className="size-full object-cover"
|
||||
loading="lazy"
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
<label className={labelClass}>
|
||||
{index === 0 ? "รูปหลัก (URL หรือ /images/...)" : `รูปที่ ${index + 1}`}
|
||||
<input
|
||||
className={inputClass}
|
||||
value={photo}
|
||||
placeholder="/images/hero.png หรือ https://…/photo.jpg"
|
||||
onChange={(event) => updateAt(index, event.target.value)}
|
||||
/>
|
||||
</label>
|
||||
<div className="flex gap-1">
|
||||
<button
|
||||
type="button"
|
||||
className={smallButtonClass}
|
||||
aria-label={`เลื่อนรูปที่ ${index + 1} ขึ้น`}
|
||||
disabled={index === 0}
|
||||
onClick={() => {
|
||||
const next = [...photos];
|
||||
[next[index - 1], next[index]] = [next[index], next[index - 1]];
|
||||
onChange(next);
|
||||
}}
|
||||
>
|
||||
↑
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className={smallButtonClass}
|
||||
aria-label={`เลื่อนรูปที่ ${index + 1} ลง`}
|
||||
disabled={index === photos.length - 1}
|
||||
onClick={() => {
|
||||
const next = [...photos];
|
||||
[next[index + 1], next[index]] = [next[index], next[index + 1]];
|
||||
onChange(next);
|
||||
}}
|
||||
>
|
||||
↓
|
||||
</button>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className={smallButtonClass}
|
||||
aria-label={`ลบรูปที่ ${index + 1}`}
|
||||
onClick={() => onChange(photos.filter((_, i) => i !== index))}
|
||||
>
|
||||
<IconTrash size={14} />
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
|
||||
<button
|
||||
type="button"
|
||||
className={`${smallButtonClass} w-fit`}
|
||||
onClick={() => onChange([...photos, ""])}
|
||||
>
|
||||
<IconPlus size={14} />
|
||||
เพิ่มรูป
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
|
||||
/**
|
||||
* ช่องแก้ slug (ใช้ใน URL) ของโรงหนัง
|
||||
*
|
||||
* ทำไมของเดิม "แก้ไม่ได้": อินพุตผูกกับ `cinema.slug` ตรง ๆ และแปลงค่าทุกคีย์
|
||||
* (`replace(/[^a-z0-9-]/g, "-")`) — พอพิมพ์ตัวอักษรไทย/เว้นวรรค มันกลายเป็น "-"
|
||||
* ทันที และถ้าลบจนว่าง ค่าก็ว่างทันที ทำให้ดูเหมือนพิมพ์อะไรไม่ได้เลย
|
||||
*
|
||||
* ที่นี่เก็บ "ข้อความดิบ" ระหว่างพิมพ์ (draft) แล้วค่อยจัดรูปตอนออกจากช่อง (blur)
|
||||
* slug ว่างหรือซ้ำกับโรงอื่นจะไม่ถูกบันทึก — คืนค่าเดิมให้
|
||||
*/
|
||||
|
||||
const SLUG_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
||||
|
||||
/** จัดรูป slug: ตัวเล็ก, ช่องว่าง/ขีดซ้ำ → ขีดเดียว, ตัดอักขระที่ไม่ใช่ a-z0-9- */
|
||||
export function normalizeSlug(value: string): string {
|
||||
return value
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/[\s_]+/g, "-")
|
||||
.replace(/[^a-z0-9-]/g, "")
|
||||
.replace(/-{2,}/g, "-")
|
||||
.replace(/^-+|-+$/g, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* แนะนำ slug จากชื่อโรงหนังภาษาอังกฤษ
|
||||
* "HOUSE CINEMA SAMYAN" → "house-cinema-samyan" · "." และอักขระอื่นถูกตัดให้เอง
|
||||
*/
|
||||
export function suggestSlug(name: string): string {
|
||||
return normalizeSlug(
|
||||
name
|
||||
.normalize("NFKD")
|
||||
.replace(/[\u0300-\u036f]/g, "")
|
||||
.replace(/&/g, " and ")
|
||||
.replace(/['’]/g, ""),
|
||||
);
|
||||
}
|
||||
|
||||
/** เหตุผลที่ slug ใช้ไม่ได้ (null = ใช้ได้) */
|
||||
export function slugProblem(
|
||||
slug: string,
|
||||
options: { others: string[]; self?: string },
|
||||
): string | null {
|
||||
if (slug === "") return "slug ว่างไม่ได้";
|
||||
if (!SLUG_PATTERN.test(slug)) return "ใช้ได้เฉพาะ a-z, 0-9 และ - (ห้ามขีดติดกัน/หัวท้าย)";
|
||||
if (options.others.some((other) => other === slug && other !== options.self)) {
|
||||
return "slug นี้มีโรงอื่นใช้อยู่";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
type SlugInputProps = {
|
||||
value: string;
|
||||
/** slug ของโรงอื่นทั้งหมด ใช้ตรวจซ้ำ */
|
||||
others: string[];
|
||||
onChange: (slug: string) => void;
|
||||
className?: string;
|
||||
};
|
||||
|
||||
export function SlugInput({ value, others, onChange, className }: SlugInputProps) {
|
||||
const [draft, setDraft] = useState<string | null>(null);
|
||||
const [notice, setNotice] = useState<string | null>(null);
|
||||
const lastValue = useRef(value);
|
||||
|
||||
// ค่าถูกเปลี่ยนจากภายนอก (สลับโรง / ใช้ JSON / คืนค่า) → ทิ้ง draft
|
||||
// เทียบกับค่าก่อนหน้า ไม่ใช่เทียบกับ draft เพราะระหว่างพิมพ์ value ยังไม่เปลี่ยน
|
||||
useEffect(() => {
|
||||
if (lastValue.current === value) return;
|
||||
lastValue.current = value;
|
||||
setDraft(null);
|
||||
}, [value]);
|
||||
|
||||
const shown = draft ?? value;
|
||||
const preview = normalizeSlug(shown);
|
||||
const problem = slugProblem(preview, { others, self: value });
|
||||
const dirty = preview !== value;
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-1">
|
||||
<input
|
||||
className={className}
|
||||
value={shown}
|
||||
spellCheck={false}
|
||||
placeholder="house-cinema-samyan"
|
||||
onChange={(event) => {
|
||||
setDraft(event.target.value);
|
||||
setNotice(null);
|
||||
}}
|
||||
onBlur={() => {
|
||||
const next = normalizeSlug(draft ?? value);
|
||||
if (next !== "" && slugProblem(next, { others, self: value }) === null) {
|
||||
if (next !== value) {
|
||||
onChange(next);
|
||||
setNotice("เปลี่ยน slug แล้ว — ลิงก์เดิมของโรงนี้จะใช้ไม่ได้ (ต้องกดบันทึก)");
|
||||
}
|
||||
} else if (next !== value) {
|
||||
setNotice("slug ไม่ถูกบันทึก (ว่าง/ซ้ำ/รูปแบบไม่ถูก) — คงค่าเดิมไว้");
|
||||
}
|
||||
setDraft(null);
|
||||
}}
|
||||
/>
|
||||
|
||||
<div className="flex flex-wrap items-center gap-2 text-[11px]">
|
||||
<code className="text-[#777]">/cinema/{preview || "…"}</code>
|
||||
{problem && dirty ? (
|
||||
<span className="text-[#D47272]">{problem}</span>
|
||||
) : null}
|
||||
{!problem && dirty ? (
|
||||
<span className="text-[#B3B3B3]">กด Tab/คลิกออกเพื่อยืนยัน slug ใหม่</span>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
{notice ? (
|
||||
<span className="text-[11px] text-[#D47272]">{notice}</span>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
import { IconPlus, IconTrash } from "@tabler/icons-react";
|
||||
|
||||
import type { TransportMode } from "@/lib/cinemas";
|
||||
import { DEFAULT_LOCALE, MESSAGES } from "@/lib/i18n/messages";
|
||||
|
||||
/**
|
||||
* แก้ไขข้อมูลการเดินทางของโรงหนัง (รถยนต์ส่วนตัว / รถไฟฟ้า / รถประจำทาง)
|
||||
* เรียงตามลำดับในอาร์เรย์ transport ของ content.json
|
||||
*/
|
||||
|
||||
const ADMIN_MESSAGES = MESSAGES[DEFAULT_LOCALE];
|
||||
const ICON_OPTIONS: Array<TransportMode["icon_name"]> = [
|
||||
"private_car",
|
||||
"metro",
|
||||
"bus",
|
||||
];
|
||||
|
||||
type TransportEditorProps = {
|
||||
value: TransportMode[];
|
||||
onChange: (next: TransportMode[]) => void;
|
||||
inputClass: string;
|
||||
labelClass: string;
|
||||
};
|
||||
|
||||
const smallButtonClass =
|
||||
"flex min-h-9 items-center gap-1 rounded-lg border border-[#303030] px-2 text-xs text-[#999] transition hover:border-[#A92626] hover:text-[#D47272]";
|
||||
|
||||
export function TransportEditor({
|
||||
value,
|
||||
onChange,
|
||||
inputClass,
|
||||
labelClass,
|
||||
}: TransportEditorProps) {
|
||||
function updateAt(index: number, patch: Partial<TransportMode>) {
|
||||
onChange(value.map((item, i) => (i === index ? { ...item, ...patch } : item)));
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-2">
|
||||
<span className="text-xs text-[#999]">การเดินทาง</span>
|
||||
|
||||
{value.length === 0 ? (
|
||||
<span className="text-[11px] text-[#777]">
|
||||
ยังไม่มีข้อมูล — หน้าเว็บจะซ่อนบล็อกการเดินทาง
|
||||
</span>
|
||||
) : null}
|
||||
|
||||
{value.map((route, index) => (
|
||||
<div
|
||||
key={index}
|
||||
className="grid gap-2 rounded-lg border border-[#242424] p-2 sm:grid-cols-[130px_minmax(0,1fr)_minmax(0,1fr)_auto]"
|
||||
>
|
||||
<label className={labelClass}>
|
||||
วิธีเดินทาง
|
||||
<select
|
||||
className={inputClass}
|
||||
value={route.icon_name}
|
||||
onChange={(event) =>
|
||||
updateAt(index, {
|
||||
icon_name: event.target.value as TransportMode["icon_name"],
|
||||
})
|
||||
}
|
||||
>
|
||||
{ICON_OPTIONS.map((icon) => (
|
||||
<option key={icon} value={icon}>
|
||||
{ADMIN_MESSAGES.transport[icon]}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label className={labelClass}>
|
||||
รายละเอียด (TH)
|
||||
<input
|
||||
className={inputClass}
|
||||
value={route.value}
|
||||
placeholder="MRT สถานีสามย่าน"
|
||||
onChange={(event) => updateAt(index, { value: event.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<label className={labelClass}>
|
||||
รายละเอียด (EN)
|
||||
<input
|
||||
className={inputClass}
|
||||
value={route.value_en ?? ""}
|
||||
placeholder="MRT Sam Yan station"
|
||||
onChange={(event) =>
|
||||
updateAt(index, { value_en: event.target.value })
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<div className="flex items-end">
|
||||
<button
|
||||
type="button"
|
||||
className={smallButtonClass}
|
||||
aria-label={`ลบวิธีเดินทางลำดับที่ ${index + 1}`}
|
||||
onClick={() => onChange(value.filter((_, i) => i !== index))}
|
||||
>
|
||||
<IconTrash size={14} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
|
||||
<button
|
||||
type="button"
|
||||
className={`${smallButtonClass} w-fit`}
|
||||
onClick={() =>
|
||||
onChange([...value, { icon_name: "bus", value: "", value_en: "" }])
|
||||
}
|
||||
>
|
||||
<IconPlus size={14} />
|
||||
เพิ่มวิธีเดินทาง
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { parseCommaList, formatList } from "@/components/admin/CommaListInput";
|
||||
import { normalizeSlug, slugProblem, suggestSlug } from "@/components/admin/SlugInput";
|
||||
|
||||
/**
|
||||
* สเปกของตัวช่วยในหลังบ้าน — กันบั๊กที่เคยเจอจริง
|
||||
* 1) ช่องคั่นด้วย "," ที่กลืนตัวคั่นระหว่างพิมพ์
|
||||
* 2) slug ที่พิมพ์ไทย/เว้นวรรคแล้วหาย
|
||||
*/
|
||||
|
||||
describe("parseCommaList", () => {
|
||||
it("แยกด้วย , และตัดช่องว่างหัวท้าย", () => {
|
||||
expect(parseCommaList("อาร์ตเฮาส์, สารคดี")).toEqual(["อาร์ตเฮาส์", "สารคดี"]);
|
||||
});
|
||||
|
||||
it("ทิ้งค่าว่าง และรับตัวคั่นเต็มความกว้าง/ขึ้นบรรทัดใหม่", () => {
|
||||
expect(parseCommaList("เงินสด, , โอน")).toEqual(["เงินสด", "โอน"]);
|
||||
expect(parseCommaList("เงินสด,โอน")).toEqual(["เงินสด", "โอน"]);
|
||||
expect(parseCommaList("ก\nข")).toEqual(["ก", "ข"]);
|
||||
});
|
||||
|
||||
it("ค่าว่างได้อาร์เรย์ว่าง", () => {
|
||||
expect(parseCommaList(" ")).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatList", () => {
|
||||
it("ต่อด้วย ', ' เสมอ", () => {
|
||||
expect(formatList(["a", "b"])).toBe("a, b");
|
||||
expect(formatList([])).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("normalizeSlug", () => {
|
||||
it("ตัวเล็ก + ช่องว่างเป็นขีด", () => {
|
||||
expect(normalizeSlug("Mecani Cinema")).toBe("mecani-cinema");
|
||||
});
|
||||
|
||||
it("ตัดอักขระที่ไม่ใช่ a-z0-9- และยุบขีดซ้ำ", () => {
|
||||
expect(normalizeSlug(" HOUSE & Co. ")).toBe("house-co");
|
||||
expect(normalizeSlug("a__b")).toBe("a-b");
|
||||
expect(normalizeSlug("--a--b--")).toBe("a-b");
|
||||
});
|
||||
|
||||
it("ภาษาไทยล้วนได้ค่าว่าง (ไม่ยอมให้บันทึก พร้อมเหตุผล)", () => {
|
||||
expect(normalizeSlug("เมคานี ใหม่")).toBe("");
|
||||
expect(slugProblem("", { others: [] })).toBe("slug ว่างไม่ได้");
|
||||
});
|
||||
});
|
||||
|
||||
describe("suggestSlug", () => {
|
||||
it("แปลงชื่ออังกฤษเป็น slug", () => {
|
||||
expect(suggestSlug("HOUSE CINEMA SAMYAN")).toBe("house-cinema-samyan");
|
||||
expect(suggestSlug("MECANI")).toBe("mecani");
|
||||
});
|
||||
|
||||
it("& กลายเป็น 'and' และตัด apostrophe", () => {
|
||||
expect(suggestSlug("Doc Club & Friends")).toBe("doc-club-and-friends");
|
||||
expect(suggestSlug("O'Brien Cinema")).toBe("obrien-cinema");
|
||||
});
|
||||
|
||||
it("ตัดเสียง accent", () => {
|
||||
expect(suggestSlug("Café Lumière")).toBe("cafe-lumiere");
|
||||
});
|
||||
});
|
||||
|
||||
describe("slugProblem", () => {
|
||||
it("ซ้ำกับโรงอื่น = ใช้ไม่ได้", () => {
|
||||
expect(
|
||||
slugProblem("mecani", { others: ["mecani", "loprama"], self: "loprama" }),
|
||||
).toBe("slug นี้มีโรงอื่นใช้อยู่");
|
||||
});
|
||||
|
||||
it("ซ้ำกับตัวเอง = ผ่าน (ตอนแก้ฟิลด์อื่น)", () => {
|
||||
expect(slugProblem("mecani", { others: ["mecani"], self: "mecani" })).toBeNull();
|
||||
});
|
||||
|
||||
it("รูปแบบผิด เช่นขีดหัว/ท้าย = ใช้ไม่ได้", () => {
|
||||
expect(slugProblem("-mecani", { others: [] })).toContain("a-z");
|
||||
expect(slugProblem("mecani--x", { others: [] })).toContain("a-z");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user