Files
web-alterflixhub/app/routes/cinema.$slug.tsx
T
Bkk1234 319c271d23 feat: บทความ 2 หน้า + แชร์/ลิงก์ย่อ + PWA + แอนิเมชัน + SEO/AI + เอกสาร
- บทความจากหลังบ้าน: /for-film-lovers, /for-cinemas (ArticlePage + ArticleEditor แบบบล็อกย่อหน้า/รูป)
- แชร์: ปุ่มบนการ์ดและท้ายหน้ารายละเอียด · ลิงก์ย่อ /c/<slug> (302) · ธีมแก้วตาม DESIGN.md
- PWA: manifest/sw/offline + ถอดการล็อกซูม (WCAG 1.4.4) + touch target ≥44px
- แอนิเมชัน reveal: IntersectionObserver + gate ด้วย html.fx-motion · เคารพ prefers-reduced-motion
- ค้นหา: normalize/ตัดวรรณยุกต์ + AND หลายคำ + จัดอันดับ · รองรับ /?q= จาก SearchAction
- SEO: pageMeta (canonical/OG/Twitter) + JSON-LD (WebSite/Organization/BreadcrumbList/MovieTheater/Article)
- ไม่ฝังโดเมนในโค้ด: origin มาจาก request (x-forwarded-host/proto) ทั้ง canonical, sitemap, robots, ลิงก์แชร์
- กัน AI: robots.txt (Nitro route, บล็อก AI 21 ตัว) + ai.txt + OG image 1200x630
- เอกสาร: README สรุปโปรเจกต์, DESIGN.md (motion/PWA/SEO/ข้อมูล), architecture.html, admin-guide
- tests: 135 ผ่าน (16 ไฟล์)
2026-11-10 02:31:10 +07:00

215 lines
7.4 KiB
TypeScript

import { IconArrowBackUp } from "@tabler/icons-react";
import { useEffect } from "react";
import { Link, useParams } from "react-router";
import { CinemaNotFound } from "@/components/cinema/CinemaNotFound";
import { CinemaSummary } from "@/components/cinema/CinemaSummary";
import { ContactPanel } from "@/components/cinema/ContactPanel";
import { FacilitiesSection } from "@/components/cinema/FacilitiesSection";
import { Gallery } from "@/components/cinema/Gallery";
import { TransportList } from "@/components/cinema/TransportList";
import { CinemaGrid } from "@/components/site/CinemaGrid";
import { Reveal } from "@/components/site/Reveal";
import { ShareButton } from "@/components/site/ShareButton";
import { SiteShell } from "@/components/site/SiteShell";
import { useNearMe } from "@/hooks/use-near-me";
import { trackEvent } from "@/lib/analytics-client";
import { APP_TITLE } from "@/lib/app-config";
import { findCinemaBySlug, cinemaName, listOtherCinemas } from "@/lib/cinemas";
import { useContent } from "@/lib/content-context";
import { readContent } from "@/lib/content.server";
import { sortByDistance } from "@/lib/geo";
import { useI18n } from "@/lib/i18n";
import { DEFAULT_LOCALE, MESSAGES } from "@/lib/i18n/messages";
import { resolvePageOrigin } from "@/lib/page-origin";
import {
breadcrumbJsonLd,
cinemaJsonLd,
originFromMatches,
pageMeta,
} from "@/lib/seo";
/** head (title) ไม่เปลี่ยนตามภาษา — meta() ไม่มี access ต่อ cookie */
const HEAD_MESSAGES = MESSAGES[DEFAULT_LOCALE];
/** ฟิลด์ที่ต้องใช้ทำ SEO ของหน้าโรงหนัง (ส่งจาก loader เพื่อให้ crawler เห็นครบใน HTML แรก) */
type CinemaSeo = {
/** origin ของคำขอ (ไม่ฝังโดเมน) */
origin: string;
name: string | null;
description: string | null;
locationLabel: string | null;
coordinates: { latitude: number; longitude: number } | null;
mapUrl: string | null;
photo: string | null;
};
export async function loader({
params,
request,
}: {
params: { slug?: string };
request: Request;
}): Promise<CinemaSeo> {
const content = await readContent();
const cinema = findCinemaBySlug(content.cinemas, params.slug);
if (!cinema) {
return {
origin: resolvePageOrigin(request),
name: null,
description: null,
locationLabel: null,
coordinates: null,
mapUrl: null,
photo: null,
};
}
return {
origin: resolvePageOrigin(request),
name: cinema.name,
description: cinema.description.trim() === "" ? null : cinema.description,
locationLabel: cinema.location_label || null,
coordinates: cinema.coordinates,
mapUrl: cinema.map_url,
photo: cinema.photos.find((photo) => photo.trim() !== "") ?? null,
};
}
export function meta({
loaderData,
params,
matches,
}: {
loaderData?: CinemaSeo;
params?: { slug?: string };
matches?: Array<{ id?: string; data?: unknown }>;
}) {
const origin = loaderData?.origin ?? originFromMatches(matches);
const name = loaderData?.name ?? null;
if (!name) {
return pageMeta({
origin,
title: `${HEAD_MESSAGES.meta.cinemaNotFoundTitle} — ${APP_TITLE}`,
description: HEAD_MESSAGES.meta.cinemaNotFoundTitle,
path: "/",
noIndex: true,
});
}
const path = `/cinema/${params?.slug ?? ""}`;
const location = loaderData?.locationLabel ?? "";
const description =
loaderData?.description ??
`${name}${location ? ` — ${location}` : ""} · ที่ตั้ง วิธีเดินทาง สิ่งอำนวยความสะดวก และช่องทางจองตั๋ว`;
const title = location
? `${name} — ${location} | AlterFlixHub`
: `${name} — AlterFlixHub`;
return [
...pageMeta({ origin, title, description, path }),
...breadcrumbJsonLd(origin, [
{ name: "หน้าแรก", path: "/" },
{ name, path },
]),
...cinemaJsonLd({
origin,
name,
description,
path,
image: loaderData?.photo ?? null,
coordinates: loaderData?.coordinates ?? null,
locationLabel: location,
mapUrl: loaderData?.mapUrl ?? null,
}),
];
}
export default function CinemaDetailRoute() {
const { messages, locale } = useI18n();
const { cinemas } = useContent();
const { status: nearMeStatus, distances } = useNearMe();
const { slug } = useParams();
const cinema = findCinemaBySlug(cinemas, slug);
// เก็บสถิติการเปิดดูหน้ารายละเอียด (ต้องอยู่ก่อน early return เพื่อให้ hook เรียกทุกครั้ง)
useEffect(() => {
if (!cinema) return;
trackEvent({ type: "cinema_view", slug: cinema.slug, locale });
}, [cinema, locale]);
if (!cinema) return <CinemaNotFound />;
const otherCinemas = listOtherCinemas(cinemas, cinema.slug);
const orderedOthers =
nearMeStatus === "ready"
? sortByDistance(otherCinemas, distances)
: otherCinemas;
return (
<SiteShell variant="detail">
<main className="relative z-10 mx-auto flex w-full max-w-[1440px] flex-col gap-10 px-5 pb-16 pt-4 md:px-[108px] md:pt-6">
<Reveal kind="fade">
<Link
to="/"
className="inline-flex w-fit items-center gap-2 text-xl font-medium transition hover:text-[#D47272]"
>
<IconArrowBackUp size={24} />
{messages.detail.back}
</Link>
</Reveal>
<Reveal kind="zoom">
<Gallery photos={cinema.photos} cinemaName={cinema.name} />
</Reveal>
<section className="grid gap-10 lg:grid-cols-[minmax(0,749px)_minmax(0,384px)] lg:justify-between">
<div className="flex flex-col gap-12">
<Reveal delay={60}>
<CinemaSummary cinema={cinema} />
</Reveal>
<Reveal delay={120}>
<TransportList transport={cinema.transport} />
</Reveal>
</div>
<Reveal delay={160}>
<ContactPanel cinema={cinema} />
</Reveal>
</section>
<Reveal>
<FacilitiesSection cinema={cinema} />
</Reveal>
<section className="flex flex-col gap-8">
<Reveal className="w-full">
<h2 className="text-2xl font-semibold leading-[1.3]">
{messages.detail.otherCinemas}
</h2>
</Reveal>
<CinemaGrid cinemas={orderedOthers} source="other_cinemas" />
</section>
{/* แชร์หน้ารายละเอียดโรงนี้ (ท้ายหน้า) */}
<Reveal kind="fade">
<section className="flex flex-wrap items-center justify-between gap-4 rounded-xl border border-[#332B2B] bg-[#1B1B1B]/70 p-5 backdrop-blur-sm">
<div className="flex flex-col gap-1">
<h2 className="text-lg font-semibold leading-[1.3]">
{messages.share.pageLabel}
</h2>
<p className="text-sm text-[#B3B3B3]">
{cinemaName(cinema, locale)}
</p>
</div>
<ShareButton
variant="wide"
slug={cinema.slug}
title={cinemaName(cinema, locale)}
/>
</section>
</Reveal>
</main>
</SiteShell>
);
}