"use client"; import { useEffect, useState } from "react"; import { getFirestore, collection, onSnapshot, query, orderBy, limit, } from "firebase/firestore"; import { motion, AnimatePresence } from "framer-motion"; import Link from "next/link"; import { ClockIcon } from "./icons"; import firebaseApp from "@/config/firebase"; const db = getFirestore(firebaseApp()); const timeAgo = (timestamp: any) => { if (!timestamp?.toDate) return "Just now"; const now = new Date(); const diff = (now.getTime() - timestamp.toDate().getTime()) / 1000; if (diff < 60) return `${Math.floor(diff)}s ago`; if (diff < 3600) return `${Math.floor(diff / 60)}m ago`; if (diff < 86400) return `${Math.floor(diff / 3600)}h ago`; return `${Math.floor(diff / 86400)}d ago`; }; const Updates = () => { const [latestUpdate, setLatestUpdate] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { const q = query( collection(db, "updates"), orderBy("createdAt", "desc"), limit(1), ); const unsubscribe = onSnapshot(q, (snapshot) => { if (!snapshot.empty) { setLatestUpdate({ id: snapshot.docs[0].id, ...snapshot.docs[0].data(), }); } setLoading(false); }); return () => unsubscribe(); }, []); if (loading || !latestUpdate) return null; return (
{/* Animated Background Glow */}
UPDATE

{latestUpdate.text}

{timeAgo(latestUpdate.createdAt)}
View All
{/* Mobile Arrow */}
); }; export default Updates;