From 1cf1398ba44f04bde1bacbd4b50a064e86501a35 Mon Sep 17 00:00:00 2001 From: Bkk1234 Date: Mon, 16 Nov 2026 01:54:15 +0700 Subject: [PATCH] =?UTF-8?q?feat(admin):=20=E0=B8=A3=E0=B8=AD=E0=B8=87?= =?UTF-8?q?=E0=B8=A3=E0=B8=B1=E0=B8=9A=E0=B8=95=E0=B8=B1=E0=B8=A7=E0=B8=81?= =?UTF-8?q?=E0=B8=A3=E0=B8=AD=E0=B8=87=E0=B8=9B=E0=B8=A3=E0=B8=B0=E0=B9=80?= =?UTF-8?q?=E0=B8=A0=E0=B8=97=E0=B9=82=E0=B8=A3=E0=B8=87=E0=B8=AB=E0=B8=99?= =?UTF-8?q?=E0=B8=B1=E0=B8=87=E0=B9=80=E0=B8=95=E0=B9=87=E0=B8=A1=E0=B8=A3?= =?UTF-8?q?=E0=B8=B9=E0=B8=9B=E0=B9=81=E0=B8=9A=E0=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ลิสต์โรงหนัง: ช่องค้นหา (ไทย/อังกฤษ/slug) + ชิปกรองประเภทพร้อมจำนวน (ทั้งหมด/ท้องถิ่น/อินดี้/กลุ่ม/ยังไม่จัด) - ป้ายประเภทท้ายชื่อทุกแถว (ยังไม่จัด = ป้ายแดง) + empty state - ตั้งประเภททีละกลุ่ม: "ตั้งประเภทให้ N โรงที่กรองอยู่" → เลือกประเภท → ใช้ (แล้วกดบันทึกทั้งหมด) - หัวหลังบ้านสรุปจำนวนต่อประเภท - แท็บคำศัพท์: ปุ่มลัดสร้าง key `venues.<ประเภท>` สำหรับแปลชื่อตัวกรอง - ปิดลากสลับลำดับขณะกรอง + hint อธิบาย - logic ย้ายไป lib: filterAdminCinemas / setVenueForSlugs / venueCounts + unit tests (169 ผ่าน) - DESIGN.md / docs/admin-guide.md อัปเดต --- DESIGN.md | 2 + app/components/admin/CinemaList.tsx | 182 +++++++++++++++++++++++++--- app/lib/cinemas.spec.ts | 87 +++++++++++++ app/lib/cinemas.ts | 59 +++++++++ app/routes/admin.tsx | 53 ++++++++ docs/admin-guide.md | 10 ++ 6 files changed, 377 insertions(+), 16 deletions(-) diff --git a/DESIGN.md b/DESIGN.md index a81309f..c75251d 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -237,6 +237,8 @@ Single source of truth per concern — do not duplicate these: - **เลือกได้ทีละชั้น**: กดประเภท → ล้างหมวดเป็น `ทั้งหมด`; กดหมวด → ล้างประเภทเป็น `null`; `ทั้งหมด` ล้างทั้งสอง (กันการกรองซ้อนที่คนใช้คาดไม่ถึง) - ตัวกรองจริงอยู่ใน `filterCinemas(..., { venue })` ทำงานแบบ AND กับหมวดและคำค้น (เผื่ออนาคตจะเปิดให้ซ้อน) — `venue = null` คือไม่กรอง - โรงที่ยังไม่ตั้ง `venue_type` จะไม่โผล่เมื่อกรองด้วยประเภทใด ๆ · ตั้งค่าได้ที่หลังบ้าน → แท็บโรงหนัง → ข้อมูลหลัก → **ประเภทโรงหนัง** (sanitize ใน `content.server.ts` เท่านั้น) +- หลังบ้านต้องรองรับพร้อมกัน: ลิสต์ซ้ายมีช่องค้นหา + ชิปกรอง `ทั้งหมด/ท้องถิ่น/อินดี้/กลุ่ม/ยังไม่จัด` พร้อมจำนวน, ป้ายประเภทท้ายชื่อทุกแถว, กล่อง **ตั้งประเภททีละกลุ่ม** (`setVenueForSlugs`) และสรุปจำนวนในหัวหลังบ้าน · logic อยู่ใน `filterAdminCinemas` / `venueCounts` (`app/lib/cinemas.ts`) ไม่ใช่ใน component (มี unit test) +- ลากสลับลำดับถูกปิดเมื่อกรองอยู่ (`draggable={mode === "manual" && !isFiltered}`) เพราะลำดับของผลที่กรองแล้วไม่มีความหมาย - `venue_type` เป็นข้อมูล ไม่ใช่ข้อความ: ห้าม hardcode ป้ายใน component ต้องอ่าน `messages.venues` (คีย์ไทย) และ override ได้ผ่าน `site.terms` (`venues.<คีย์>`) - Popularity path (`category === "ยอดนิยม"`) ถูกปิดเมื่อมี `venue` — กันการจัดอันดับข้ามประเภท diff --git a/app/components/admin/CinemaList.tsx b/app/components/admin/CinemaList.tsx index 0433fb0..aa313d4 100644 --- a/app/components/admin/CinemaList.tsx +++ b/app/components/admin/CinemaList.tsx @@ -6,7 +6,23 @@ import { } from "@tabler/icons-react"; import { useMemo, useState } from "react"; -import type { Cinema } from "@/lib/cinemas"; +import { + VENUE_TYPES, + filterAdminCinemas, + venueCounts as countByVenue, + type Cinema, + type VenueType, + type VenueFilterValue, +} from "@/lib/cinemas"; + +/** ตัวกรองประเภทโรงหนังในลิสต์หลังบ้าน — "all" = ทุกประเภท, "unset" = ยังไม่จัดประเภท */ +type VenueFilter = VenueFilterValue; + +const venueShort: Record = { + โรงภาพยนตร์ท้องถิ่น: "ท้องถิ่น", + โรงภาพยนตร์อินดี้: "อินดี้", + กลุ่มคนฉายหนัง: "กลุ่ม", +}; /** * รายชื่อโรงหนังในหลังบ้าน — เรียง ก-ฮ และสลับตำแหน่งได้ @@ -28,7 +44,9 @@ function sortKey(cinema: Cinema): string { /** เรียงชื่อโรงหนังภาษาไทยตามพจนานุกรม ก-ฮ */ export function sortCinemasByName(cinemas: Cinema[]): Cinema[] { - return [...cinemas].sort((a, b) => thaiCollator.compare(sortKey(a), sortKey(b))); + return [...cinemas].sort((a, b) => + thaiCollator.compare(sortKey(a), sortKey(b)), + ); } /** เทียบว่าสองลิสต์มี slug เรียงเหมือนกันหรือยัง */ @@ -58,7 +76,11 @@ export function moveCinema( } /** เลื่อนขึ้น/ลงทีละหนึ่งตำแหน่ง (สำหรับปุ่มลูกศร) */ -export function shiftCinema(cinemas: Cinema[], slug: string, delta: number): Cinema[] { +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; @@ -79,6 +101,8 @@ type CinemaListProps = { onSelect: (slug: string) => void; onReorder: (next: Cinema[]) => void; onSaveOrder: () => void; + /** ตั้งประเภทให้หลายโรงพร้อมกัน (slug ที่กรองอยู่, ประเภทใหม่) */ + onBulkVenue?: (slugs: string[], venue: VenueType) => void; }; const modeButtonClass = (active: boolean) => @@ -97,14 +121,27 @@ export function CinemaList({ onSelect, onReorder, onSaveOrder, + onBulkVenue, }: CinemaListProps) { const [mode, setMode] = useState("manual"); + const [venueFilter, setVenueFilter] = useState("all"); + const [query, setQuery] = useState(""); + const [bulkVenue, setBulkVenue] = useState(VENUE_TYPES[0]); const [dragSlug, setDragSlug] = useState(null); const [overSlug, setOverSlug] = useState(null); + const venueCounts = useMemo(() => countByVenue(cinemas), [cinemas]); + + const filtered = useMemo( + () => filterAdminCinemas(cinemas, { venue: venueFilter, query }), + [cinemas, venueFilter, query], + ); + + const isFiltered = venueFilter !== "all" || query.trim() !== ""; + const visible = useMemo( - () => (mode === "thai" ? sortCinemasByName(cinemas) : cinemas), - [cinemas, mode], + () => (mode === "thai" ? sortCinemasByName(filtered) : filtered), + [filtered, mode], ); function handleDrop(event: React.DragEvent, toSlug: string) { @@ -139,6 +176,85 @@ export function CinemaList({ + {/* ค้นหา + กรองตามประเภทโรงหนัง (ชั้นกรองที่ 1 บนหน้าแรก) */} + setQuery(event.target.value)} + placeholder="ค้นชื่อหรือ slug…" + aria-label="ค้นหาโรงหนัง" + className="w-full rounded-lg border border-[#303030] bg-[#141414] px-2 py-1.5 text-[12px] text-[#E5E5E5] placeholder:text-[#666] focus:border-[#A92626] focus:outline-none" + /> +
+ {( + [ + ["all", "ทั้งหมด", cinemas.length], + ...VENUE_TYPES.map( + (venue) => + [venue, venueShort[venue], venueCounts[venue]] as const, + ), + ["unset", "ยังไม่จัด", venueCounts.unset] as const, + ] as const + ).map(([value, label, count]) => { + const active = venueFilter === value; + return ( + + ); + })} +
+ + {onBulkVenue && isFiltered ? ( +
+

+ ตั้งประเภทให้ {visible.length} โรงที่กรองอยู่ +

+
+ + +
+

+ ต้องกด "บันทึกทั้งหมด" อีกครั้งเพื่อบันทึกลงไฟล์ +

+
+ ) : null} + {mode === "thai" ? (
เรียงตามชื่อไทย ก-ฮ · โหมดนี้ลากสลับไม่ได้ @@ -155,7 +271,9 @@ export function CinemaList({
) : (

- ลากชื่อเพื่อสลับตำแหน่ง หรือกด ↑ ↓ ทีละขั้น + {isFiltered + ? "กำลังกรองอยู่ — ล้างตัวกรองก่อนจึงจะลาก/เลื่อนลำดับได้" + : "ลากชื่อเพื่อสลับตำแหน่ง หรือกด ↑ ↓ ทีละขั้น"}

)} @@ -178,7 +296,7 @@ export function CinemaList({ return (
  • { setDragSlug(item.slug); event.dataTransfer.effectAllowed = "move"; @@ -191,7 +309,9 @@ export function CinemaList({ setOverSlug(item.slug); }} onDragLeave={() => { - setOverSlug((current) => (current === item.slug ? null : current)); + setOverSlug((current) => + current === item.slug ? null : current, + ); }} onDrop={(event) => handleDrop(event, item.slug)} onDragEnd={() => { @@ -201,13 +321,17 @@ export function CinemaList({ 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]" : "" + isOver + ? "outline outline-2 outline-offset-[-2px] outline-[#A92626]" + : "" } ${mode === "manual" ? "cursor-grab active:cursor-grabbing" : ""}`} >
  • ); })} + {visible.length === 0 ? ( +
  • + ไม่พบโรงหนังตามตัวกรองนี้ +
  • + ) : null} ); diff --git a/app/lib/cinemas.spec.ts b/app/lib/cinemas.spec.ts index e33aa01..1f5f99d 100644 --- a/app/lib/cinemas.spec.ts +++ b/app/lib/cinemas.spec.ts @@ -6,6 +6,9 @@ import { hasVenue, listOtherCinemas, type Cinema, + filterAdminCinemas, + setVenueForSlugs, + venueCounts, } from "./cinemas"; import { filterCinemas } from "./search"; @@ -98,3 +101,87 @@ describe("hasVenue / filterCinemas (ชั้นกรองประเภท ).toEqual([local]); }); }); + +describe("หลังบ้าน: ตัวกรองประเภทโรงหนัง", () => { + const base = { + slug: "base", + name: "X", + tags: [], + location_label: "", + coordinates: null, + description: "", + map_url: null, + address_lines: [], + transport: [], + contacts: [], + payments: [], + photos: [], + } as Cinema; + const list: Cinema[] = [ + { + ...base, + slug: "a-local", + name: "Alpha", + name_th: "อัลฟา", + venue_type: "โรงภาพยนตร์ท้องถิ่น", + }, + { ...base, slug: "b-indie", name: "Beta", venue_type: "โรงภาพยนตร์อินดี้" }, + { ...base, slug: "c-group", name: "Gamma", venue_type: "กลุ่มคนฉายหนัง" }, + { ...base, slug: "d-none", name: "Delta" }, + ]; + + it("กรอง all / unset / แต่ละประเภท", () => { + expect(filterAdminCinemas(list, { venue: "all", query: "" })).toHaveLength( + 4, + ); + expect( + filterAdminCinemas(list, { venue: "unset", query: "" }).map( + (c) => c.slug, + ), + ).toEqual(["d-none"]); + expect( + filterAdminCinemas(list, { venue: "โรงภาพยนตร์อินดี้", query: "" }).map( + (c) => c.slug, + ), + ).toEqual(["b-indie"]); + }); + + it("ค้นด้วยชื่อไทย/อังกฤษ/slug ร่วมกับตัวกรอง", () => { + expect( + filterAdminCinemas(list, { venue: "all", query: "อัลฟา" }).map( + (c) => c.slug, + ), + ).toEqual(["a-local"]); + expect( + filterAdminCinemas(list, { venue: "โรงภาพยนตร์ท้องถิ่น", query: "beta" }), + ).toHaveLength(0); + expect( + filterAdminCinemas(list, { venue: "all", query: " GAMMA " })[0]?.slug, + ).toBe("c-group"); + }); + + it("setVenueForSlugs ตั้งค่าเฉพาะ slug ที่ระบุ และไม่แก้ตัวเดิม", () => { + const next = setVenueForSlugs(list, ["d-none", "b-indie"], "กลุ่มคนฉายหนัง"); + expect(next.find((c) => c.slug === "d-none")?.venue_type).toBe( + "กลุ่มคนฉายหนัง", + ); + expect(next.find((c) => c.slug === "b-indie")?.venue_type).toBe( + "กลุ่มคนฉายหนัง", + ); + expect(next.find((c) => c.slug === "a-local")?.venue_type).toBe( + "โรงภาพยนตร์ท้องถิ่น", + ); + expect(list.find((c) => c.slug === "d-none")?.venue_type).toBeUndefined(); + expect(setVenueForSlugs(list, [], "โรงภาพยนตร์อินดี้")).toBe(list); + }); + + it("venueCounts นับครบทุกช่อง", () => { + expect(venueCounts(list)).toEqual({ + all: 4, + unset: 1, + โรงภาพยนตร์ท้องถิ่น: 1, + โรงภาพยนตร์อินดี้: 1, + กลุ่มคนฉายหนัง: 1, + }); + }); +}); diff --git a/app/lib/cinemas.ts b/app/lib/cinemas.ts index 921e5a4..a76b5d4 100644 --- a/app/lib/cinemas.ts +++ b/app/lib/cinemas.ts @@ -373,6 +373,65 @@ export function hasVenue(cinema: Cinema, venue: VenueType | null): boolean { return venue === null || cinema.venue_type === venue; } +/** ตัวกรองประเภทในลิสต์หลังบ้าน — "all" = ทุกประเภท, "unset" = ยังไม่จัดประเภท */ +export type VenueFilterValue = VenueType | "all" | "unset"; + +/** กรองรายชื่อโรงหนังในหลังบ้านตามประเภท (ชั้นกรองที่ 1) + คำค้นชื่อ/slug */ +export function filterAdminCinemas( + cinemas: Cinema[], + { + venue, + query, + }: { + venue: VenueFilterValue; + query: string; + }, +): Cinema[] { + const needle = query.trim().toLowerCase(); + return cinemas.filter((cinema) => { + if (venue === "unset") { + if (cinema.venue_type) return false; + } else if (venue !== "all" && cinema.venue_type !== venue) { + return false; + } + if (!needle) return true; + return `${cinema.name} ${cinema.name_th ?? ""} ${cinema.slug}` + .toLowerCase() + .includes(needle); + }); +} + +/** ตั้งประเภทให้หลายโรงพร้อมกัน (คืนลิสต์ใหม่ ไม่แตะตัวเดิม) */ +export function setVenueForSlugs( + cinemas: Cinema[], + slugs: string[], + venue: VenueType, +): Cinema[] { + if (slugs.length === 0) return cinemas; + const target = new Set(slugs); + return cinemas.map((cinema) => + target.has(cinema.slug) ? { ...cinema, venue_type: venue } : cinema, + ); +} + +/** นับจำนวนโรงต่อประเภท (สำหรับสรุปในหลังบ้าน) */ +export function venueCounts( + cinemas: Cinema[], +): Record { + const counts: Record = { + all: cinemas.length, + unset: 0, + โรงภาพยนตร์ท้องถิ่น: 0, + โรงภาพยนตร์อินดี้: 0, + กลุ่มคนฉายหนัง: 0, + }; + for (const cinema of cinemas) { + if (cinema.venue_type) counts[cinema.venue_type] += 1; + else counts.unset += 1; + } + return counts; +} + export function findCinemaBySlug( cinemas: Cinema[], slug: string | undefined, diff --git a/app/routes/admin.tsx b/app/routes/admin.tsx index 2c6f90b..b397ac1 100644 --- a/app/routes/admin.tsx +++ b/app/routes/admin.tsx @@ -264,6 +264,18 @@ export default function AdminRoute() {

    {store.cinemas.length} โรงหนัง · {terms.length} คำศัพท์ ·{" "} {store.site.footer.length} ไอคอน footer + {" · "} + {VENUE_TYPES.map((venue) => { + const count = store.cinemas.filter( + (item) => item.venue_type === venue, + ).length; + return `${count} ${venue.replace("โรงภาพยนตร์", "")}${ + venue === VENUE_TYPES[VENUE_TYPES.length - 1] ? "" : " · " + }`; + }).join("")} + {" · "} + {store.cinemas.filter((item) => !item.venue_type).length}{" "} + ยังไม่จัดประเภท

    @@ -335,6 +347,16 @@ export default function AdminRoute() { onSelect={setSelected} onReorder={updateCinemas} onSaveOrder={() => void save({ cinemas: store.cinemas })} + onBulkVenue={(slugs, venue) => { + const target = new Set(slugs); + updateCinemas( + store.cinemas.map((item) => + target.has(item.slug) + ? { ...item, venue_type: venue } + : item, + ), + ); + }} /> @@ -933,6 +955,37 @@ export default function AdminRoute() { > + เพิ่มคำศัพท์ + {VENUE_TYPES.map((venue) => { + const key = `venues.${venue}`; + const exists = store.site.terms.some((term) => term.key === key); + return ( + + ); + })}
    diff --git a/docs/admin-guide.md b/docs/admin-guide.md index 59ff183..9797520 100644 --- a/docs/admin-guide.md +++ b/docs/admin-guide.md @@ -39,6 +39,16 @@ โหมดเรียง ก-ฮ เป็นแค่การดู — กด **ใช้ลำดับ ก-ฮ นี้** เพื่อเอาลำดับนั้นมาใช้จริง เมื่อลำดับเปลี่ยนจะมีปุ่ม **บันทึกลำดับใหม่** (หรือกด "บันทึกทั้งหมด" ก็ได้) +### กรอง/ค้นหาโรงหนังในลิสต์ (ตัวกรองชั้นแรก) + +เหนือรายการมี **ช่องค้นหา** (ชื่อไทย/อังกฤษ/slug) และ **ชิปกรองประเภท** พร้อมจำนวน: +`ทั้งหมด · ท้องถิ่น · อินดี้ · กลุ่ม · ยังไม่จัด` + +- แต่ละแถวมี **ป้ายประเภท** ท้ายชื่อ · โรงที่ยังไม่ตั้งประเภทจะเป็นป้ายแดง "ยังไม่จัด" +- **ตั้งประเภททีละกลุ่ม**: กรอง/ค้นหาให้เหลือเฉพาะโรงที่ต้องการ → กล่อง *"ตั้งประเภทให้ N โรงที่กรองอยู่"* → เลือกประเภท → **ใช้** → แล้วกด **บันทึกทั้งหมด** (ถ้าไม่บันทึกแล้วออกจากหน้า การเปลี่ยนจะหาย) +- ตอนกรองอยู่จะ **ลากสลับลำดับไม่ได้** (กันเรียงผิด) — ล้างตัวกรองก่อนจึงจะลากได้ +- คำแปลชื่อประเภทแก้ได้ที่แท็บ **คำศัพท์หลายภาษา** → ปุ่มลัด `+ คำแปลตัวกรอง: <ประเภท>` (สร้างคีย์ `venues.<ประเภท>` ให้) + > **หน้าแรกสลับการ์ดเองทุกวัน** — ลำดับที่จัดไว้จะถูกใช้เป็น "ลำดับฐาน" แล้วระบบหมุน/สลับให้อัตโนมัติ > เพื่อไม่ให้โรงใดอยู่ตำแหน่งเดิมสองวันติดกัน (ทุกคนเห็นเหมือนกันในวันเดียวกัน) — รายละเอียดดู DESIGN.md หัวข้อ "ลำดับการ์ดหน้าแรก"