Files
tvone/app/components/tvone-promo-strip.tsx
T

165 lines
5.3 KiB
TypeScript
Raw Normal View History

2026-03-28 23:01:17 +01:00
"use client";
2026-03-27 22:56:54 +01:00
import Image from "next/image";
2026-03-28 23:10:16 +01:00
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import type { CSSProperties } from "react";
2026-03-27 22:56:54 +01:00
2026-04-06 16:16:15 +01:00
// Definição do tipo para garantir 100% de compatibilidade TS
export interface SliderPhoto {
src: string;
alt: string;
id?: string | number;
}
2026-03-27 22:56:54 +01:00
2026-03-28 23:01:17 +01:00
const ROTATE_MS = 5500;
2026-04-06 16:16:15 +01:00
// --- 1. COMPONENTE DE SLIDE INDIVIDUAL ---
interface SlideProps {
photo: SliderPhoto;
onRatioLoad: (src: string, ratio: number) => void;
}
2026-03-27 23:10:26 +01:00
2026-04-06 16:16:15 +01:00
function PromoStripSingleSlide({ photo, onRatioLoad }: SlideProps) {
2026-03-27 23:10:26 +01:00
return (
2026-04-06 16:16:15 +01:00
<div className="relative h-full w-full">
<Image
src={photo.src}
alt={photo.alt}
fill
priority
className="object-cover object-center"
sizes="100vw"
onLoad={(e) => {
const target = e.currentTarget;
if (target.naturalHeight > 0) {
onRatioLoad(photo.src, target.naturalWidth / target.naturalHeight);
}
}}
/>
{/* Label Interna - Estilo Premium/iOS */}
<div className="absolute bottom-6 left-6 z-10 md:bottom-10 md:left-10">
<span className="rounded-full bg-black/40 backdrop-blur-xl px-4 py-2 text-[10px] font-bold uppercase tracking-[0.2em] text-white border border-white/10 shadow-2xl">
Destaque
</span>
</div>
2026-03-28 23:01:17 +01:00
</div>
2026-03-27 23:10:26 +01:00
);
}
2026-04-06 16:16:15 +01:00
// --- 2. HOOK DE DADOS (Simulação de Fetch) ---
2026-03-28 23:01:17 +01:00
function useSliderPhotos(): { photos: SliderPhoto[]; loading: boolean } {
const [photos, setPhotos] = useState<SliderPhoto[]>([]);
2026-04-06 16:16:15 +01:00
const [loading, setLoading] = useState<boolean>(true);
2026-03-28 23:01:17 +01:00
useEffect(() => {
let cancelled = false;
fetch("/api/slider-photos", { cache: "no-store" })
.then((r) => (r.ok ? r.json() : []))
.then((data: unknown) => {
if (cancelled) return;
if (Array.isArray(data)) {
2026-04-06 16:16:15 +01:00
const validated = data.filter(
(x): x is SliderPhoto =>
typeof x === "object" && x !== null && "src" in x && "alt" in x
2026-03-28 23:01:17 +01:00
);
2026-04-06 16:16:15 +01:00
setPhotos(validated);
2026-03-28 23:01:17 +01:00
}
})
.catch(() => {
if (!cancelled) setPhotos([]);
})
.finally(() => {
if (!cancelled) setLoading(false);
});
2026-04-06 16:16:15 +01:00
return () => { cancelled = true; };
2026-03-28 23:01:17 +01:00
}, []);
return { photos, loading };
2026-03-27 23:10:26 +01:00
}
2026-04-06 16:16:15 +01:00
// --- 3. COMPONENTE PRINCIPAL (O CARROSSEL) ---
2026-03-27 22:56:54 +01:00
export function TvonePromoStrip() {
2026-03-28 23:01:17 +01:00
const { photos, loading } = useSliderPhotos();
2026-04-06 16:16:15 +01:00
const [index, setIndex] = useState<number>(0);
const [ratios, setRatios] = useState<Record<string, number>>({});
const [reduceMotion, setReduceMotion] = useState<boolean>(false);
2026-03-27 22:56:54 +01:00
2026-04-06 16:16:15 +01:00
// Detetar preferência de movimento do sistema (iOS/MacOS/Windows)
2026-03-28 23:01:17 +01:00
useEffect(() => {
const mq = window.matchMedia("(prefers-reduced-motion: reduce)");
const sync = () => setReduceMotion(mq.matches);
sync();
mq.addEventListener("change", sync);
return () => mq.removeEventListener("change", sync);
}, []);
2026-04-06 16:16:15 +01:00
// Lógica de avanço automático
2026-03-28 23:01:17 +01:00
const advance = useCallback(() => {
if (photos.length <= 1) return;
2026-04-06 16:16:15 +01:00
setIndex((i: number) => (i + 1) % photos.length);
2026-03-28 23:01:17 +01:00
}, [photos.length]);
useEffect(() => {
if (photos.length <= 1 || reduceMotion) return;
const id = window.setInterval(advance, ROTATE_MS);
return () => window.clearInterval(id);
}, [photos.length, reduceMotion, advance]);
2026-04-06 16:16:15 +01:00
// Handler para atualizar rácios de imagem
const handleRatio = useCallback((src: string, ratio: number) => {
setRatios((prev) => (prev[src] === ratio ? prev : { ...prev, [src]: ratio }));
}, []);
// Calcula a proporção do contentor baseado no slide ativo
const currentRatio = useMemo<number>(() => {
const activePhoto = photos[index];
return activePhoto ? (ratios[activePhoto.src] || 16 / 9) : 16 / 9;
}, [photos, index, ratios]);
if (loading && photos.length === 0) {
return <div className="aspect-[16/9] w-full animate-pulse bg-neutral-100 dark:bg-neutral-800 rounded-3xl" />;
}
2026-03-28 23:01:17 +01:00
2026-04-06 16:16:15 +01:00
if (photos.length === 0) return null;
2026-03-28 23:01:17 +01:00
return (
2026-04-06 16:16:15 +01:00
<section
className="relative w-full overflow-hidden bg-neutral-100 dark:bg-neutral-900 transition-[aspect-ratio] duration-700 ease-[cubic-bezier(0.32,0.72,0,1)]"
style={{ aspectRatio: currentRatio } as CSSProperties}
2026-03-28 23:01:17 +01:00
role="region"
aria-roledescription="carrossel"
>
2026-04-06 16:16:15 +01:00
{/* O "ROLO" (A fita flexível) */}
<div
className="flex h-full w-full transition-transform duration-1000 ease-[cubic-bezier(0.32,0.72,0,1)]"
style={{ transform: `translateX(-${index * 100}%)` }}
>
{photos.map((photo: SliderPhoto, i: number) => (
<div key={photo.src || i} className="h-full w-full flex-shrink-0">
<PromoStripSingleSlide
photo={photo}
onRatioLoad={handleRatio}
/>
</div>
))}
2026-03-27 22:56:54 +01:00
</div>
2026-04-06 16:16:15 +01:00
{/* INDICADORES (iOS Dot Style) */}
{photos.length > 1 && (
<div className="absolute bottom-6 left-1/2 flex -translate-x-1/2 gap-2 z-20">
{photos.map((_, i: number) => (
<button
key={i}
onClick={() => setIndex(i)}
className={`h-1.5 rounded-full transition-all duration-500 ${
index === i ? "w-8 bg-white" : "w-1.5 bg-white/30 hover:bg-white/60"
}`}
aria-label={`Ir para slide ${i + 1}`}
/>
))}
</div>
)}
</section>
2026-03-27 22:56:54 +01:00
);
2026-04-06 16:16:15 +01:00
}