diff --git a/app/page.js b/app/page.js index 3f9faa1..c4da25b 100644 --- a/app/page.js +++ b/app/page.js @@ -26,12 +26,8 @@ function HomeInner() { return ( <> -
- -
- -
-
+ +
diff --git a/components/Navbar.js b/components/Navbar.js index 6937f8b..a341702 100644 --- a/components/Navbar.js +++ b/components/Navbar.js @@ -1,10 +1,11 @@ 'use client'; -// 导航栏:论坛名、自定义页面链接、登录/注册、用户菜单、主题切换 +// 导航栏:论坛名、自定义页面链接、登录/注册、私信、用户菜单、主题切换 import { useState, useEffect } from 'react'; import { useApp } from './AppProvider'; import { Icon } from './Icons'; import { API } from '@/lib/api'; +import { DMButton } from './chat/ChatManager'; export default function Navbar({ onOpenModal }) { const { currentUser, forumName, theme, toggleTheme, navigate, openCustomPage, logout } = useApp(); @@ -59,6 +60,7 @@ export default function Navbar({ onOpenModal }) {
+
{!currentUser ? (
diff --git a/components/chat/ChatManager.js b/components/chat/ChatManager.js index 6cb45ee..4b971e6 100644 --- a/components/chat/ChatManager.js +++ b/components/chat/ChatManager.js @@ -1,38 +1,55 @@ 'use client'; -// 私信管理器:会话列表 modal + 聊天窗口 modal + 未读 badge + openPrivateChat +// 私信管理器:会话列表 modal + 聊天窗口 modal + 未读 badge import { useState, useEffect, useCallback } from 'react'; import { API } from '@/lib/api'; import { useApp } from '../AppProvider'; import ConversationList from './ConversationList'; import ChatWindow from './ChatWindow'; -export default function ChatManager() { +// 私信按钮(含未读 badge),渲染在导航栏内 +export function DMButton() { const { currentUser } = useApp(); - const [listOpen, setListOpen] = useState(false); - const [chat, setChat] = useState(null); // {conversationId, otherUserId, otherUsername} const [unread, setUnread] = useState(0); - const [refreshKey, setRefreshKey] = useState(0); - const refreshList = useCallback(() => setRefreshKey((k) => k + 1), []); - - // 未读 badge 轮询(30 秒 + 每次刷新) useEffect(() => { - if (!currentUser) return; + if (!currentUser) { setUnread(0); return; } const update = () => { API.getConversations() - .then((cs) => { - const total = (cs || []).reduce((s, c) => s + (c.unread_count || 0), 0); - setUnread(total); - }) + .then((cs) => setUnread((cs || []).reduce((s, c) => s + (c.unread_count || 0), 0))) .catch(() => {}); }; update(); const t = setInterval(update, 30000); return () => clearInterval(t); - }, [currentUser, refreshKey]); + }, [currentUser]); - // 监听其他组件发起的打开私信事件 + return ( + + ); +} + +// 私信 modals 容器(会话列表 + 聊天窗口) +export default function ChatManager() { + const [listOpen, setListOpen] = useState(false); + const [chat, setChat] = useState(null); // {conversationId, otherUserId, otherUsername} + const [refreshKey, setRefreshKey] = useState(0); + + const refreshList = useCallback(() => setRefreshKey((k) => k + 1), []); + + // 监听事件:打开会话列表 / 打开聊天 useEffect(() => { const handler = (e) => { setChat(e.detail); @@ -59,21 +76,6 @@ export default function ChatManager() { return ( <> - {/* 导航栏私信按钮 + 未读 badge */} - - {listOpen && (