'use client' import { useEffect, useEffectEvent, useLayoutEffect, useRef } from 'react' import { getAssetUrl } from '@/lib/utils' interface LightboxProps { isOpen: boolean onClose: () => void src: string alt: string type: 'image' | 'video' startTime?: number } export function Lightbox({ isOpen, onClose, src, alt, type, startTime }: LightboxProps) { const overlayRef = useRef(null) const mediaButtonRef = useRef(null) const videoRef = useRef(null) const closeLightbox = useEffectEvent(onClose) useEffect(() => { if (!isOpen) return const handleKeyDown = (event: KeyboardEvent) => { if (event.key === 'Escape') { closeLightbox() } } const handleClickOutside = (event: MouseEvent) => { if (overlayRef.current && event.target === overlayRef.current) { closeLightbox() } } const previousOverflow = document.body.style.overflow document.addEventListener('keydown', handleKeyDown) document.addEventListener('click', handleClickOutside) document.body.style.overflow = 'hidden' mediaButtonRef.current?.focus() return () => { document.removeEventListener('keydown', handleKeyDown) document.removeEventListener('click', handleClickOutside) document.body.style.overflow = previousOverflow } }, [isOpen]) useLayoutEffect(() => { if (isOpen && type === 'video' && videoRef.current && startTime != null && startTime > 0) { videoRef.current.currentTime = startTime } }, [isOpen, startTime, type]) if (!isOpen) return null return (
) }