import React, { useState, useRef, useEffect } from "react"; import { Modal, Button, Tabs, Empty, message } from "antd"; import type { TabsProps } from "antd"; import axios from "axios"; import { MESSAGE_STATUS, MESSAGE_TAB } from "@/constants"; import { useNotificationStore } from "@/store/NotificationStore"; import { Eraser, FileText } from "lucide-react"; import dayjs from "dayjs"; import { renderNoticeLength } from "@/utils/utils"; interface MessageModalProps { visible: boolean; onClose: () => void; fetchNoticeList: (id?: number, isRead?: number) => Promise; } const MessageModal: React.FC = ({ visible, onClose, fetchNoticeList, }) => { const [activeKey, setActiveKey] = useState(MESSAGE_TAB.UNREAD); const { notifications, changeNotification } = useNotificationStore(); const [loading, setLoading] = useState(false); const [hasMore, setHasMore] = useState(true); // 新增状态,用于标记是否还有更多数据 const observerRef = useRef(null); const unreadNotifications = notifications.filter( (notification) => notification.is_read === MESSAGE_STATUS.UNREAD ); // 根据activeKey获取对应的isRead值 const getIsReadStatus = (tabKey: string) => { switch (tabKey) { case MESSAGE_TAB.UNREAD: return MESSAGE_STATUS.UNREAD; case MESSAGE_TAB.HAS_READ: return MESSAGE_STATUS.HAS_READ; default: return undefined; // 全部消息时不传isRead参数 } }; useEffect(() => { const observer = new IntersectionObserver( async (entries) => { const target = entries[0]; if (target.isIntersecting && !loading && hasMore) { const filteredNotifications = getFilteredNotifications(); if (filteredNotifications.length > 0) { setLoading(true); const lastId = filteredNotifications[filteredNotifications.length - 1].id; try { const isRead = getIsReadStatus(activeKey); const newMessages = await fetchNoticeList(lastId, isRead); if (newMessages.length === 0) { setHasMore(false); setLoading(false); return; } } catch (error) { console.error("Failed to fetch more messages:", error); } setLoading(false); } } }, { threshold: 0.1, } ); if (observerRef.current) { observer.observe(observerRef.current); } return () => observer.disconnect(); }, [loading, fetchNoticeList, activeKey, hasMore]); // 添加hasMore作为依赖 // 获取过滤后的通知列表 const getFilteredNotifications = () => { let filteredNotifications = notifications; if (activeKey === MESSAGE_TAB.UNREAD) { filteredNotifications = notifications.filter( (notification) => notification.is_read === MESSAGE_STATUS.UNREAD ); } else if (activeKey === MESSAGE_TAB.HAS_READ) { filteredNotifications = notifications.filter( (notification) => notification.is_read === MESSAGE_STATUS.HAS_READ ); } return filteredNotifications; }; const tabItems: TabsProps["items"] = [ { key: MESSAGE_TAB.ALL, label: 全部, }, { key: MESSAGE_TAB.UNREAD, label: ( 未读 {!!unreadNotifications.length && ( {renderNoticeLength(unreadNotifications)} )} ), }, { key: MESSAGE_TAB.HAS_READ, label: 已读, }, ]; // 简化 Tab 切换处理函数 const handleTabChange = (key: string) => { setActiveKey(key); setHasMore(true); // 切换tab时重置hasMore状态 }; return ( 消息中心 {!!renderNoticeLength(unreadNotifications) && `(${renderNoticeLength(unreadNotifications)})`}
{ const baseUrl = import.meta.env.MODE === "development" ? "/notification" : import.meta.env.VITE_NOTIFICATION_URL; const res = await axios.post(`${baseUrl}/bazb/clearMsg`); if (res.data.code === 200) { changeNotification({ type: "clear" }); } }} > 清除消息
} open={visible} onCancel={onClose} footer={null} style={{ top: 30, maxWidth: "90%", maxHeight: "80%", }} >
{(() => { const filteredNotifications = getFilteredNotifications(); return ( <> {filteredNotifications.length === 0 ? (
) : ( <> {filteredNotifications.map((message, index) => (
病例时效提醒
{dayjs(message.push_time).format( "YYYY-MM-DD HH:mm:ss" )}

{message.title}

{message.content}

{message.footer || "祝您工作顺利!"}

))} {loading && (
加载中...
)} {hasMore && (
)} )} ); })()}
); }; export default MessageModal;