F2-11: 消息中心前端 — Drawer、顶栏铃铛、工作台通知卡、e2e
- src/api/notice.ts:新增 fetchNotices/fetchUnreadCount/markNoticeRead/markAllNoticesRead - src/shell/NoticeDrawer.tsx:消息中心抽屉(未读蓝点/展开内容/全部已读/过滤标签) - src/shell/Topbar.tsx:新增消息图标按钮(doc 图标,noticeBadge badge) - src/shell/AppShell.tsx:消息未读数 60s 轮询 + NoticeDrawer 状态管理 - src/pages/WorkbenchPage.tsx:点亮通知公告卡片(原占位注释替换为真实卡片) - e2e/notice.spec.ts:5 个用例(API 3 + UI 2),50/50 全绿 - e2e/artifacts/f2-11/:截图(工作台卡片/Drawer 展开/全部已读后) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
306
src/shell/NoticeDrawer.tsx
Normal file
306
src/shell/NoticeDrawer.tsx
Normal file
@@ -0,0 +1,306 @@
|
||||
// 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<NoticeItem[]>([]);
|
||||
const [total, setTotal] = useState(0);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [unreadOnly, setUnreadOnly] = useState(false);
|
||||
const [expandedId, setExpandedId] = useState<string | null>(null);
|
||||
const [markingAll, setMarkingAll] = useState(false);
|
||||
const drawerRef = useRef<HTMLDivElement>(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 (
|
||||
<>
|
||||
{/* 半透明遮罩 */}
|
||||
<div
|
||||
style={{
|
||||
position: 'fixed',
|
||||
inset: 0,
|
||||
background: 'rgba(0,0,0,0.18)',
|
||||
zIndex: 999,
|
||||
}}
|
||||
/>
|
||||
|
||||
{/* 抽屉主体 */}
|
||||
<div
|
||||
ref={drawerRef}
|
||||
style={{
|
||||
position: 'fixed',
|
||||
top: 0,
|
||||
right: 0,
|
||||
bottom: 0,
|
||||
width: 400,
|
||||
background: 'var(--surface)',
|
||||
boxShadow: '-4px 0 24px rgba(0,0,0,0.12)',
|
||||
zIndex: 1000,
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
overflow: 'hidden',
|
||||
}}
|
||||
>
|
||||
{/* 头部 */}
|
||||
<div
|
||||
style={{
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'space-between',
|
||||
padding: '14px 16px',
|
||||
borderBottom: '1px solid var(--line)',
|
||||
flexShrink: 0,
|
||||
}}
|
||||
>
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
|
||||
<Icon name="bell" size={16} />
|
||||
<span style={{ fontWeight: 600, fontSize: 'var(--fs)' }}>消息中心</span>
|
||||
{unreadCount > 0 && (
|
||||
<span
|
||||
style={{
|
||||
background: 'var(--err)',
|
||||
color: '#fff',
|
||||
borderRadius: 10,
|
||||
padding: '1px 7px',
|
||||
fontSize: 11,
|
||||
fontWeight: 700,
|
||||
}}
|
||||
>
|
||||
{unreadCount}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
|
||||
{unreadCount > 0 && (
|
||||
<button
|
||||
className="btn btn-text btn"
|
||||
onClick={handleReadAll}
|
||||
disabled={markingAll}
|
||||
style={{ fontSize: 'var(--fs-sm)' }}
|
||||
>
|
||||
{markingAll ? '处理中…' : '全部已读'}
|
||||
</button>
|
||||
)}
|
||||
<button className="tb-icon" onClick={onClose} title="关闭">
|
||||
<Icon name="x" size={16} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 过滤标签 */}
|
||||
<div
|
||||
style={{
|
||||
display: 'flex',
|
||||
gap: 8,
|
||||
padding: '8px 16px',
|
||||
borderBottom: '1px solid var(--line)',
|
||||
flexShrink: 0,
|
||||
}}
|
||||
>
|
||||
{(['全部', '仅未读'] as const).map((label, i) => {
|
||||
const active = i === 0 ? !unreadOnly : unreadOnly;
|
||||
return (
|
||||
<button
|
||||
key={label}
|
||||
className={`btn btn-sm ${active ? 'btn-pri' : 'btn-text'}`}
|
||||
onClick={() => setUnreadOnly(i === 1)}
|
||||
>
|
||||
{label}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
<span className="muted" style={{ fontSize: 'var(--fs-sm)', marginLeft: 'auto', lineHeight: '28px' }}>
|
||||
共 {total} 条
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* 消息列表 */}
|
||||
<div style={{ flex: 1, overflowY: 'auto' }}>
|
||||
{loading && (
|
||||
<div className="empty" style={{ color: 'var(--text-3)' }}>加载中…</div>
|
||||
)}
|
||||
|
||||
{!loading && items.length === 0 && (
|
||||
<div className="empty">
|
||||
<Icon name="bell" size={28} style={{ color: 'var(--text-3)' }} />
|
||||
{unreadOnly ? '没有未读消息' : '暂无消息'}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!loading && items.map((item) => {
|
||||
const isExpanded = expandedId === item.id;
|
||||
return (
|
||||
<div
|
||||
key={item.id}
|
||||
onClick={() => 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)')
|
||||
}
|
||||
>
|
||||
<div style={{ display: 'flex', alignItems: 'flex-start', gap: 8 }}>
|
||||
{/* 未读蓝点 */}
|
||||
<span
|
||||
style={{
|
||||
width: 8,
|
||||
height: 8,
|
||||
borderRadius: '50%',
|
||||
background: item.isread === 0 ? 'var(--brand)' : 'transparent',
|
||||
flexShrink: 0,
|
||||
marginTop: 6,
|
||||
}}
|
||||
/>
|
||||
<div style={{ flex: 1, minWidth: 0 }}>
|
||||
<div
|
||||
style={{
|
||||
fontWeight: item.isread === 0 ? 600 : 400,
|
||||
fontSize: 'var(--fs)',
|
||||
overflow: 'hidden',
|
||||
textOverflow: 'ellipsis',
|
||||
whiteSpace: isExpanded ? 'normal' : 'nowrap',
|
||||
}}
|
||||
>
|
||||
{item.title}
|
||||
</div>
|
||||
{isExpanded && item.summary && (
|
||||
<div
|
||||
style={{
|
||||
marginTop: 6,
|
||||
fontSize: 'var(--fs-sm)',
|
||||
color: 'var(--text-2)',
|
||||
lineHeight: 1.6,
|
||||
whiteSpace: 'pre-wrap',
|
||||
wordBreak: 'break-word',
|
||||
}}
|
||||
>
|
||||
{item.summary}
|
||||
</div>
|
||||
)}
|
||||
<div
|
||||
style={{
|
||||
marginTop: 4,
|
||||
fontSize: 11,
|
||||
color: 'var(--text-3)',
|
||||
display: 'flex',
|
||||
gap: 8,
|
||||
}}
|
||||
>
|
||||
{item.type != null && (
|
||||
<span className="tag tag-info" style={{ fontSize: 10 }}>
|
||||
{item.type}
|
||||
</span>
|
||||
)}
|
||||
<span>{fmtTime(item.createtime)}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user