สาเหตุ: photos[] อ้างลิงก์ hotlink (googleusercontent/gstatic/fbcdn ฯลฯ) ซึ่งตอบ 200 แต่ 0 ไบต์เมื่อลิงก์หมดอายุ/ถูกจำกัด rate → รูปแตกเงียบ ๆ เช่น /cinema/phahol-theatre - scripts/mirror-photos.mjs: ดาวน์โหลด 20 รูปจาก 12 โรง มาเก็บ public/images/cinemas แล้วแก้ content.json ชี้ path ในโปรเจกต์ (สำรอง .bak-photos-* ก่อนเขียน) → เหลือรูปภายนอก 0 - บีบอัดรูปใหญ่ (<=1600px, q82): 6.12 MB → 2.91 MB - PhotoImage: img + onError fallback เป็นโลโก้ (ใช้ใน CinemaCard/Gallery) กันรูปแตกทุกกรณี - scripts/audit-photos.mjs: ตรวจรูปเสีย exit 1 - DESIGN.md: กฎรูปภาพ (ห้าม hotlink, ต้องใช้ PhotoImage, เพดานขนาด)
110 lines
4.4 KiB
TypeScript
110 lines
4.4 KiB
TypeScript
import { IconChevronLeft, IconChevronRight } from "@tabler/icons-react";
|
|
import { useState, type ReactNode } from "react";
|
|
|
|
import { Logo } from "@/components/site/Logo";
|
|
import { PhotoImage } from "@/components/site/PhotoImage";
|
|
import { rotateGallery, splitGallery } from "@/lib/gallery";
|
|
import { useI18n } from "@/lib/i18n";
|
|
|
|
type GalleryProps = {
|
|
photos: string[];
|
|
cinemaName: string;
|
|
};
|
|
|
|
type GallerySlotProps = {
|
|
photo: string | null;
|
|
alt: string;
|
|
/** เนื้อหาที่แสดงเมื่อยังไม่มีรูป — ใช้โลโก้ AlterFlixHub แทนข้อความ */
|
|
placeholder: ReactNode;
|
|
className: string;
|
|
};
|
|
|
|
function GallerySlot({ photo, alt, placeholder, className }: GallerySlotProps) {
|
|
return (
|
|
<PhotoImage
|
|
src={photo}
|
|
alt={alt}
|
|
size="sm"
|
|
className={className}
|
|
fallback={placeholder}
|
|
/>
|
|
);
|
|
}
|
|
|
|
/** แกลเลอรีภาพโรงหนัง — ปุ่มลูกศรเลื่อนดูภาพทั้งหมดโดยภาพหลักและภาพย่อยเลื่อนตามกัน */
|
|
export function Gallery({ photos, cinemaName }: GalleryProps) {
|
|
const { messages } = useI18n();
|
|
const [startIndex, setStartIndex] = useState(0);
|
|
const total = photos.length;
|
|
// หมุนรายการตามตำแหน่งที่เลือก แล้วแบ่งเป็นภาพหลัก + ภาพย่อยตามเดิม
|
|
const { main, thumbs, remaining } = splitGallery(
|
|
rotateGallery(photos, startIndex),
|
|
);
|
|
const [firstThumb = null, secondThumb = null] = thumbs;
|
|
const canNavigate = total > 1;
|
|
|
|
function step(direction: 1 | -1) {
|
|
setStartIndex((current) => current + direction);
|
|
}
|
|
|
|
return (
|
|
<section className="grid gap-6 lg:grid-cols-[minmax(0,807px)_minmax(0,387px)]">
|
|
<div className="relative overflow-hidden rounded-xl">
|
|
<GallerySlot
|
|
key={`main-${main ?? "empty"}`}
|
|
photo={main}
|
|
alt={`${messages.gallery.main} — ${cinemaName}`}
|
|
placeholder={<Logo size="md" />}
|
|
className="fx-swap aspect-[269/202] w-full lg:aspect-auto lg:h-[606px]"
|
|
/>
|
|
{canNavigate ? (
|
|
<>
|
|
<button
|
|
type="button"
|
|
onClick={() => step(-1)}
|
|
aria-label={messages.gallery.previous}
|
|
className="absolute left-6 top-1/2 flex size-10 -translate-y-1/2 items-center justify-center rounded-full bg-black/50 text-white transition after:absolute after:-inset-1 after:content-[''] hover:bg-black/75 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#A92626] active:scale-95"
|
|
>
|
|
<IconChevronLeft size={24} />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => step(1)}
|
|
aria-label={messages.gallery.next}
|
|
className="absolute right-6 top-1/2 flex size-10 -translate-y-1/2 items-center justify-center rounded-full bg-black/50 text-white transition after:absolute after:-inset-1 after:content-[''] hover:bg-black/75 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#A92626] active:scale-95"
|
|
>
|
|
<IconChevronRight size={24} />
|
|
</button>
|
|
</>
|
|
) : null}
|
|
</div>
|
|
|
|
<div className="grid grid-cols-2 gap-6 lg:grid-cols-1 lg:grid-rows-2">
|
|
<div className="overflow-hidden rounded-xl">
|
|
<GallerySlot
|
|
key={`thumb-1-${firstThumb ?? "empty"}`}
|
|
photo={firstThumb}
|
|
alt={`${messages.gallery.second} — ${cinemaName}`}
|
|
placeholder={<Logo size="sm" />}
|
|
className="fx-swap aspect-[387/290] w-full"
|
|
/>
|
|
</div>
|
|
<div className="relative overflow-hidden rounded-xl">
|
|
<GallerySlot
|
|
key={`thumb-2-${secondThumb ?? "empty"}`}
|
|
photo={secondThumb}
|
|
alt={`${messages.gallery.third} — ${cinemaName}`}
|
|
placeholder={<Logo size="sm" />}
|
|
className="fx-swap aspect-[387/290] w-full"
|
|
/>
|
|
{remaining > 0 ? (
|
|
<div className="absolute bottom-2.5 right-2.5 flex size-[60px] items-center justify-center rounded-lg bg-black/80 text-lg font-medium">
|
|
{messages.gallery.more(remaining)}
|
|
</div>
|
|
) : null}
|
|
</div>
|
|
</div>
|
|
</section>
|
|
);
|
|
}
|