feat(client): 通知中心与未读角标(F16)

- Topbar 增加通知铃铛与未读数
- 新增 /notifications 列表页,支持筛选与标记已读
- 为 workflow/reimbursement/auth 通知提供跳转入口
这个提交包含在:
2026-02-09 18:01:27 +08:00
父节点 589a248cc3
当前提交 273caf10a9
共修改 4 个文件,包含 329 行新增和 0 行删除
+224
查看文件
@@ -0,0 +1,224 @@
import { Link, useSearchParams } from "react-router-dom";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { fetchNotifications, markNotificationRead } from "../api/notifications";
import { isApiError } from "../api/types";
import { mapApiErrorMessage } from "../api/errorMessages";
import { PageHeader } from "../components/common/PageHeader";
import { Button } from "../components/ui/button";
import { Card } from "../components/ui/card";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "../components/ui/table";
import { Tabs, TabsList, TabsTrigger } from "../components/ui/tabs";
const PAGE_SIZE = 20;
function resolveTargetLink(item: { targetType: string | null; targetId: string | null }) {
if (!item.targetType || !item.targetId) return null;
if (item.targetType === "workflow_task") {
return `/workflow/tasks/${item.targetId}`;
}
if (item.targetType === "reimbursement_form") {
return `/reimbursements/${item.targetId}`;
}
if (item.targetType === "employee") {
return "/settings/profile";
}
return null;
}
export function NotificationsPage() {
const queryClient = useQueryClient();
const [searchParams, setSearchParams] = useSearchParams();
const unreadOnlyRaw = searchParams.get("unreadOnly") ?? "0";
const unreadOnly = unreadOnlyRaw === "1" || unreadOnlyRaw === "true";
const page = Number(searchParams.get("page") ?? "1") || 1;
const listQuery = useQuery({
queryKey: ["notifications", "list", unreadOnly, page],
queryFn: () =>
fetchNotifications({
unreadOnly,
page,
pageSize: PAGE_SIZE,
}),
});
const markReadMutation = useMutation({
mutationFn: async (notificationId: string) => markNotificationRead(notificationId),
onSuccess: async () => {
await Promise.all([
queryClient.invalidateQueries({ queryKey: ["notifications"] }),
queryClient.invalidateQueries({ queryKey: ["notifications", "unread-count"] }),
]);
},
});
const totalPages = Math.max(1, Math.ceil((listQuery.data?.total ?? 0) / PAGE_SIZE));
return (
<div className="space-y-6">
<PageHeader
title="通知"
subtitle="站内通知与待办提醒。"
actions={
<Button asChild variant="secondary" data-testid="notifications-back">
<Link to="/dashboard">返回仪表盘</Link>
</Button>
}
/>
<Card className="space-y-4 p-6">
<div className="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
<Tabs
data-testid="notifications-filter-tabs"
value={unreadOnly ? "unread" : "all"}
onValueChange={(value) =>
setSearchParams({
unreadOnly: value === "unread" ? "1" : "0",
page: "1",
})
}
>
<TabsList className="flex w-full flex-wrap justify-start gap-1 md:w-auto">
<TabsTrigger data-testid="notifications-filter-all" value="all">
全部
</TabsTrigger>
<TabsTrigger data-testid="notifications-filter-unread" value="unread">
未读
</TabsTrigger>
</TabsList>
</Tabs>
<div className="text-sm text-slate-600">
未读 {listQuery.data?.unreadCount ?? 0} 条
</div>
</div>
{listQuery.isLoading ? <p className="text-sm text-slate-500">加载中...</p> : null}
{listQuery.isError ? (
<p className="rounded-lg border border-red-200 bg-red-50 px-3 py-2 text-sm text-red-600">
{isApiError(listQuery.error)
? `${listQuery.error.code}: ${mapApiErrorMessage(
listQuery.error.code,
listQuery.error.details,
listQuery.error.message,
)}`
: "加载通知失败"}
</p>
) : null}
{!listQuery.isLoading && !listQuery.isError ? (
<div className="rounded-lg border border-slate-200">
<Table data-testid="notifications-table">
<TableHeader>
<tr>
<TableHead>标题</TableHead>
<TableHead>时间</TableHead>
<TableHead>状态</TableHead>
<TableHead className="w-40">操作</TableHead>
</tr>
</TableHeader>
<TableBody>
{(listQuery.data?.items ?? []).length ? (
listQuery.data?.items.map((item) => {
const targetLink = resolveTargetLink(item);
return (
<TableRow data-testid="notification-row" key={item.id}>
<TableCell>
<div className="font-medium text-slate-900">{item.title}</div>
{item.body ? (
<div className="mt-1 text-xs text-slate-500">{item.body}</div>
) : null}
</TableCell>
<TableCell className="text-xs text-slate-600">
{item.createdAt.slice(0, 19).replace("T", " ")}
</TableCell>
<TableCell className="text-xs text-slate-600">
{item.readAt ? "已读" : "未读"}
</TableCell>
<TableCell>
<div className="flex flex-wrap gap-2">
{targetLink ? (
<Button asChild size="sm" variant="secondary" data-testid="notification-target-link">
<Link to={targetLink}>查看</Link>
</Button>
) : null}
{!item.readAt ? (
<Button
data-testid="notification-mark-read"
size="sm"
type="button"
variant="secondary"
disabled={markReadMutation.isPending}
onClick={() => markReadMutation.mutate(item.id)}
>
标为已读
</Button>
) : null}
</div>
</TableCell>
</TableRow>
);
})
) : (
<TableRow>
<TableCell className="py-3 text-slate-500" colSpan={4}>
暂无通知
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
) : null}
<div className="flex items-center justify-between text-sm">
<span>
第 {page} / {totalPages} 页
</span>
<div className="flex gap-2">
<Button
data-testid="notifications-prev-page"
variant="secondary"
disabled={page <= 1}
onClick={() =>
setSearchParams({ unreadOnly: unreadOnly ? "1" : "0", page: String(Math.max(1, page - 1)) })
}
type="button"
>
上一页
</Button>
<Button
data-testid="notifications-next-page"
variant="secondary"
disabled={page >= totalPages}
onClick={() =>
setSearchParams({
unreadOnly: unreadOnly ? "1" : "0",
page: String(Math.min(totalPages, page + 1)),
})
}
type="button"
>
下一页
</Button>
</div>
</div>
</Card>
</div>
);
}