nomadro/apps/nomadweb/frontend/src/components/NotificationsClient.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

135 lines
4.5 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 { NotificationItem } from "@community/lib/types";
import RingNext from "@community/components/RingNext";
import { useRingSteps } from "@community/lib/rings";
export default function NotificationsClient() {
const { token } = useAuth();
const { t } = useI18n();
const rings = useRingSteps();
const [items, setItems] = useState<NotificationItem[]>([]);
const [loading, setLoading] = useState(Boolean(token));
useEffect(() => {
if (!token) {
setItems([]);
setLoading(false);
return;
}
setLoading(true);
api
.getNotifications(token)
.then(setItems)
.catch(() => setItems([]))
.finally(() => setLoading(false));
}, [token]);
const markAll = async () => {
if (!token) return;
await api.markNotificationsRead(token).catch(() => null);
setItems((list) => list.map((n) => ({ ...n, read: true })));
};
const openOne = async (n: NotificationItem) => {
if (!token || n.read) return;
await api.notificationAction(token, n.id, "read").catch(() => null);
setItems((list) => list.map((x) => (x.id === n.id ? { ...x, read: true } : x)));
};
if (!token) {
return (
<div className="chat-page">
<div className="container">
<div className="dating-gate reveal">
<h2>{t.notifications.title}</h2>
<p>{t.notifications.loginDesc}</p>
<div className="dating-empty-actions">
<Link href="/community/login?next=/community/notifications" className="btn btn-primary">
{t.nav.login}
</Link>
<Link href="/community/meetups" className="btn btn-ghost">
{t.nav.meetups}
</Link>
<Link href="/community" className="btn btn-ghost">
{t.nav.community}
</Link>
</div>
</div>
<RingNext steps={rings.afterDating} />
</div>
</div>
);
}
return (
<div className="chat-page">
<div className="container">
<div className="section-header reveal">
<h1>{t.notifications.title}</h1>
<div className="notif-toolbar">
<button type="button" className="btn btn-sm" onClick={() => void markAll()} disabled={items.every((n) => n.read)}>
{t.notifications.markRead}
</button>
<Link href="/community/settings/notifications" className="btn btn-sm">
{t.notifSettings.title}
</Link>
</div>
</div>
<div className="chat-list">
{loading && <p className="dest-empty">{t.common.loading}</p>}
{!loading &&
items.map((n) => (
<div key={n.id} className={`chat-list-item reveal${n.read ? "" : " unread"}`}>
<div>
<strong>{n.title}</strong>
<p>{n.body}</p>
<small>{n.created_at}</small>
</div>
{n.link ? (
<Link
href={n.link}
onClick={() => {
void openOne(n);
}}
>
{t.common.open}
</Link>
) : (
!n.read && (
<button type="button" className="btn btn-sm" onClick={() => void openOne(n)}>
{t.notifications.markOne}
</button>
)
)}
</div>
))}
{!loading && items.length === 0 && (
<div className="notif-empty reveal">
<p className="dest-empty">{t.notifications.empty}</p>
<p className="section-desc">{t.notifications.emptyHint}</p>
<div className="notif-empty-links">
<Link href="/community/meetups" className="btn btn-sm">
{t.nav.meetups}
</Link>
<Link href="/community/dating" className="btn btn-sm">
{t.nav.dating}
</Link>
<Link href="/community" className="btn btn-sm">
{t.nav.community}
</Link>
</div>
</div>
)}
</div>
<RingNext steps={rings.afterDating} />
</div>
</div>
);
}