"use client"; import { Link } from "@/i18n/routing"; import { getHeroData, listCarousel } from "@/service/landing/landing"; import { formatDateToIndonesian, textEllipsis } from "@/utils/globals"; import { useEffect, useState } from "react"; import { Icon } from "../ui/icon"; export default function NewsTicker() { const [article, setArticle] = useState([]); const [currentNewsIndex, setCurrentNewsIndex] = useState(0); const [animate, setAnimate] = useState(false); useEffect(() => { async function getArticle() { const response = await getHeroData(); setArticle(response?.data?.data?.content); } getArticle(); }, []); const triggerAnimation = (newIndex: number) => { setAnimate(true); setTimeout(() => { setCurrentNewsIndex(newIndex); setAnimate(false); }, 300); }; const handlePrev = () => { const newIndex = (currentNewsIndex - 1 + article.length) % article.length; triggerAnimation(newIndex); }; const handleNext = () => { const newIndex = (currentNewsIndex + 1) % article.length; triggerAnimation(newIndex); }; useEffect(() => { const interval = setInterval(() => { triggerAnimation((currentNewsIndex + 1) % article.length); }, 7000); return () => clearInterval(interval); }, [article.length]); return (
BREAKING NEWS

{article[currentNewsIndex]?.title}

{textEllipsis(article[currentNewsIndex]?.title, 28)}

{formatDateToIndonesian(article[currentNewsIndex]?.createdAt)}

handlePrev()}> handleNext()}>
); }