"use client"; import { useEffect, useState } from "react"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { useTranslations } from "next-intl"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Badge } from "@/components/ui/badge"; import { cn } from "@/lib/utils"; type ConversationSummary = { id: string; otherUser: { id: string; name: string; username: string | null; avatarUrl: string | null } | null; lastMessage: string | null; lastMessageAt: string; unreadCount: number; }; export function ConversationsList() { const pathname = usePathname(); const t = useTranslations("messages"); const [conversations, setConversations] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { let cancelled = false; async function load() { const res = await fetch("/api/v1/conversations"); if (res.ok && !cancelled) { const data = (await res.json()) as { conversations: ConversationSummary[] }; setConversations(data.conversations); } if (!cancelled) setLoading(false); } void load(); const interval = setInterval(load, 10000); return () => { cancelled = true; clearInterval(interval); }; }, []); if (loading) return

{t("loading")}

; if (conversations.length === 0) { return

{t("noConversationsYet")}

; } return (
{conversations.map((c) => ( {c.otherUser?.avatarUrl && } {(c.otherUser?.name ?? "?").slice(0, 2).toUpperCase()}

0 && "font-semibold")}> {c.otherUser?.name ?? t("unknownUser")}

{c.unreadCount > 0 && ( {c.unreadCount > 9 ? "9+" : c.unreadCount} )}

0 ? "text-foreground" : "text-muted-foreground")}> {c.lastMessage ?? t("noMessagesPreview")}

))}
); }