import { useState, useEffect } from 'react' import { Layout, Badge, Avatar, Dropdown, Space, Popover, List, Tabs, Button, Empty, Typography, Segmented, Tooltip } from 'antd' import { useNavigate } from 'react-router-dom' import { MenuFoldOutlined, MenuUnfoldOutlined, BellOutlined, ProjectOutlined, TeamOutlined, NotificationOutlined, MoonOutlined, SunOutlined, GlobalOutlined } from '@ant-design/icons' import useUserStore from '@/stores/userStore' import useNotificationStore from '@/stores/notificationStore' import useThemeStore from '@/stores/themeStore' import { getNotifications, getUnreadCount, markAsRead, markAllAsRead } from '@/api/notification' import Toast from '@/components/Toast/Toast' import './AppHeader.css' const { Header } = Layout const { Text } = Typography function AppHeader({ collapsed, onToggle, showLogo = true }) { const navigate = useNavigate() const { user } = useUserStore() const { unreadCount, fetchUnreadCount, decrementUnreadCount, resetUnreadCount } = useNotificationStore() const { isDarkMode, toggleTheme } = useThemeStore() const [notifications, setNotifications] = useState([]) const [loading, setLoading] = useState(false) const [popoverVisible, setPopoverVisible] = useState(false) const [lang, setLang] = useState('zh') useEffect(() => { if (user) { fetchUnreadCount() const timer = setInterval(fetchUnreadCount, 120000) return () => clearInterval(timer) } }, [user]) const fetchNotifications = async () => { setLoading(true) try { const res = await getNotifications({ page: 1, page_size: 5 }) setNotifications(res.data || []) } catch (error) { console.error('Fetch notifications error:', error) } finally { setLoading(false) } } const handleMarkRead = async (id) => { try { await markAsRead(id) setNotifications(notifications.map(n => n.id === id ? { ...n, is_read: true } : n)) decrementUnreadCount() } catch (error) { console.error('Mark read error:', error) } } const handleMarkAllRead = async () => { try { await markAllAsRead() setNotifications(notifications.map(n => ({ ...n, is_read: true }))) resetUnreadCount() Toast.success('操作成功', '所有通知已标记为已读') } catch (error) { console.error('Mark all read error:', error) } } const handleNotificationClick = (n) => { if (!n.is_read) { handleMarkRead(n.id) } if (n.link) { navigate(n.link) setPopoverVisible(false) } } const getCategoryIcon = (category) => { switch (category) { case 'project': return case 'collaboration': return default: return } } const notificationContent = (
消息通知 {unreadCount > 0 && ( )}
}} renderItem={(item) => ( handleNotificationClick(item)} > } title={{item.title}} description={
{item.content}
{new Date(item.created_at).toLocaleString('zh-CN')}
} />
)} />
) return (
{/* 左侧:Logo + 折叠按钮 */} {showLogo && (
{/* Logo 区域 */}
logo

NexDocus

{/* 折叠按钮 */}
{collapsed ? : }
)} {!showLogo &&
} {/* Spacer if left is empty */} {/* 右侧:功能按钮 */}
{/* 1. 主题切换 */}
{isDarkMode ? : }
{/* 2. 语言切换 */} {/* 3. 消息通知 */} { setPopoverVisible(visible) if (visible) { fetchNotifications() } }} placement="bottomRight" overlayClassName="header-notification-popover" >
) } export default AppHeader