"use client"; import { useEffect, useRef } from "react"; import Image from "next/image"; import { ChevronLeft, ChevronRight, X } from "lucide-react"; import type { PrivateAlbum } from "@/data/dto/private-room"; import styles from "../private-room-screen.module.css"; export interface PrivateAlbumGalleryProps { album: PrivateAlbum; imageIndex: number; onClose: () => void; onImageIndexChange: (index: number) => void; } export function PrivateAlbumGallery({ album, imageIndex, onClose, onImageIndexChange, }: PrivateAlbumGalleryProps) { const touchStartXRef = useRef(null); const currentImage = album.images[imageIndex]; const hasPrevious = imageIndex > 0; const hasNext = imageIndex < album.images.length - 1; useEffect(() => { const handleKeyDown = (event: KeyboardEvent) => { if (event.key === "Escape") onClose(); if (event.key === "ArrowLeft" && hasPrevious) { onImageIndexChange(imageIndex - 1); } if (event.key === "ArrowRight" && hasNext) { onImageIndexChange(imageIndex + 1); } }; document.addEventListener("keydown", handleKeyDown); return () => document.removeEventListener("keydown", handleKeyDown); }, [hasNext, hasPrevious, imageIndex, onClose, onImageIndexChange]); if (!currentImage?.url) return null; const handleTouchEnd = (event: React.TouchEvent) => { const touchStartX = touchStartXRef.current; touchStartXRef.current = null; if (touchStartX === null) return; const delta = event.changedTouches[0]?.clientX ?? touchStartX; const distance = delta - touchStartX; if (distance > 48 && hasPrevious) onImageIndexChange(imageIndex - 1); if (distance < -48 && hasNext) onImageIndexChange(imageIndex + 1); }; return (
{ touchStartXRef.current = event.touches[0]?.clientX ?? null; }} onTouchEnd={handleTouchEnd} >
{`${album.title
{imageIndex + 1} / {album.images.length} {hasPrevious ? ( ) : null} {hasNext ? ( ) : null}
); }