Files
SupplierSystem/src/components/NotificationBell.tsx
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

192 lines
6.9 KiB
TypeScript

import React, { useCallback, useRef, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { Bell } from 'lucide-react';
import { useAuth } from '../hooks/useAuth';
import { useClickOutside } from '../hooks/useClickOutside';
import { useNotificationStream, type StreamNotificationItem } from '../hooks/useNotificationStream';
import {
clearAllNotifications,
markAllNotificationsRead,
markNotificationRead,
navigateToNotificationTarget,
} from '../utils/notificationActions';
type NotificationItem = StreamNotificationItem;
function mergeNotification(prev: NotificationItem[], incoming: NotificationItem) {
const withoutDup = prev.filter((item) => item.id !== incoming.id);
return [incoming, ...withoutDup].slice(0, 8);
}
export const NotificationBell: React.FC = () => {
const navigate = useNavigate();
const { session } = useAuth();
const [open, setOpen] = useState(false);
const [loading, setLoading] = useState(true);
const [busy, setBusy] = useState(false);
const [notifications, setNotifications] = useState<NotificationItem[]>([]);
const containerRef = useRef<HTMLDivElement>(null);
const gotSnapshotRef = useRef(false);
const handleSnapshot = useCallback((items: StreamNotificationItem[]) => {
gotSnapshotRef.current = true;
setLoading(false);
setNotifications(items as NotificationItem[]);
}, []);
const handlePush = useCallback((item: StreamNotificationItem) => {
setLoading(false);
setNotifications((prev) => mergeNotification(prev, item as NotificationItem));
}, []);
useNotificationStream({
enabled: Boolean(session),
onSnapshot: handleSnapshot,
onNotification: handlePush,
onRefresh: handleSnapshot,
});
useClickOutside(containerRef, open, () => setOpen(false));
const unreadCount = notifications.filter((n) => !n.is_read).length;
const handleToggle = () => {
setOpen((prev) => !prev);
};
const markItemRead = async (item: NotificationItem) => {
if (item.is_read) return;
await markNotificationRead(item.id);
setNotifications((prev) =>
prev.map((n) => (n.id === item.id ? { ...n, is_read: true } : n))
);
};
const handleNotificationClick = async (item: NotificationItem) => {
await markItemRead(item);
};
const handleNotificationDoubleClick = async (item: NotificationItem) => {
setOpen(false);
await navigateToNotificationTarget(item, navigate, {
markRead: () => markItemRead(item),
});
};
const handleMarkAllRead = async () => {
if (busy || unreadCount <= 0) return;
setBusy(true);
try {
await markAllNotificationsRead();
setNotifications((prev) => prev.map((n) => ({ ...n, is_read: true })));
} catch (error) {
console.error('全部已读失败:', error);
} finally {
setBusy(false);
}
};
const handleClearAll = async () => {
if (busy || notifications.length <= 0) return;
setBusy(true);
try {
await clearAllNotifications();
setNotifications([]);
} catch (error) {
console.error('清空通知失败:', error);
} finally {
setBusy(false);
}
};
return (
<div ref={containerRef} className="relative">
<button
type="button"
onClick={handleToggle}
aria-expanded={open}
aria-label="消息通知"
className="app-row-hover p-2 rounded-md transition-colors duration-150 relative"
>
<Bell className="w-4 h-4 text-slate-600 dark:text-slate-300" />
{unreadCount > 0 && (
<span className="absolute top-1 right-1 min-w-[16px] h-4 px-1 bg-indigo-600 text-white text-[10px] font-medium rounded-full ring-2 ring-white dark:ring-slate-900 flex items-center justify-center">
{unreadCount > 9 ? '9+' : unreadCount}
</span>
)}
</button>
{open && (
<div className="app-surface app-foreground absolute right-0 top-full mt-2 w-80 rounded-xl border shadow-lg z-50 overflow-hidden">
<div className="app-border flex items-center justify-between gap-2 px-4 py-3 border-b">
<p className="text-sm font-semibold text-slate-900 dark:text-slate-100">消息通知</p>
<div className="flex items-center gap-2">
{unreadCount > 0 && (
<button
type="button"
disabled={busy}
onClick={() => void handleMarkAllRead()}
className="text-xs text-indigo-600 hover:text-indigo-800 disabled:opacity-50"
>
全部已读
</button>
)}
{notifications.length > 0 && (
<button
type="button"
disabled={busy}
onClick={() => void handleClearAll()}
className="text-xs text-slate-500 hover:text-slate-700 dark:hover:text-slate-200 disabled:opacity-50"
>
一键清空
</button>
)}
</div>
</div>
<div className="max-h-80 overflow-y-auto">
{loading && !gotSnapshotRef.current ? (
<div className="app-foreground-muted py-10 text-center text-sm">连接中...</div>
) : notifications.length === 0 ? (
<div className="app-foreground-muted py-10 text-center text-sm">暂无通知</div>
) : (
notifications.map((item) => (
<button
key={item.id}
type="button"
onClick={() => void handleNotificationClick(item)}
onDoubleClick={() => void handleNotificationDoubleClick(item)}
title="双击跳转到处理页面"
className={`app-border app-row-hover w-full text-left px-4 py-3 border-b transition-colors ${
!item.is_read ? 'bg-indigo-50/60 dark:bg-indigo-950/30' : ''
}`}
>
<p className="text-sm font-medium text-slate-900 dark:text-slate-100 line-clamp-1">
{item.title || '系统通知'}
</p>
{item.content && (
<p className="app-foreground-muted text-xs mt-1 line-clamp-2">{item.content}</p>
)}
<p className="app-foreground-muted text-[11px] mt-2">
{item.created_at ? new Date(item.created_at).toLocaleString('zh-CN') : '-'}
</p>
</button>
))
)}
</div>
<button
type="button"
onClick={() => {
setOpen(false);
navigate('/system/notifications');
}}
className="app-border w-full px-4 py-2.5 text-sm text-indigo-600 dark:text-indigo-300 hover:bg-indigo-50 dark:hover:bg-indigo-950/40 border-t"
>
查看全部通知
</button>
</div>
)}
</div>
);
};