diff --git a/surfsense_web/components/ui/hero-carousel.tsx b/surfsense_web/components/ui/hero-carousel.tsx index cfafd68f6..327970de2 100644 --- a/surfsense_web/components/ui/hero-carousel.tsx +++ b/surfsense_web/components/ui/hero-carousel.tsx @@ -152,13 +152,38 @@ function usePrefetchVideos() { }, []); } +const AUTOPLAY_MS = 6000; + function HeroCarousel() { const [activeIndex, setActiveIndex] = useState(0); const [isGifExpanded, setIsGifExpanded] = useState(false); + const [isHovered, setIsHovered] = useState(false); + const [isTabVisible, setIsTabVisible] = useState(true); const directionRef = useRef<"forward" | "backward">("forward"); usePrefetchVideos(); + const shouldAutoPlay = !isGifExpanded && !isHovered && isTabVisible; + + useEffect(() => { + if (!shouldAutoPlay) return; + + const id = setTimeout(() => { + directionRef.current = "forward"; + setActiveIndex((prev) => + prev >= carouselItems.length - 1 ? 0 : prev + 1 + ); + }, AUTOPLAY_MS); + + return () => clearTimeout(id); + }, [activeIndex, shouldAutoPlay]); + + useEffect(() => { + const handler = () => setIsTabVisible(!document.hidden); + document.addEventListener("visibilitychange", handler); + return () => document.removeEventListener("visibilitychange", handler); + }, []); + const goTo = useCallback( (newIndex: number) => { directionRef.current = newIndex >= activeIndex ? "forward" : "backward"; @@ -179,7 +204,11 @@ function HeroCarousel() { const isForward = directionRef.current === "forward"; return ( -