import Image from "next/image"; import { useEffect, useMemo, useRef, useState } from "react"; type ImageSlide = { type: "image"; id: string; src: string; alt: string; }; type StatItem = { headline: string; description: string; }; type StatsSlide = { type: "stats"; id: string; title: string; items: StatItem[]; }; export type HeroSlide = ImageSlide | StatsSlide; type HeroSliderProps = { slides: HeroSlide[]; autoAdvanceMs?: number; }; export default function HeroSlider({ slides, autoAdvanceMs = 7000, }: HeroSliderProps) { const [activeIndex, setActiveIndex] = useState(0); const [isPaused, setIsPaused] = useState(false); const touchStartX = useRef(null); const touchDelta = useRef(0); const maxIndex = slides.length - 1; const goTo = (index: number) => { if (index < 0) return setActiveIndex(maxIndex); if (index > maxIndex) return setActiveIndex(0); return setActiveIndex(index); }; const next = () => goTo(activeIndex + 1); const prev = () => goTo(activeIndex - 1); const currentSlide = useMemo(() => slides[activeIndex], [slides, activeIndex]); useEffect(() => { if (isPaused || slides.length <= 1) return undefined; const timer = window.setInterval(() => { setActiveIndex((prevIndex) => (prevIndex + 1) % slides.length); }, autoAdvanceMs); return () => window.clearInterval(timer); }, [autoAdvanceMs, isPaused, slides.length]); const handleTouchStart = (event: React.TouchEvent) => { touchStartX.current = event.touches[0]?.clientX ?? null; touchDelta.current = 0; }; const handleTouchMove = (event: React.TouchEvent) => { if (touchStartX.current === null) return; touchDelta.current = (event.touches[0]?.clientX ?? touchStartX.current) - touchStartX.current; }; const handleTouchEnd = () => { const delta = touchDelta.current; touchStartX.current = null; touchDelta.current = 0; if (Math.abs(delta) < 40) return; if (delta > 0) prev(); if (delta < 0) next(); }; return (
setIsPaused(true)} onMouseLeave={() => setIsPaused(false)} onTouchStart={handleTouchStart} onTouchMove={handleTouchMove} onTouchEnd={handleTouchEnd} >
{currentSlide.type === "image" ? ( {currentSlide.alt} ) : (

{currentSlide.title}

{currentSlide.items.map((item) => (

{item.headline}

{item.description}

))}
)}
{slides.map((slide, index) => (
{slides.length > 1 && ( <> )}
); }