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("manual"); const [dragSlug, setDragSlug] = useState(null); const [overSlug, setOverSlug] = useState(null); const visible = useMemo( () => (mode === "thai" ? sortCinemasByName(cinemas) : cinemas), [cinemas, mode], ); function handleDrop(event: React.DragEvent, 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 (
{mode === "thai" ? (
เรียงตามชื่อไทย ก-ฮ · โหมดนี้ลากสลับไม่ได้
) : (

ลากชื่อเพื่อสลับตำแหน่ง หรือกด ↑ ↓ ทีละขั้น

)} {dirty ? ( ) : null}
    {visible.map((item, index) => { const isSelected = item.slug === selected; const isDragging = dragSlug === item.slug; const isOver = overSlug === item.slug && dragSlug !== item.slug; return (
  • { 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" : ""}`} > {mode === "manual" ? ( ) : null}
  • ); })}
); }