'use client' import { AnimatePresence, motion } from 'motion/react' import { useEffect, useState } from 'react' import { Button as ButtonRAC } from 'react-aria-components' import { useIntl } from 'react-intl' import { IconButton } from '../../IconButton' import { MaterialIcon } from '../../Icons/MaterialIcon' import { Typography } from '../../Typography' import Image from '../../Image' import { LightboxImage } from '..' import styles from './gallery.module.css' type GalleryProps = { images: LightboxImage[] onClose: () => void onSelectImage: (image: LightboxImage) => void onImageClick: () => void selectedImage: LightboxImage | null hideLabel?: boolean } export default function Gallery({ images, onClose, onSelectImage, onImageClick, selectedImage, hideLabel, }: GalleryProps) { const intl = useIntl() const [animateLeft, setAnimateLeft] = useState(true) const mainImage = selectedImage || images[0] const mainImageIndex = images.findIndex((img) => img === mainImage) function getThumbImages() { const thumbs = [] for (let i = 1; i <= Math.min(5, images.length); i++) { const index = (mainImageIndex + i) % images.length thumbs.push(images[index]) } return thumbs } function handleNext() { setAnimateLeft(true) const nextIndex = (mainImageIndex + 1) % images.length onSelectImage(images[nextIndex]) } function handlePrev() { setAnimateLeft(false) const prevIndex = (mainImageIndex - 1 + images.length) % images.length onSelectImage(images[prevIndex]) } const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'ArrowLeft') { handlePrev() } else if (e.key === 'ArrowRight') { handleNext() } } useEffect(() => { window.addEventListener('keydown', handleKeyDown) return () => { window.removeEventListener('keydown', handleKeyDown) } }) const variants = { initial: (animateLeft: boolean) => ({ opacity: 0, x: animateLeft ? 300 : -300, }), animate: { opacity: 1, x: 0 }, exit: (animateLeft: boolean) => ({ opacity: 0, x: animateLeft ? -300 : 300, }), } as const return (
{mainImage.caption && !hideLabel && ( {mainImage.caption} )}