"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"; import { useTranslations } from "next-intl"; import { useParams } from "next/navigation"; export default function NewsTicker() { const [article, setArticle] = useState([]); const [currentNewsIndex, setCurrentNewsIndex] = useState(0); const [animate, setAnimate] = useState(false); const params = useParams(); const locale = params?.locale; const t = useTranslations("LandingPage"); useEffect(() => { async function getArticle() { const response = await getHeroData(locale == "en"); 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 (
{" "}
{" "} {t("breakingNews", { defaultValue: "Breaking News" })}{" "}
{" "}
{" "}
{" "} {article?.length > 0 && ( <> {" "} {" "}

{article[currentNewsIndex]?.title}

{" "} {" "} {" "}

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

{" "} {" "}

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

{" "} )}{" "}
{" "}
{" "} handlePrev()} > {" "} {" "} {" "} handleNext()} > {" "} {" "} {" "}
{" "}
); }