Files
web-alterflixhub/app/components/admin/CinemaList.tsx
T
2026-11-01 17:24:44 +07:00

254 lines
10 KiB
TypeScript

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>
);
}