"use client"; import { useEffect, useState } from "react"; import { Link } from "@/i18n/navigation"; import { useLocale, useTranslation } from "@/i18n/navigation"; import Footer from "@/app/components/Footer"; import { fetchAuthMe } from "@/app/lib/api-client"; import { fetchConversations, type ConversationItem } from "@/app/lib/chat"; import { mediaUrl } from "@/app/lib/media"; function formatTime(value?: string, locale = "zh"): string { if (!value) return ""; const date = new Date(value); if (Number.isNaN(date.getTime())) return ""; const diff = Date.now() - date.getTime(); const minutes = Math.floor(diff / 60000); if (minutes < 1) return locale === "zh" ? "刚刚" : "Now"; if (minutes < 60) return locale === "zh" ? `${minutes} 分钟前` : `${minutes}m`; const hours = Math.floor(minutes / 60); if (hours < 24) return locale === "zh" ? `${hours} 小时前` : `${hours}h`; const days = Math.floor(hours / 24); if (days < 7) return locale === "zh" ? `${days} 天前` : `${days}d`; return `${date.getMonth() + 1}/${date.getDate()}`; } function ConversationRow({ item, locale }: { item: ConversationItem; locale: string }) { const peerName = item.peer?.name || (locale === "zh" ? "成员" : "Member"); const preview = item.lastMessagePreview || (locale === "zh" ? "开始聊天吧" : "Say hello"); const unread = Number(item.unreadCount || 0); return (
{peerName} {unread > 0 ? ( {unread > 9 ? "9+" : unread} ) : null}

{peerName}

{formatTime(item.lastMessageAt, locale)}

{item.peer?.location ? `📍 ${item.peer.location} · ` : ""} {preview}

); } export default function ChatListPage() { const { t } = useTranslation("chat"); const locale = useLocale(); const [items, setItems] = useState([]); const [unread, setUnread] = useState(0); const [loading, setLoading] = useState(true); const [isLoggedIn, setIsLoggedIn] = useState(false); const load = () => { setLoading(true); fetchConversations() .then((data) => { setItems(data.items); setUnread(data.unread); }) .catch(() => { setItems([]); setUnread(0); }) .finally(() => setLoading(false)); }; useEffect(() => { fetchAuthMe().then((data) => setIsLoggedIn(Boolean(data?.user?.id))); load(); }, []); return (

Direct Messages

{t("title")}

{t("subtitle")}

{t("notifications")} {t("goMatching")}
{unread > 0 ? (

{locale === "zh" ? `${unread} 条未读私信` : `${unread} unread message${unread === 1 ? "" : "s"}`}

) : null}
{!isLoggedIn ? (

{t("loginRequired")}

{t("loginAction")}
) : loading ? (
{[1, 2, 3].map((key) => (
))}
) : items.length ? (
{items.map((item) => ( ))}
) : (

{t("emptyTitle")}

{t("emptySubtitle")}

{t("goMatching")}
)}
); }