import React, { useState, useEffect } from "react"; import { Megaphone } from "lucide-react"; import { Link } from "@inertiajs/react"; import { Post } from "@/components/DetailArtikel/types"; interface RunningTextProps { posts: Post[]; } export default function RunningText({ posts }: RunningTextProps) { const [currentMessageIndex, setCurrentMessageIndex] = useState(0); const [isVisible, setIsVisible] = useState(true); const [isPaused, setIsPaused] = useState(false); const getLatestPostsByCategory = () => { const groupedPosts = posts.reduce((acc, post) => { const category = post.kategori?.NamaKategori || "Uncategorized"; if (!acc[category]) { acc[category] = []; } acc[category].push(post); return acc; }, {} as Record); // Get latest 2 posts from each category and flatten return Object.values(groupedPosts) .map((categoryPosts) => categoryPosts .sort( (a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime() ) .slice(0, 2) ) .flat(); }; const messages = getLatestPostsByCategory(); useEffect(() => { if (isPaused || messages.length === 0) return; const interval = setInterval(() => { setIsVisible(false); setTimeout(() => { setCurrentMessageIndex( (prevIndex) => (prevIndex + 1) % messages.length ); setIsVisible(true); }, 500); }, 3000); return () => clearInterval(interval); }, [isPaused, messages.length]); const getRouteByCategory = (kategori: string, slug: string) => { const routes: { [key: string]: string } = { Pengumuman: `/pengumuman/${slug}`, Peraturan: `/peraturan/${slug}`, Undangan: `/undangan/${slug}`, }; return routes[kategori] || `/sekilasinfo/${slug}`; }; if (messages.length === 0) return null; return (
setIsPaused(true)} onMouseLeave={() => setIsPaused(false)} >

{ messages[currentMessageIndex].kategori ?.NamaKategori } {" "} {messages[currentMessageIndex].JudulPost}

); }