'use client'; import { ImageMeta } from '@/interfaces/image'; import styles from '@/styles/Gallery.module.scss'; import Image from 'next/image'; import { useCallback, useEffect, useState } from 'react'; interface GalleryProps { initialImages: ImageMeta[]; } const HORIZONTAL_ASPECT_RATIO = 1.8; const VERTICAL_ASPECT_RATIO = 0.8; export default function Gallery({ initialImages, }: GalleryProps) { const [fullScreenImageId, setFullScreenImageIdState] = useState< string | null >(null); const setFullScreenImage = useCallback((imageId: string | null) => { setFullScreenImageIdState(imageId); const url = new URL(window.location.href); if (imageId) { url.searchParams.set('image', imageId); } else { url.searchParams.delete('image'); } window.history.replaceState( window.history.state, '', `${url.pathname}${url.search}${url.hash}`, ); }, []); useEffect(() => { const synchronizeWithUrl = () => { const params = new URLSearchParams(window.location.search); setFullScreenImageIdState(params.get('image')); }; synchronizeWithUrl(); window.addEventListener('popstate', synchronizeWithUrl); return () => { window.removeEventListener('popstate', synchronizeWithUrl); }; }, []); useEffect(() => { if (!fullScreenImageId) { return; } const previousOverflow = document.body.style.overflow; const handleKeyDown = (event: KeyboardEvent) => { if (event.key === 'Escape') { setFullScreenImage(null); } }; document.body.style.overflow = 'hidden'; window.addEventListener('keydown', handleKeyDown); return () => { document.body.style.overflow = previousOverflow; window.removeEventListener('keydown', handleKeyDown); }; }, [fullScreenImageId, setFullScreenImage]); const currentImageIndex = fullScreenImageId ? initialImages.findIndex( (image) => image.id === fullScreenImageId, ) : -1; const previousImage = currentImageIndex > 0 ? initialImages[currentImageIndex - 1] : null; const nextImage = currentImageIndex >= 0 && currentImageIndex < initialImages.length - 1 ? initialImages[currentImageIndex + 1] : null; return (