reboot alterflixhub
This commit is contained in:
commit
1e84023a44
188 files changed
+31629
No files matched your search
@@ -0,0 +1,144 @@
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useLoaderData } from "react-router";
|
||||
|
||||
import { HeroSection } from "@/components/home/HeroSection";
|
||||
import { CategoryFilter, CinemaGrid } from "@/components/site/CinemaGrid";
|
||||
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 { filterCinemas, CINEMA_CATEGORIES, DEFAULT_CATEGORY, publiclyVisible, type Cinema, type CinemaCategory } from "@/lib/cinemas";
|
||||
import { dailyOrder, dayKey } from "@/lib/daily-order";
|
||||
import { computePopularity } from "@/lib/popularity.server";
|
||||
import { useContent } from "@/lib/content-context";
|
||||
import { sortByDistance } from "@/lib/geo";
|
||||
import { useI18n } from "@/lib/i18n";
|
||||
import { DEFAULT_LOCALE, MESSAGES } from "@/lib/i18n/messages";
|
||||
|
||||
/** head (title/description) ไม่เปลี่ยนตามภาษา — meta() ไม่มี access ต่อ cookie */
|
||||
const HEAD_MESSAGES = MESSAGES[DEFAULT_LOCALE];
|
||||
|
||||
export function meta() {
|
||||
return [
|
||||
{ title: APP_TITLE },
|
||||
{ name: "description", content: HEAD_MESSAGES.meta.homeDescription },
|
||||
];
|
||||
}
|
||||
|
||||
/** จำนวนการ์ดที่แสดงก่อนกด "แสดงเพิ่ม" */
|
||||
const INITIAL_VISIBLE = 4;
|
||||
|
||||
/** อันดับยอดนิยม + คีย์วันที่สำหรับสลับลำดับการ์ด (memo 5 นาที) — ส่งมากับ loader */
|
||||
export async function loader() {
|
||||
return { popularity: await computePopularity(), orderKey: dayKey() };
|
||||
}
|
||||
|
||||
export default function HomeRoute() {
|
||||
const { messages, locale } = useI18n();
|
||||
const { popularity, orderKey } = useLoaderData<typeof loader>();
|
||||
const { cinemas } = useContent();
|
||||
const { status: nearMeStatus, distances, requestLocation } = useNearMe();
|
||||
const [category, setCategory] = useState<CinemaCategory>(DEFAULT_CATEGORY);
|
||||
const [query, setQuery] = useState("");
|
||||
const [showAll, setShowAll] = useState(false);
|
||||
|
||||
const matched = useMemo(() => {
|
||||
// ลำดับการ์ดสลับทุกวัน (ไม่ให้โรงใดอยู่ตำแหน่งเดิมสองวันติดกัน)
|
||||
const base = dailyOrder(publiclyVisible(cinemas), (cinema) => cinema.slug, orderKey);
|
||||
const byStats =
|
||||
category === "ยอดนิยม" && popularity.ready && query.trim() === "";
|
||||
if (!byStats) return filterCinemas(base, { category, query });
|
||||
|
||||
// ใช้เฉพาะโรงที่ยังไม่ถูกซ่อน และเรียงตามอันดับสถิติ
|
||||
const bySlug = new Map(base.map((cinema) => [cinema.slug, cinema]));
|
||||
return popularity.slugs
|
||||
.map((slug) => bySlug.get(slug))
|
||||
.filter((cinema): cinema is Cinema => cinema !== undefined);
|
||||
}, [cinemas, category, query, popularity, orderKey]);
|
||||
// เรียงจากใกล้สุดไปไกลสุดเมื่อทราบตำแหน่งแล้ว (ไม่กรองตามรัศมี — ไกลแค่ไหนก็ยังแสดง)
|
||||
const ordered =
|
||||
nearMeStatus === "ready" ? sortByDistance(matched, distances) : matched;
|
||||
const visibleCinemas = showAll ? ordered : ordered.slice(0, INITIAL_VISIBLE);
|
||||
const hasMore = ordered.length > visibleCinemas.length;
|
||||
|
||||
const keyword = query.trim();
|
||||
|
||||
// เก็บสถิติการค้นหา: ยิงครั้งเดียวหลังผู้ใช้หยุดพิมพ์ 900ms และมีคำค้นอย่างน้อย 2 ตัวอักษร
|
||||
useEffect(() => {
|
||||
if (keyword.length < 2) return;
|
||||
const timer = setTimeout(() => {
|
||||
trackEvent({
|
||||
type: "search",
|
||||
query: keyword,
|
||||
category,
|
||||
result_count: matched.length,
|
||||
locale,
|
||||
});
|
||||
}, 900);
|
||||
return () => clearTimeout(timer);
|
||||
}, [keyword, category, matched.length, locale]);
|
||||
|
||||
function handleRequestLocation() {
|
||||
requestLocation();
|
||||
trackEvent({
|
||||
type: "near_me",
|
||||
category,
|
||||
query: keyword || undefined,
|
||||
result_count: matched.length,
|
||||
locale,
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<SiteShell variant="home">
|
||||
<main className="relative z-10 mx-auto max-w-[1440px] px-5 pb-12 md:px-[108px] md:pb-16">
|
||||
<HeroSection
|
||||
query={query}
|
||||
onQueryChange={setQuery}
|
||||
nearMeStatus={nearMeStatus}
|
||||
onRequestLocation={handleRequestLocation}
|
||||
/>
|
||||
|
||||
<section className="mt-16 flex flex-col gap-6 md:mt-[65px]">
|
||||
<div className="flex min-h-[72px] items-center border-b border-[#1F1F1F] py-2">
|
||||
<h2 className="text-[28px] font-semibold leading-[1.3]">
|
||||
{messages.list.title}
|
||||
</h2>
|
||||
</div>
|
||||
|
||||
<CategoryFilter
|
||||
active={category}
|
||||
categories={CINEMA_CATEGORIES}
|
||||
onChange={setCategory}
|
||||
/>
|
||||
|
||||
{visibleCinemas.length === 0 ? (
|
||||
<p className="py-16 text-center text-sm text-[#B3B3B3]">
|
||||
{messages.list.empty}
|
||||
</p>
|
||||
) : (
|
||||
<div className="mt-2">
|
||||
<CinemaGrid
|
||||
cinemas={visibleCinemas}
|
||||
source={keyword ? "search" : "category"}
|
||||
searchQuery={keyword || undefined}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{hasMore ? (
|
||||
<div className="flex justify-center pt-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowAll(true)}
|
||||
className="h-10 w-[200px] rounded-lg border border-[#8F0808] bg-[#8F0808] transition hover:bg-[#a30a0a]"
|
||||
>
|
||||
{messages.list.showMore}
|
||||
</button>
|
||||
</div>
|
||||
) : null}
|
||||
</section>
|
||||
</main>
|
||||
</SiteShell>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user