import { Dialog, DialogPanel } from "@headlessui/react"; import { ArrowLeftIcon, ArrowRightIcon } from "@heroicons/react/24/solid"; import { memo, useEffect, useMemo, useState } from "react"; import { useHotkeys } from "react-hotkeys-hook"; import { Spinner } from "@/components/Shared/UI"; import cn from "@/helpers/cn"; interface LightBoxProps { show: boolean; onClose: () => void; images: string[]; initialIndex?: number; } const LightBox = ({ show, onClose, images, initialIndex = 0 }: LightBoxProps) => { const [currentIndex, setCurrentIndex] = useState(initialIndex); const [isLoading, setIsLoading] = useState(true); const currentImage = useMemo( () => images[currentIndex], [images, currentIndex] ); useEffect(() => { if (show) { setCurrentIndex(initialIndex); setIsLoading(true); } }, [show, initialIndex]); const handleNext = () => { setCurrentIndex((prev) => { const next = Math.min(prev + 1, images.length - 1); if (next !== prev) setIsLoading(true); return next; }); }; const handlePrevious = () => { setCurrentIndex((prev) => { const prevIndex = Math.max(prev - 1, 0); if (prevIndex !== prev) setIsLoading(true); return prevIndex; }); }; useHotkeys("escape", onClose, { enabled: show }); useHotkeys("arrowright", handleNext, { enabled: show }); useHotkeys("arrowleft", handlePrevious, { enabled: show }); return ( ); }; export default memo(LightBox);