import React, { useState, useEffect } from 'react';
import { Dialog, DialogContent, DialogTrigger, DialogTitle, DialogDescription } from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
import { ChevronLeft, ChevronRight, X } from 'lucide-react';
interface ImageLightboxProps {
images: Array<{
id: number;
image_path: string;
image_desc?: string;
}>;
initialIndex?: number;
}
export function ImageLightbox({ images, initialIndex = 0 }: ImageLightboxProps) {
const [open, setOpen] = useState(false);
const [currentIndex, setCurrentIndex] = useState(initialIndex);
useEffect(() => {
setCurrentIndex(initialIndex);
}, [initialIndex]);
useEffect(() => {
if (open) {
setCurrentIndex(initialIndex);
}
}, [open, initialIndex]);
const handlePrevious = (e: React.MouseEvent) => {
e.stopPropagation();
setCurrentIndex((prev) => (prev === 0 ? images.length - 1 : prev - 1));
};
const handleNext = (e: React.MouseEvent) => {
e.stopPropagation();
setCurrentIndex((prev) => (prev === images.length - 1 ? 0 : prev + 1));
};
// Handle keyboard navigation
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (!open) return;
if (e.key === 'ArrowLeft') {
setCurrentIndex((prev) => (prev === 0 ? images.length - 1 : prev - 1));
} else if (e.key === 'ArrowRight') {
setCurrentIndex((prev) => (prev === images.length - 1 ? 0 : prev + 1));
} else if (e.key === 'Escape') {
setOpen(false);
}
};
window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, [open, images.length]);
if (!images.length) return null;
const currentImage = images[currentIndex];
return (
);
}
export function SingleImageLightbox({ image }: { image: { image_path: string; image_desc?: string } }) {
const [open, setOpen] = useState(false);
return (
);
}