import { useQuery } from "@tanstack/react-query";
import { useState, useEffect, useCallback } from "react";
import { ChevronLeft, ChevronRight, X, Images, Maximize2 } from "lucide-react";
interface GalleryImage {
folder: string;
fileName: string;
thumb: string;
large: string;
}
function Lightbox({
images,
startIndex,
onClose,
}: {
images: GalleryImage[];
startIndex: number;
onClose: () => void;
}) {
const [index, setIndex] = useState(startIndex);
const prev = useCallback(() => setIndex((i) => (i - 1 + images.length) % images.length), [images.length]);
const next = useCallback(() => setIndex((i) => (i + 1) % images.length), [images.length]);
useEffect(() => {
const handler = (e: KeyboardEvent) => {
if (e.key === "Escape") onClose();
if (e.key === "ArrowLeft") prev();
if (e.key === "ArrowRight") next();
};
window.addEventListener("keydown", handler);
return () => window.removeEventListener("keydown", handler);
}, [onClose, prev, next]);
return (
e.stopPropagation()}>
{index + 1} / {images.length}
);
}
function SingleImageCarousel({
images,
autoPlay = true,
interval = 5000,
onExpand,
}: {
images: GalleryImage[];
autoPlay?: boolean;
interval?: number;
onExpand?: (index: number) => void;
}) {
const [index, setIndex] = useState(0);
const [paused, setPaused] = useState(false);
const prev = useCallback(() => setIndex((i) => (i - 1 + images.length) % images.length), [images.length]);
const next = useCallback(() => setIndex((i) => (i + 1) % images.length), [images.length]);
useEffect(() => {
if (!autoPlay || paused || images.length <= 1) return;
const timer = setInterval(next, interval);
return () => clearInterval(timer);
}, [autoPlay, paused, next, interval, images.length]);
if (images.length === 0) return null;
return (
setPaused(true)}
onMouseLeave={() => setPaused(false)}
>
{onExpand && (
)}
{images.slice(0, Math.min(images.length, 20)).map((_, i) => (
);
}
export function PhotoGalleryWidget() {
const { data: images, isLoading } = useQuery({
queryKey: ["/api/gallery"],
});
const [lightboxIndex, setLightboxIndex] = useState(null);
if (isLoading) {
return (
);
}
if (!images || images.length === 0) return null;
return (
<>
Fotogalerie
setLightboxIndex(i)}
/>
{lightboxIndex !== null && (
setLightboxIndex(null)}
/>
)}
>
);
}
export default function GalleryPage() {
const { data: images, isLoading } = useQuery({
queryKey: ["/api/gallery"],
});
const [lightboxIndex, setLightboxIndex] = useState(null);
return (
{isLoading ? (
) : images && images.length > 0 ? (
setLightboxIndex(i)}
/>
{images.length} Fotos
) : (
Keine Fotos vorhanden
)}
{lightboxIndex !== null && images && (
setLightboxIndex(null)}
/>
)}
);
}