"use client"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { listCoWriterDocuments, type CoWriterDocumentSummary, } from "@/lib/co-writer-api"; import { subscribeCoWriterChanges } from "@/lib/co-writer-events"; import { formatRelativeTime } from "@/lib/relative-time"; interface CoWriterRecentProps { collapsed?: boolean; limit?: number; } export function CoWriterRecent({ collapsed = false, limit = 4, }: CoWriterRecentProps) { const { i18n } = useTranslation(); const [docs, setDocs] = useState([]); const pathname = usePathname(); useEffect(() => { let active = true; void listCoWriterDocuments() .then((items) => { if (active) setDocs(items.slice(0, limit)); }) .catch(() => {}); return () => { active = false; }; }, [limit, pathname]); useEffect(() => { let active = true; const unsubscribe = subscribeCoWriterChanges(() => { void listCoWriterDocuments() .then((items) => { if (active) setDocs(items.slice(0, limit)); }) .catch(() => {}); }); return () => { active = false; unsubscribe(); }; }, [limit]); if (docs.length === 0) return null; if (collapsed) return null; return (
{docs.map((doc) => ( {doc.title || "Untitled draft"} {formatRelativeTime(Number(doc.updated_at) || 0, i18n.language)} ))}
); }