'use client' import { CampaignContentCard, type CampaignContentCardData } from '@/components/CampaignContentCard' import { useCallback, useEffect, useRef, useState, type KeyboardEvent } from 'react' const PROGRAMMATIC_SCROLL_LOCK_MS = 700 type CampaignContentCarouselProps = { ariaLabel: string items: CampaignContentCardData[] } /** * Mobile-only carousel for the campaign home content section: one card per * screen with snap scrolling, page dots and a "N de M" readout. Deliberately * not a variant of `CampaignCarousel` — that module is pinned to the Penpot * auto-advancing geometry of the problem/flags sections. */ export const CampaignContentCarousel = ({ ariaLabel, items }: CampaignContentCarouselProps) => { const trackRef = useRef(null) const isProgrammaticScrollRef = useRef(false) const [activeIndex, setActiveIndex] = useState(0) const releaseProgrammaticScroll = useCallback(() => { isProgrammaticScrollRef.current = false }, []) useEffect(() => releaseProgrammaticScroll, [releaseProgrammaticScroll]) const scrollToItem = useCallback( (index: number) => { const track = trackRef.current const target = track?.querySelector(`[data-carousel-index="${index}"]`) if (!track || !target) return isProgrammaticScrollRef.current = true setActiveIndex(index) track.scrollTo({ left: target.offsetLeft - track.offsetLeft }) window.setTimeout(releaseProgrammaticScroll, PROGRAMMATIC_SCROLL_LOCK_MS) }, [releaseProgrammaticScroll], ) const handleKeyDown = (event: KeyboardEvent) => { if (event.key !== 'ArrowRight' && event.key !== 'ArrowLeft') return event.preventDefault() const next = activeIndex + (event.key === 'ArrowRight' ? 1 : -1) scrollToItem(Math.max(0, Math.min(items.length - 1, next))) } const syncActiveItem = useCallback(() => { const track = trackRef.current if (!track || isProgrammaticScrollRef.current) return const cards = Array.from(track.querySelectorAll('[data-carousel-index]')) let nearestIndex = activeIndex let nearestDistance = Number.POSITIVE_INFINITY cards.forEach((card, index) => { const distance = Math.abs(card.offsetLeft - track.offsetLeft - track.scrollLeft) if (distance < nearestDistance) { nearestDistance = distance nearestIndex = index } }) if (nearestIndex !== activeIndex) setActiveIndex(nearestIndex) }, [activeIndex]) return (
    1 ? 0 : undefined} data-carousel-track className="scrollbar-hide m-0 flex list-none snap-x snap-mandatory overflow-x-auto scroll-smooth p-0 motion-reduce:scroll-auto focus-visible:outline-2 focus-visible:outline-offset-4 focus-visible:outline-current" > {items.map((item, index) => (
  1. ))}
{items.length > 1 ? ( <>
{items.map((item, index) => (

{activeIndex + 1} de {items.length} · deslize para ver os próximos

) : null}
) }