nomadro/apps/nomadweb/frontend/src/components/ChatClient.tsx
eric 28998243c3 Add community root-path middleware and fix login next= targets.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 19:07:54 -05:00

109 lines
3.7 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
import Link from "next/link";
import { api } from "@community/lib/api";
import { useAuth } from "@community/lib/auth";
import { useI18n } from "@community/lib/i18n";
import type { ConversationItem } from "@community/lib/types";
import RingNext from "@community/components/RingNext";
import { useRingSteps } from "@community/lib/rings";
function Avatar({ photo, name }: { photo?: string; name?: string }) {
if (photo?.startsWith("http")) {
return (
// eslint-disable-next-line @next/next/no-img-element
<img src={photo} alt="" className="chat-avatar-img" />
);
}
return <span className="chat-avatar">{photo || name?.[0] || "💬"}</span>;
}
export default function ChatClient() {
const { user, token } = useAuth();
const { t } = useI18n();
const rings = useRingSteps();
const [items, setItems] = useState<ConversationItem[]>([]);
const [loading, setLoading] = useState(Boolean(token));
useEffect(() => {
if (!token) {
setLoading(false);
return;
}
setLoading(true);
api
.getConversations(token)
.then(setItems)
.catch(() => setItems([]))
.finally(() => setLoading(false));
}, [token]);
if (!user) {
return (
<div className="chat-page">
<div className="container">
<div className="dating-gate reveal">
<h2>{t.chat.loginTitle}</h2>
<p>{t.chat.loginDesc}</p>
<div className="dating-empty-actions">
<Link href="/community/login?next=/community/chat" className="btn btn-primary">{t.nav.login}</Link>
<Link href="/community/meetups" className="btn btn-ghost">{t.dating.meetAtEvents}</Link>
<Link href="/community/dating" className="btn btn-ghost">{t.chat.goDating}</Link>
</div>
</div>
</div>
</div>
);
}
return (
<div className="chat-page">
<div className="container">
<nav className="detail-nav">
<Link href="/community/dating">{t.nav.dating}</Link>
</nav>
<div className="section-header reveal">
<span className="section-tag">{t.chat.tag}</span>
<h1>{t.chat.title}</h1>
</div>
<div className="chat-list">
{loading && <p className="dest-empty">{t.chat.loadingList}</p>}
{!loading &&
items.map((c) => (
<Link
key={c.id}
href={`/chat/${c.id}`}
className={`chat-list-item reveal${c.unreadCount > 0 ? " unread" : ""}`}
>
<Avatar photo={c.peer?.photo} name={c.peer?.name} />
<div>
<strong>{c.peer?.name || t.chat.peerFallback}</strong>
<p>{c.lastMessagePreview || t.chat.noMessages}</p>
</div>
{c.unreadCount > 0 && <em className="chat-unread">{c.unreadCount}</em>}
</Link>
))}
{!loading && items.length === 0 && (
<div className="notif-empty reveal">
<p className="dest-empty">{t.chat.empty}</p>
<div className="dating-empty-actions">
<Link href="/community/dating" className="btn btn-primary btn-sm">
{t.chat.goDating}
</Link>
<Link href="/community/meetups" className="btn btn-sm">
{t.dating.meetAtEvents}
</Link>
<Link href="/community" className="btn btn-sm">
{t.nav.community}
</Link>
</div>
</div>
)}
</div>
<RingNext steps={rings.afterDating} />
</div>
</div>
);
}