// NoticeDrawer — F2-11 消息中心抽屉 // 从铃铛图标点击弹出,显示站内消息列表;支持单条置已读、全部已读、展开内容 import { useEffect, useRef, useState } from 'react'; import { Icon } from '../components/ui/Icon'; import { fetchNotices, markNoticeRead, markAllNoticesRead, type NoticeItem, } from '../api/notice'; interface NoticeDrawerProps { onClose: () => void; /** 关闭后通知父层刷新未读数 */ onReadChange: () => void; } /** 格式化毫秒时间戳为可读字符串 */ function fmtTime(ts: number | null): string { if (!ts) return ''; const d = new Date(ts); const now = new Date(); const diffMs = now.getTime() - d.getTime(); const diffMin = Math.floor(diffMs / 60_000); if (diffMin < 1) return '刚刚'; if (diffMin < 60) return `${diffMin} 分钟前`; const diffH = Math.floor(diffMin / 60); if (diffH < 24) return `${diffH} 小时前`; const diffD = Math.floor(diffH / 24); if (diffD < 7) return `${diffD} 天前`; return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`; } export function NoticeDrawer({ onClose, onReadChange }: NoticeDrawerProps) { const [items, setItems] = useState([]); const [total, setTotal] = useState(0); const [loading, setLoading] = useState(true); const [unreadOnly, setUnreadOnly] = useState(false); const [expandedId, setExpandedId] = useState(null); const [markingAll, setMarkingAll] = useState(false); const drawerRef = useRef(null); // 加载消息列表 const load = (unread: boolean) => { setLoading(true); fetchNotices({ unreadOnly: unread, page: 1, perPage: 20 }) .then((r) => { setItems(r.items); setTotal(r.total); }) .catch(() => { setItems([]); setTotal(0); }) .finally(() => setLoading(false)); }; useEffect(() => { load(unreadOnly); }, [unreadOnly]); // 点击抽屉外部关闭 useEffect(() => { const handler = (e: MouseEvent) => { if (drawerRef.current && !drawerRef.current.contains(e.target as Node)) { onClose(); } }; document.addEventListener('mousedown', handler); return () => document.removeEventListener('mousedown', handler); }, [onClose]); // 单条置已读 const handleRead = async (item: NoticeItem) => { // 展开/收起内容 setExpandedId((prev) => (prev === item.id ? null : item.id)); if (item.isread === 0) { try { await markNoticeRead(item.id); setItems((prev) => prev.map((i) => (i.id === item.id ? { ...i, isread: 1 } : i)), ); onReadChange(); } catch { // 静默失败,不影响 UI } } }; // 全部已读 const handleReadAll = async () => { setMarkingAll(true); try { await markAllNoticesRead(); setItems((prev) => prev.map((i) => ({ ...i, isread: 1 }))); onReadChange(); } catch { // 静默失败 } finally { setMarkingAll(false); } }; const unreadCount = items.filter((i) => i.isread === 0).length; return ( <> {/* 半透明遮罩 */}
{/* 抽屉主体 */}
{/* 头部 */}
消息中心 {unreadCount > 0 && ( {unreadCount} )}
{unreadCount > 0 && ( )}
{/* 过滤标签 */}
{(['全部', '仅未读'] as const).map((label, i) => { const active = i === 0 ? !unreadOnly : unreadOnly; return ( ); })} 共 {total} 条
{/* 消息列表 */}
{loading && (
加载中…
)} {!loading && items.length === 0 && (
{unreadOnly ? '没有未读消息' : '暂无消息'}
)} {!loading && items.map((item) => { const isExpanded = expandedId === item.id; return (
handleRead(item)} style={{ padding: '12px 16px', borderBottom: '1px solid var(--line)', cursor: 'pointer', background: item.isread === 0 ? 'var(--brand-softer)' : 'var(--surface)', transition: 'background .1s', }} onMouseEnter={(e) => ((e.currentTarget as HTMLDivElement).style.background = item.isread === 0 ? 'var(--brand-soft)' : 'var(--surface-2)') } onMouseLeave={(e) => ((e.currentTarget as HTMLDivElement).style.background = item.isread === 0 ? 'var(--brand-softer)' : 'var(--surface)') } >
{/* 未读蓝点 */}
{item.title}
{isExpanded && item.summary && (
{item.summary}
)}
{item.type != null && ( {item.type} )} {fmtTime(item.createtime)}
); })}
); }