"use client"; import { useEffect, useRef, useState } from "react"; import { ChevronLeft, ChevronRight } from "lucide-react"; import { Placeholder } from "@/components/Placeholder"; import { SlotLink } from "@/components/SlotLink"; import type { SlotMediaItem } from "@/lib/static-images"; const SLIDE_W = "min(1280px, calc(100vw - 5rem))"; const GAP = "1rem"; const INTERVAL_MS = 6000; const TRANSITION_MS = 700; export function Hero({ slides }: { slides: SlotMediaItem[]; aspectRatio?: string }) { const [active, setActive] = useState(0); const [shift, setShift] = useState(0); const [animating, setAnimating] = useState(false); const [paused, setPaused] = useState(false); const lock = useRef(false); const shiftRef = useRef(0); const countRef = useRef(slides.length); countRef.current = slides.length; const finishTransition = () => { if (!lock.current) return; lock.current = false; const delta = shiftRef.current; const total = countRef.current; shiftRef.current = 0; setActive((current) => (current + delta + total) % total); setShift(0); setAnimating(false); }; const go = (direction: -1 | 1) => { if (countRef.current < 2 || lock.current) return; lock.current = true; shiftRef.current = direction; setAnimating(true); setShift(direction); window.setTimeout(() => { if (lock.current && shiftRef.current === direction) finishTransition(); }, TRANSITION_MS + 80); }; useEffect(() => { setActive(0); setShift(0); shiftRef.current = 0; setAnimating(false); lock.current = false; }, [slides.length]); useEffect(() => { if (slides.length < 2 || paused || animating) return; const timer = window.setInterval(() => go(1), INTERVAL_MS); return () => window.clearInterval(timer); }, [paused, animating, slides.length, active]); if (slides.length === 0) return null; const count = slides.length; const offsets = count === 1 ? ([0] as number[]) : shift > 0 ? ([-1, 0, 1, 2] as number[]) : shift < 0 ? ([-2, -1, 0, 1] as number[]) : ([-1, 0, 1] as number[]); return (
setPaused(true)} onMouseLeave={() => setPaused(false)} >
{ if (event.target !== event.currentTarget) return; if (event.propertyName !== "transform") return; finishTransition(); }} > {offsets.map((offset) => { const index = (active + offset + count * 10) % count; const item = slides[index]; const isActiveSlot = offset === shift; return (
{isActiveSlot ? (

{item.title || "فروشگاه برادران برقعی"}

{item.kicker ? (

{item.kicker}

) : null}
مشاهده قطعات
) : ( )}
); })}
{count > 1 ? (
) : null}
{count > 1 ? (
{slides.map((item, i) => (
) : null}
); }