import { cn } from '@/lib/utils' import { useScreenSize } from '@/providers/ScreenSizeProvider' import { TImageInfo } from '@/types' import { ReactNode, useState } from 'react' import { createPortal } from 'react-dom' import Lightbox from 'yet-another-react-lightbox' import Zoom from 'yet-another-react-lightbox/plugins/zoom' import Image from '../Image' import NsfwOverlay from '../NsfwOverlay' export default function ImageGallery({ className, images, isNsfw = false, size = 'normal' }: { className?: string images: TImageInfo[] isNsfw?: boolean size?: 'normal' | 'small' }) { const { isSmallScreen } = useScreenSize() const [index, setIndex] = useState(-1) const handlePhotoClick = (event: React.MouseEvent, current: number) => { event.stopPropagation() event.preventDefault() setIndex(current) } let imageContent: ReactNode | null = null if (images.length === 1) { imageContent = ( handlePhotoClick(e, 0)} /> ) } else if (size === 'small') { imageContent = (
{images.map((image, i) => ( handlePhotoClick(e, i)} /> ))}
) } else if (isSmallScreen && (images.length === 2 || images.length === 4)) { imageContent = (
{images.map((image, i) => ( handlePhotoClick(e, i)} /> ))}
) } else { imageContent = (
{images.map((image, i) => ( handlePhotoClick(e, i)} /> ))}
) } return (
{imageContent} {index >= 0 && createPortal(
e.stopPropagation()}> ({ src: url }))} plugins={[Zoom]} open={index >= 0} close={() => setIndex(-1)} controller={{ closeOnBackdropClick: true, closeOnPullUp: true, closeOnPullDown: true }} styles={{ toolbar: { paddingTop: '2.25rem' } }} />
, document.body )} {isNsfw && }
) }