"use client"; import { useEffect, useMemo, useState } from "react"; import { Link } from "@/i18n/navigation"; import Footer from "@/app/components/Footer"; import { apiFetch } from "@/app/lib/api-client"; interface NotificationItem { id: string; title: string; body: string; category: string; priority: "low" | "normal" | "high" | string; actionLabel?: string; actionUrl?: string; entityType?: string; entityId?: string; icon?: string; created?: string; read: boolean; archived: boolean; pinned: boolean; muted?: boolean; metadata?: Record; } interface PreferenceRecord { channels: Record; categories: Record; quietHoursStart: string; quietHoursEnd: string; digest: string; allowMarketing: boolean; } const CATEGORY_META: Record = { all: { label: "全部", icon: "📬", color: "bg-gray-900 text-white" }, system: { label: "系统", icon: "🔔", color: "bg-slate-100 text-slate-700 dark:bg-slate-800 dark:text-slate-200" }, meetup: { label: "活动", icon: "🍹", color: "bg-orange-100 text-orange-700 dark:bg-orange-950/40 dark:text-orange-200" }, gig: { label: "赏金", icon: "💰", color: "bg-emerald-100 text-emerald-700 dark:bg-emerald-950/40 dark:text-emerald-200" }, match: { label: "匹配", icon: "💚", color: "bg-pink-100 text-pink-700 dark:bg-pink-950/40 dark:text-pink-200" }, service: { label: "服务", icon: "🧭", color: "bg-blue-100 text-blue-700 dark:bg-blue-950/40 dark:text-blue-200" }, community: { label: "社区", icon: "🔥", color: "bg-violet-100 text-violet-700 dark:bg-violet-950/40 dark:text-violet-200" }, city: { label: "城市", icon: "🏙️", color: "bg-cyan-100 text-cyan-700 dark:bg-cyan-950/40 dark:text-cyan-200" }, }; const STATUS_TABS = [ { key: "active", label: "收件箱" }, { key: "unread", label: "未读" }, { key: "read", label: "已读" }, { key: "archived", label: "归档" }, ]; const DEFAULT_PREFERENCES: PreferenceRecord = { channels: { in_app: true, email: false, wechat: false }, categories: { system: true, meetup: true, gig: true, match: true, service: true, community: true, city: true, }, quietHoursStart: "22:00", quietHoursEnd: "08:00", digest: "daily", allowMarketing: false, }; function formatTime(value?: string): 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 "刚刚"; if (minutes < 60) return `${minutes} 分钟前`; const hours = Math.floor(minutes / 60); if (hours < 24) return `${hours} 小时前`; return `${date.getMonth() + 1}月${date.getDate()}日`; } function priorityLabel(priority: string): string { if (priority === "high") return "重要"; if (priority === "low") return "低优先级"; return "普通"; } export default function MessagesPage() { const [items, setItems] = useState([]); const [unread, setUnread] = useState(0); const [categories, setCategories] = useState>({}); const [mutedCount, setMutedCount] = useState(0); const [category, setCategory] = useState("all"); const [status, setStatus] = useState("active"); const [query, setQuery] = useState(""); const [loading, setLoading] = useState(false); const [selectedId, setSelectedId] = useState(null); const [preferences, setPreferences] = useState(DEFAULT_PREFERENCES); const [notice, setNotice] = useState(""); const selected = useMemo( () => items.find((item) => item.id === selectedId) || items[0], [items, selectedId] ); const loadNotifications = () => { setLoading(true); const params = new URLSearchParams({ category, status, q: query }); apiFetch<{ items: NotificationItem[]; unread: number; categories: Record; muted: number }>(`/api/notifications?${params.toString()}`) .then((data) => { setItems(data.items || []); setUnread(Number(data.unread || 0)); setCategories(data.categories || {}); setMutedCount(Number(data.muted || 0)); if (data.items?.length && !data.items.some((item) => item.id === selectedId)) { setSelectedId(data.items[0].id); } }) .catch(() => { setItems([]); setUnread(0); setCategories({}); setMutedCount(0); }) .finally(() => setLoading(false)); }; useEffect(() => { loadNotifications(); apiFetch<{ record: PreferenceRecord }>("/api/notifications/preferences") .then((data) => setPreferences({ ...DEFAULT_PREFERENCES, ...(data.record || {}) })) .catch(() => setPreferences(DEFAULT_PREFERENCES)); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); useEffect(() => { const timer = window.setTimeout(loadNotifications, 250); return () => window.clearTimeout(timer); // eslint-disable-next-line react-hooks/exhaustive-deps }, [category, status, query]); const mutateNotification = async (id: string, action: "read" | "unread" | "archive" | "restore" | "pin") => { await apiFetch(`/api/notifications/${id}/${action}`, { method: "POST" }); loadNotifications(); }; const markAllRead = async () => { const data = await apiFetch<{ count: number }>("/api/notifications/mark-all-read", { method: "POST" }); setNotice(`已标记 ${data.count} 条消息为已读`); loadNotifications(); }; const savePreferences = async () => { await apiFetch("/api/notifications/preferences", { method: "PUT", body: JSON.stringify(preferences), }); setNotice("通知偏好已保存"); loadNotifications(); }; return (

Message Center

站内消息中心

活动、任务、匹配、服务咨询、城市更新和系统公告统一收口,所有动作都和 FastAPI / PocketBase 数据联动。

偏好设置
{notice && (
{notice}
)}

未读消息

{unread}

当前列表

{items.length}

静音消息

{mutedCount}

当前策略

{preferences.digest === "instant" ? "实时提醒" : "每日摘要"}

{preferences.quietHoursStart} - {preferences.quietHoursEnd} 免打扰

消息流

{loading ? "正在同步 PocketBase" : "最新消息在前,置顶优先"}

{items.length} 条
{items.map((item) => { const meta = CATEGORY_META[item.category] || CATEGORY_META.system; const active = selected?.id === item.id; return ( ); })}
{items.length === 0 && (
📭

没有符合条件的消息

换一个分类、状态或搜索词试试。

)}
); }