From 5fdd78acc6b032788abaa848749ee0db2c42af6f Mon Sep 17 00:00:00 2001 From: BANK Date: Tue, 27 Oct 2026 22:24:30 +0700 Subject: [PATCH] feat(app): JSON-driven theaters, admin CRUD with login, hidden flag, image upload, PWA --- .gitignore | 1 + app/.gitignore | 7 +- app/DESIGN.md | 22 +- app/app/lib/auth.server.ts | 101 ++++ app/app/lib/images.server.ts | 88 +++ app/app/lib/theaters-store.server.ts | 85 +++ app/app/lib/theaters.ts | 156 +++++ app/app/root.tsx | 46 +- app/app/routes/_index.tsx | 303 +++++++--- app/app/routes/admin.image.tsx | 51 ++ app/app/routes/admin.login.tsx | 129 ++++ app/app/routes/admin.logout.tsx | 16 + app/app/routes/admin.save.tsx | 54 ++ app/app/routes/admin.session.tsx | 17 + app/app/routes/admin.tsx | 860 +++++++++++++++++++++++++++ app/app/routes/cinema.$slug.tsx | 374 +++++++++++- app/app/routes/single.tsx | 115 ++++ app/public/data/theaters.json | 192 ++++++ app/public/images/theaters/.gitkeep | 0 app/public/manifest.json | 33 +- app/public/sw.js | 120 ++++ app/public/version.json | 6 + 22 files changed, 2668 insertions(+), 108 deletions(-) create mode 100644 app/app/lib/auth.server.ts create mode 100644 app/app/lib/images.server.ts create mode 100644 app/app/lib/theaters-store.server.ts create mode 100644 app/app/lib/theaters.ts create mode 100644 app/app/routes/admin.image.tsx create mode 100644 app/app/routes/admin.login.tsx create mode 100644 app/app/routes/admin.logout.tsx create mode 100644 app/app/routes/admin.save.tsx create mode 100644 app/app/routes/admin.session.tsx create mode 100644 app/app/routes/admin.tsx create mode 100644 app/app/routes/single.tsx create mode 100644 app/public/data/theaters.json create mode 100644 app/public/images/theaters/.gitkeep create mode 100644 app/public/sw.js create mode 100644 app/public/version.json diff --git a/.gitignore b/.gitignore index 2c084e1..2cf1341 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,4 @@ desktop.ini # temp .tmp/ *.local +/New folder/ diff --git a/app/.gitignore b/app/.gitignore index acf4309..f13729a 100644 --- a/app/.gitignore +++ b/app/.gitignore @@ -30,14 +30,19 @@ dist-ssr !.env.example .tmp/ -**/data/ +/data/ data/pglite/ data/uploads/ data/settings.json data/.sessions.json +# ข้อมูลโรงหนังเป็นเนื้อหาของเว็บ (ไม่ใช่ local data) — ต้อง commit +!public/data/ +!public/data/theaters.json + learnings.md .vercel/ .agent-native/ data/*.lock +public/data/theaters.backup.json diff --git a/app/DESIGN.md b/app/DESIGN.md index 45928ff..c5fe965 100644 --- a/app/DESIGN.md +++ b/app/DESIGN.md @@ -9,7 +9,6 @@ that direction: once filled in, read and preserve them on every build; while they are still empty, the first substantial UI pass fills them in. ## Non-negotiable: impress by default - When this app is still a blank canvas, every app generated from this template must look **spectacular on first load**, even when the build prompt gives no design direction. "Looks like a clean, intentional product" is the floor, not @@ -65,3 +64,24 @@ Surface And Page Boundaries so the surfaces are wired correctly. - Density comes from data, not prose. Subtract explanatory chrome; never subtract the visual craft that makes the app impressive. - Compare against real products in the chosen mode, not against the starter. + +## AlterFlixHub — ของจริงที่ใช้อยู่ (อัปเดต 2026-10-27) + +**แบรนด์:** แดง #8F0808, พื้นดำ #0D0D0D, การ์ด #1B1B1B, เส้นขอบ #2A2A2A/#332B2B +โลโก้ฟอนต์ Jersey 15 (มีเฉพาะชื่อแบรนด์) · เนื้อหาไทยใช้ Inter + Noto Sans Thai + +**หน้าเว็บ** +- `/` หน้าแรก: hero + ช่องค้นหา + การ์ดโรงหนัง (การ์ดมีรูป = รูปปก + gradient ทับให้ตัวหนังสืออ่านออก) +- `/cinema/:slug` รายละเอียด: รูปใหญ่ + ป้ายหมวด + ข้อมูลเดินทาง/ติดต่อ/สิ่งอำนวยความสะดวก + "โรงหนังอิสระอื่นๆ" +- `/admin` หลังบ้าน (ต้องล็อกอินที่ `/admin/login`): ฟอร์มแก้ข้อมูล, อัปโหลดรูป, ปุ่มซ่อน/แสดง, ลบโรง +- `/single` เก็บหน้าเวอร์ชันเดิม (single-page + detail sheet) ไว้เทียบ + +**สถานะการซ่อน:** `hidden: true` ใน JSON = ไม่ขึ้นหน้าแรก/ตัวนับ แต่เปิดด้วย URL ตรงได้ และขึ้นป้าย "ซ่อนจากหน้าแรก" ที่หน้ารายละเอียด + +**รูปโรงหนัง:** เก็บไฟล์จริงที่ `public/images/theaters/` ผ่าน `/admin/image` (JPG/PNG/WebP/AVIF ≤ 6 MB) +ใช้ `object-cover` + กันที่ด้วย aspect ratio (การ์ด 3:4 / รูปใหญ่ 269:202) → ไม่เกิด layout shift +รูปการ์ดใช้ `loading="lazy"` (อยู่ใต้ fold) รูปใหญ่หน้ารายละเอียดโหลดทันที + +**คอนทราสต์ที่วัดแล้ว (WCAG)**: #B3B3B3/#111 = 8.9:1 · #e0b357/#2a1f00 = 8.4:1 · #ff9a9a/#111 = 9.4:1 · ขาว/#8F0808 = 9.1:1 — ผ่าน AA ทั้งหมด + +**PWA:** `public/sw.js` (network-first หน้าเว็บ + `/data/`, cache-first assets, ข้าม `/_agent-native/`), bump `CACHE_VERSION` เมื่อแก้ asset, manifest ผูก `/icon-192.svg` + `/icon-512.svg` diff --git a/app/app/lib/auth.server.ts b/app/app/lib/auth.server.ts new file mode 100644 index 0000000..54b3e69 --- /dev/null +++ b/app/app/lib/auth.server.ts @@ -0,0 +1,101 @@ +import crypto from "node:crypto"; + +import { redirect } from "react-router"; + +/** + * ยืนยันตัวตนสำหรับหน้าหลังบ้าน (/admin) + * ------------------------------------------------------------ + * ★ ผู้ใช้/รหัสค่าเริ่มต้น: it's me / it's me&it's me (override ด้วย env ได้) + * ★ session = cookie HttpOnly เซ็นด้วย HMAC — ไม่เก็บรหัสใน cookie + * ★ อายุ session 12 ชั่วโมง + * ★ ใช้ฝั่ง server เท่านั้น (ห้าม import ใน component ฝั่ง client) + */ + +export const ADMIN_USERNAME = process.env.ADMIN_USERNAME ?? "it's me"; +export const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD ?? "it's me&it's me"; + +const SECRET = + process.env.ADMIN_SESSION_SECRET ?? "alterflixhub-dev-secret-please-change"; +const COOKIE_NAME = "alterflix_admin"; +const MAX_AGE_SECONDS = 60 * 60 * 12; + +function sign(payload: string): string { + return crypto.createHmac("sha256", SECRET).update(payload).digest("base64url"); +} + +function safeEqual(a: string, b: string): boolean { + const ha = crypto.createHash("sha256").update(a).digest(); + const hb = crypto.createHash("sha256").update(b).digest(); + return crypto.timingSafeEqual(ha, hb); +} + +export function verifyCredentials(username: string, password: string): boolean { + return safeEqual(username, ADMIN_USERNAME) && safeEqual(password, ADMIN_PASSWORD); +} + +export function createSessionCookie(): string { + const expires = Date.now() + MAX_AGE_SECONDS * 1000; + const payload = `${ADMIN_USERNAME}.${expires}`; + const token = `${Buffer.from(payload).toString("base64url")}.${sign(payload)}`; + const parts = [ + `${COOKIE_NAME}=${token}`, + "Path=/", + "HttpOnly", + "SameSite=Lax", + `Max-Age=${MAX_AGE_SECONDS}`, + ]; + return parts.join("; "); +} + +export function clearSessionCookie(): string { + return `${COOKIE_NAME}=; Path=/; HttpOnly; SameSite=Lax; Max-Age=0`; +} + +function readCookie(request: Request, name: string): string | null { + const header = request.headers.get("cookie"); + if (!header) return null; + for (const chunk of header.split(";")) { + const [key, ...rest] = chunk.trim().split("="); + if (key === name) return rest.join("="); + } + return null; +} + +export type AdminSession = { username: string; expires: number }; + +export function getSession(request: Request): AdminSession | null { + const token = readCookie(request, COOKIE_NAME); + if (!token) return null; + const [encodedPayload, signature] = token.split("."); + if (!encodedPayload || !signature) return null; + + let payload: string; + try { + payload = Buffer.from(encodedPayload, "base64url").toString("utf8"); + } catch { + return null; + } + if (!safeEqual(signature, sign(payload))) return null; + + const [username, expiresRaw] = payload.split("."); + const expires = Number(expiresRaw); + if (!username || !Number.isFinite(expires) || expires < Date.now()) return null; + + return { username, expires }; +} + +/** ใช้ใน loader/action ของหน้าที่ต้องล็อกอิน — เด้งไป /login ถ้ายังไม่มี session */ +export function requireAdmin(request: Request): AdminSession { + const session = getSession(request); + if (!session) { + const url = new URL(request.url); + const next = `${url.pathname}${url.search}`; + throw redirect(`/admin/login?next=${encodeURIComponent(next)}`); + } + return session; +} + +/** ตรวจจาก handler ที่ไม่ใช่ loader (เช่น API) — คืน true/false */ +export function isAdminRequest(request: Request): boolean { + return getSession(request) !== null; +} diff --git a/app/app/lib/images.server.ts b/app/app/lib/images.server.ts new file mode 100644 index 0000000..281723d --- /dev/null +++ b/app/app/lib/images.server.ts @@ -0,0 +1,88 @@ +import fs from "node:fs/promises"; +import path from "node:path"; + +/** + * จัดการไฟล์รูปโรงหนัง (ฝั่ง server เท่านั้น) + * ------------------------------------------------------------ + * ★ เก็บที่ public/images/theaters/ → เสิร์ฟเป็น /images/theaters/<ไฟล์> + * ★ รับเฉพาะ jpeg/png/webp/avif และไม่เกิน 6 MB + * ★ ชื่อไฟล์부터 slug + เวลา → อัปรูปซ้ำได้ ไม่ทับของเก่า (แล้วลบไฟล์เก่าให้) + * ★ ตรวจ path ก่อนลบ — ลบได้เฉพาะไฟล์ในโฟลเดอร์ของตัวเอง + */ + +const IMAGE_DIR = path.join(process.cwd(), "public", "images", "theaters"); +const PUBLIC_PREFIX = "/images/theaters/"; + +const MIME_EXT: Record = { + "image/jpeg": "jpg", + "image/jpg": "jpg", + "image/png": "png", + "image/webp": "webp", + "image/avif": "avif", +}; + +export const MAX_IMAGE_BYTES = 6 * 1024 * 1024; +export const ACCEPTED_MIME = Object.keys(MIME_EXT); +export const ACCEPT_ATTR = ".jpg,.jpeg,.png,.webp,.avif,image/jpeg,image/png,image/webp,image/avif"; + +export type ImageSaveResult = + | { ok: true; url: string; bytes: number } + | { ok: false; message: string }; + +function safeSlug(slug: string): string { + const cleaned = slug + .toLowerCase() + .replace(/[^a-z0-9-]+/g, "-") + .replace(/^-+|-+$/g, "") + .slice(0, 60); + return cleaned || "theater"; +} + +export async function saveTheaterImage( + slug: string, + file: File, +): Promise { + const ext = MIME_EXT[file.type.toLowerCase()]; + if (!ext) { + return { + ok: false, + message: "รองรับเฉพาะไฟล์ JPG, PNG, WebP หรือ AVIF", + }; + } + if (file.size === 0) return { ok: false, message: "ไฟล์ว่าง" }; + if (file.size > MAX_IMAGE_BYTES) { + return { ok: false, message: "ไฟล์ใหญ่เกิน 6 MB" }; + } + + await fs.mkdir(IMAGE_DIR, { recursive: true }); + + const filename = `${safeSlug(slug)}-${Date.now()}.${ext}`; + const buffer = Buffer.from(await file.arrayBuffer()); + await fs.writeFile(path.join(IMAGE_DIR, filename), buffer); + + return { ok: true, url: `${PUBLIC_PREFIX}${filename}`, bytes: buffer.byteLength }; +} + +/** ลบรูปเดิม (เฉพาะ path ที่อยู่ใน /images/theaters/) — ใช้ตอนอัปใหม่หรือกดลบรูป */ +export async function deleteTheaterImage(url: string | null): Promise { + if (!url || !url.startsWith(PUBLIC_PREFIX)) return false; + const filename = url.slice(PUBLIC_PREFIX.length); + if (!filename || filename.includes("/") || filename.includes("..")) return false; + + try { + await fs.unlink(path.join(IMAGE_DIR, filename)); + return true; + } catch { + return false; + } +} + +/** รายชื่อไฟล์รูปที่เก็บไว้ (ใช้นับ/ตรวจในหน้าหลังบ้าน) */ +export async function listTheaterImages(): Promise { + try { + const files = await fs.readdir(IMAGE_DIR); + return files.filter((name) => MIME_EXT[`image/${name.split(".").pop()}`] ?? true); + } catch { + return []; + } +} diff --git a/app/app/lib/theaters-store.server.ts b/app/app/lib/theaters-store.server.ts new file mode 100644 index 0000000..deb2b13 --- /dev/null +++ b/app/app/lib/theaters-store.server.ts @@ -0,0 +1,85 @@ +import fs from "node:fs/promises"; +import path from "node:path"; + +import type { Theater, TheatersFile } from "./theaters"; +import { validateTheatersFile } from "./theaters"; + +/** + * ชั้นอ่าน/เขียนไฟล์ JSON (ใช้ฝั่ง server เท่านั้น) + * ------------------------------------------------------------ + * ★ ข้อมูลจริงอยู่ที่ public/data/theaters.json (อ่านง่าย แก้มือได้) + * ★ ก่อนเขียนทุกครั้ง สำรองของเดิมไว้ที่ theaters.backup.json + * ★ หน้าบ้าน (loader) อ่านผ่าน readTheatersFile() — ไม่มีสำเนาข้อมูลในโค้ด + */ + +const DATA_DIR = path.join(process.cwd(), "public", "data"); +const FILE_PATH = path.join(DATA_DIR, "theaters.json"); +const BACKUP_PATH = path.join(DATA_DIR, "theaters.backup.json"); + +const EMPTY_FILE: TheatersFile = { + updated_at: new Date().toISOString(), + theaters: [], +}; + +export function theatersFilePath(): string { + return FILE_PATH; +} + +export async function readTheatersFile(): Promise { + try { + const raw = await fs.readFile(FILE_PATH, "utf8"); + const parsed = JSON.parse(raw) as TheatersFile; + const error = validateTheatersFile(parsed); + if (error) { + return { ...EMPTY_FILE, updated_at: new Date().toISOString() }; + } + return { + updated_at: parsed.updated_at ?? new Date().toISOString(), + theaters: parsed.theaters, + }; + } catch { + return { ...EMPTY_FILE, updated_at: new Date().toISOString() }; + } +} + +export async function readTheaters(): Promise { + const file = await readTheatersFile(); + return file.theaters; +} + +export type WriteResult = { + saved: number; + updated_at: string; + backup_created: boolean; +}; + +/** เขียนทับไฟล์ข้อมูล — สำรองของเดิมก่อนเสมอ */ +export async function writeTheaters(theaters: Theater[]): Promise { + const error = validateTheatersFile({ theaters }); + if (error) throw new Error(error); + + await fs.mkdir(DATA_DIR, { recursive: true }); + + let backupCreated = false; + try { + await fs.copyFile(FILE_PATH, BACKUP_PATH); + backupCreated = true; + } catch { + backupCreated = false; + } + + const next: TheatersFile = { + updated_at: new Date().toISOString(), + theaters, + }; + + // เขียนแบบ JSON อ่านง่าย: 2 spaces + CRLF ปิดท้ายไฟล์ + const text = `${JSON.stringify(next, null, 2)}\n`; + await fs.writeFile(FILE_PATH, text, "utf8"); + + return { + saved: theaters.length, + updated_at: next.updated_at, + backup_created: backupCreated, + }; +} diff --git a/app/app/lib/theaters.ts b/app/app/lib/theaters.ts new file mode 100644 index 0000000..c6dce05 --- /dev/null +++ b/app/app/lib/theaters.ts @@ -0,0 +1,156 @@ +import { + IconBook, + IconBriefcase, + IconBus, + IconCoffee, + IconPlant2, + IconShoppingBag, + IconSofa, + IconTicket, + IconToolsKitchen2, + IconTrain, +} from "@tabler/icons-react"; +import type { ComponentType } from "react"; + +/** ---------- ชนิดข้อมูล (ตรงกับ public/data/theaters.json) ---------- */ + +export type TheaterTransport = { + /** ชื่อไอคอนใน ICONS */ + icon: string; + label: string; + value: string; +}; + +export type TheaterContact = { label: string; value: string }; + +export type TheaterFacility = { + /** ชื่อไอคอนใน ICONS */ + icon: string; + title: string; + sub: string; +}; + +export type Theater = { + /** ใช้ใน URL: /cinema/ */ + slug: string; + name: string; + area: string; + distance: string; + categories: string[]; + /** null = ยังไม่มีข้อมูลรายละเอียด */ + description: string | null; + address: string | null; + map_url: string | null; + transport: TheaterTransport[]; + contacts: TheaterContact[]; + payments: string[]; + facilities: TheaterFacility[]; + /** true = ซ่อนจากหน้าแรก/รายการสาธารณะ (ยังเปิดดูด้วย URL ตรงได้) */ + hidden: boolean; + /** รูปปกโรงหนัง — path ในเว็บ (/images/theaters/...) หรือ URL เต็ม; null = ใช้พื้นหลังไล่สี */ + image: string | null; +}; + +export type TheatersFile = { + updated_at: string; + theaters: Theater[]; +}; + +/** ที่อยู่ไฟล์ข้อมูล (public/) — ใช้ทั้งตอนอ่านฝั่ง server และ fetch ฝั่ง client */ +export const THEATERS_JSON_PATH = "/data/theaters.json"; + +/** ---------- ไอคอน ---------- */ + +type IconComponent = ComponentType<{ size?: number; stroke?: number }>; + +export const ICONS: Record = { + bus: IconBus, + train: IconTrain, + sofa: IconSofa, + ticket: IconTicket, + food: IconToolsKitchen2, + cafe: IconCoffee, + garden: IconPlant2, + book: IconBook, + workspace: IconBriefcase, + shop: IconShoppingBag, +}; + +/** รายชื่อไอคอนที่เลือกได้ในหน้าหลังบ้าน */ +export const ICON_NAMES = Object.keys(ICONS); + +export function resolveIcon(name: string): IconComponent | null { + return ICONS[name] ?? null; +} + +/** ---------- ตัวช่วย ---------- */ + +export function getTheater( + theaters: Theater[], + slug: string | undefined, +): Theater | undefined { + if (!slug) return undefined; + return theaters.find((theater) => theater.slug === slug); +} + +/** สร้าง slug จากชื่อโรง — ใช้ตอนเพิ่มโรงใหม่ในหน้าหลังบ้าน */ +export function toSlug(name: string): string { + return name + .trim() + .toLowerCase() + .replace(/[^a-z0-9ก-๙]+/g, "-") + .replace(/^-+|-+$/g, ""); +} + +export const CATEGORY_OPTIONS = [ + "ยอดนิยม", + "อาร์ตเฮาส์", + "สารคดี", + "หนังสั้น", + "หนังคลาสสิก", + "หนังนานาชาติ", + "หนังนอกกระแส", + "หนังในกระแส", + "หนังฝรั่งเศส", +]; + +export function emptyTheater(name = "โรงหนังใหม่"): Theater { + return { + slug: toSlug(name) || `theater-${Date.now()}`, + name, + area: "", + distance: "—", + categories: [], + description: null, + address: null, + map_url: null, + transport: [], + contacts: [], + payments: [], + facilities: [], + hidden: false, + image: null, + }; +} + +/** ตรวจโครงสร้างไฟล์ JSON — คืน null ถ้าผ่าน, คืนข้อความถ้าไม่ผ่าน */ +export function validateTheatersFile(value: unknown): string | null { + if (!value || typeof value !== "object") return "ไฟล์ต้องเป็น object"; + const file = value as Partial; + if (!Array.isArray(file.theaters)) return "ต้องมีคีย์ theaters เป็น array"; + const slugs = new Set(); + for (const [i, theater] of file.theaters.entries()) { + if (!theater || typeof theater !== "object") { + return `รายการที่ ${i + 1} ไม่ใช่ object`; + } + if (typeof theater.slug !== "string" || !theater.slug) { + return `รายการที่ ${i + 1} ไม่มี slug`; + } + if (slugs.has(theater.slug)) return `slug ซ้ำ: ${theater.slug}`; + slugs.add(theater.slug); + if (typeof theater.name !== "string" || !theater.name) { + return `รายการที่ ${i + 1} ไม่มี name`; + } + } + return null; +} diff --git a/app/app/root.tsx b/app/app/root.tsx index 3723f9d..1d88b81 100644 --- a/app/app/root.tsx +++ b/app/app/root.tsx @@ -5,7 +5,7 @@ import { createAgentNativeQueryClient } from "@agent-native/core/client/hooks"; import { getThemeInitScript } from "@agent-native/core/client/ui"; import { AppProviders } from "@agent-native/toolkit/app/providers"; import { useQueryClient } from "@tanstack/react-query"; -import { useState } from "react"; +import { useEffect, useState } from "react"; import { Links, Meta, Outlet, Scripts, ScrollRestoration } from "react-router"; import type { LinksFunction } from "react-router"; @@ -26,8 +26,51 @@ configureTracking({ export const links: LinksFunction = () => [ { rel: "stylesheet", href: stylesheet }, + { rel: "manifest", href: appPath("/manifest.json") }, ]; +/** ลงทะเบียน Service Worker — อัปเดตแล้ว reload ครั้งเดียวเมื่อตัวใหม่เข้าคุมหน้า */ +function useServiceWorker() { + useEffect(() => { + if (typeof navigator === "undefined" || !("serviceWorker" in navigator)) { + return; + } + let reloaded = false; + const onControllerChange = () => { + if (reloaded) return; + reloaded = true; + window.location.reload(); + }; + navigator.serviceWorker.addEventListener( + "controllerchange", + onControllerChange, + ); + navigator.serviceWorker + .register("/sw.js", { scope: "/" }) + .then((registration) => { + registration.addEventListener("updatefound", () => { + const installing = registration.installing; + installing?.addEventListener("statechange", () => { + if ( + installing.state === "installed" && + navigator.serviceWorker.controller + ) { + installing.postMessage("SKIP_WAITING"); + } + }); + }); + }) + .catch(() => undefined); + + return () => { + navigator.serviceWorker.removeEventListener( + "controllerchange", + onControllerChange, + ); + }; + }, []); +} + const THEME_INIT_SCRIPT = getThemeInitScript(); export function Layout({ children }: { children: React.ReactNode }) { @@ -79,6 +122,7 @@ function DbSyncSetup() { export default function Root() { const [queryClient] = useState(() => createAgentNativeQueryClient()); + useServiceWorker(); return ( diff --git a/app/app/routes/_index.tsx b/app/app/routes/_index.tsx index 6743aa5..aeb0490 100644 --- a/app/app/routes/_index.tsx +++ b/app/app/routes/_index.tsx @@ -1,115 +1,260 @@ -import { useEffect } from "react"; -import { useSearchParams } from "react-router"; +import { useState } from "react"; +import { + IconArrowRight, + IconBrandFacebook, + IconBrandInstagram, + IconBrandLinkedin, + IconBrandTelegram, + IconBrandYoutube, + IconBuildingCommunity, + IconChevronDown, + IconMapPin, + IconSearch, + IconSettings, +} from "@tabler/icons-react"; +import { Link, useLoaderData } from "react-router"; -import { CINEMAS, getCinema } from "@/lib/cinemas"; -import { useLang } from "@/lib/i18n"; -import { CinemaCard } from "@/components/cinema/CinemaCard"; -import { CinemaDetailSheet } from "@/components/cinema/CinemaDetailSheet"; -import { SiteFooter, SiteHeader } from "@/components/cinema/SiteHeader"; +import { readTheatersFile } from "@/lib/theaters-store.server"; +import type { Theater } from "@/lib/theaters"; export function meta() { return [ - { title: "AlterFlixHub — โรงภาพยนตร์" }, + { title: "AlterFlixHub — โรงหนังอิสระ" }, { name: "description", - content: "เลือกโรงภาพยนตร์ที่ใช่ ดูรอบฉายและรายละเอียดได้ในที่เดียว", + content: "ค้นหาโรงภาพยนตร์อิสระ ทางเลือกในการดูหนัง", }, ]; } +/** อ่านข้อมูลจาก public/data/theaters.json ทุกครั้งที่มี request */ +export async function loader() { + const file = await readTheatersFile(); + return { theaters: file.theaters, updatedAt: file.updated_at }; +} + +const HERO_IMAGE = + "https://api.builder.io/api/v1/image/assets/TEMP/9e066417bc0d24ee206fcf20fd53ef301362fdc4?width=2880"; + +const CATEGORIES = [ + "ทั้งหมด", + "ยอดนิยม", + "อาร์ตเฮาส์", + "สารคดี", + "หนังสั้น", + "หนังคลาสสิก", + "หนังนานาชาติ", + "หนังนอกกระแส", + "หนังในกระแส", + "หนังฝรั่งเศส", +]; + +const SOCIALS = [ + IconBrandFacebook, + IconBrandInstagram, + IconBrandLinkedin, + IconBrandYoutube, + IconBrandTelegram, +]; + +const CARD_GRADIENTS = [ + "from-[#3a2a22] via-[#2a2a3a] to-[#1b1b1b]", + "from-[#2a2a3a] via-[#1f2a3a] to-[#1b1b1b]", + "from-[#3a3a4a] via-[#2a2a2a] to-[#1b1b1b]", + "from-[#4a3320] via-[#2a2a2a] to-[#1b1b1b]", +]; + +function Logo({ className = "" }: { className?: string }) { + return ( + + + AlterFlixHub + + + ); +} + /** - * หน้าแรก (Home) — รายการโรงภาพยนตร์ + รายละเอียดในหน้าเดียวกัน - * ------------------------------------------------------------ - * ★ เว็บหน้าเดียว: การ์ด → เปิด sheet ทับ ไม่เปลี่ยน route, ไม่รีเฟรช - * รายการด้านหลังไม่ unmount → scroll ที่เลื่อนไว้ยังอยู่ - * ★ URL = /?cinema= (pushState) → แชร์ลิงก์ได้, back ปิด sheet - * ★ มือถือ 6" (360–430px): การ์ด 1 คอลัมน์แนวนอน, sheet เต็มจอ - * ★ แท็บเล็ต (640–1023px): 2 คอลัมน์ - * ★ เดสก์ท็อป (≥1024px): 3 คอลัมน์, sheet กลางจอ + * หน้าแรก — รายการโรงหนังอิสระ (อ่านจาก JSON) + * ★ การ์ดลิงก์ไป /cinema/ + * ★ ปุ่มหมวดกรองฝั่ง client + * ★ ลิงก์ "จัดการข้อมูล" → /admin (ระบบหลังบ้าน) */ export default function HomeRoute() { - const { t } = useLang(); - const [searchParams, setSearchParams] = useSearchParams(); - const activeSlug = searchParams.get("cinema"); - const activeCinema = getCinema(activeSlug ?? undefined); - - /** เปิดรายละเอียด — push history entry (ปุ่ม back ของเบราว์เซอร์จะปิด sheet) */ - const openCinema = (slug: string) => { - setSearchParams({ cinema: slug }); + const { theaters: all } = useLoaderData() as { + theaters: Theater[]; + updatedAt: string; }; + const [active, setActive] = useState(CATEGORIES[0]); - /** ปิด — replace เพื่อไม่ให้ history บวมด้วย entry ของ state "ปิด" */ - const closeCinema = () => { - setSearchParams({}, { replace: true }); - }; + // โรงที่ถูกซ่อนในหลังบ้าน (hidden=true) ไม่ขึ้นหน้าแรก + const visible = all.filter((theater) => !theater.hidden); - /* title เปลี่ยนตามโรงที่เปิด — ลิงก์ที่แชร์จะได้ชื่อถูก */ - useEffect(() => { - document.title = activeCinema - ? `${activeCinema.name} — AlterFlixHub` - : "AlterFlixHub — โรงภาพยนตร์"; - }, [activeCinema]); + const theaters = + active === "ทั้งหมด" + ? visible + : visible.filter((theater) => theater.categories.includes(active)); return ( -
- {/* แสงแดงเรือง ๆ หลัง hero — decorative */} -