"use client"; import Image from "next/image"; import { Photo } from "@/lib/types"; import { useState, useEffect, useCallback, useRef } from "react"; import { X, ChevronLeft, ChevronRight } from "lucide-react"; const PhotoGallery = ({ photos }: { photos: Photo[] }) => { const [selectedIndex, setSelectedIndex] = useState(null); const touchStart = useRef(null); const selected = selectedIndex !== null ? photos[selectedIndex] : null; const close = useCallback(() => setSelectedIndex(null), []); const prev = useCallback(() => { setSelectedIndex((i) => (i !== null && i > 0 ? i - 1 : i)); }, []); const next = useCallback(() => { setSelectedIndex((i) => i !== null && i < photos.length - 1 ? i + 1 : i ); }, [photos.length]); useEffect(() => { if (selectedIndex === null) return; document.body.style.overflow = "hidden"; const handleKey = (e: KeyboardEvent) => { if (e.key === "Escape") close(); if (e.key === "ArrowLeft") prev(); if (e.key === "ArrowRight") next(); }; window.addEventListener("keydown", handleKey); return () => { document.body.style.overflow = ""; window.removeEventListener("keydown", handleKey); }; }, [selectedIndex, close, prev, next]); const handleTouchStart = (e: React.TouchEvent) => { touchStart.current = e.touches[0].clientX; }; const handleTouchEnd = (e: React.TouchEvent) => { if (touchStart.current === null) return; const diff = e.changedTouches[0].clientX - touchStart.current; if (Math.abs(diff) > 50) { if (diff > 0) prev(); else next(); } touchStart.current = null; }; return ( <>
{photos.map((photo, i) => ( ))}
{selected && (
{selectedIndex! > 0 && ( )} {selectedIndex! < photos.length - 1 && ( )}
{selectedIndex! + 1} / {photos.length}
)} ); }; export default PhotoGallery;