From d729be9177dbb9f404cf651ce8fff83ccb35eeaa Mon Sep 17 00:00:00 2001 From: mtvpls Date: Sun, 7 Dec 2025 21:30:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=81=8A=E5=A4=A9=E6=B0=94?= =?UTF-8?q?=E6=B3=A1=E6=9C=AA=E8=AF=BB=E8=AE=A1=E6=95=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 1 + .../watch-room/ChatFloatingWindow.tsx | 48 +++++++++++++++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index ffd0204..264781e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -57,6 +57,7 @@ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static # 安装 Socket.IO 相关依赖(standalone 模式不会自动包含) +RUN rm /app/package.json RUN pnpm add socket.io@^4.8.1 socket.io-client@^4.8.1 --prod # 切换到非特权用户 diff --git a/src/components/watch-room/ChatFloatingWindow.tsx b/src/components/watch-room/ChatFloatingWindow.tsx index f881923..5313f6d 100644 --- a/src/components/watch-room/ChatFloatingWindow.tsx +++ b/src/components/watch-room/ChatFloatingWindow.tsx @@ -17,6 +17,28 @@ export default function ChatFloatingWindow() { const [unreadCount, setUnreadCount] = useState(0); const messagesEndRef = useRef(null); const lastMessageCountRef = useRef(0); + const isOpenRef = useRef(isOpen); + const isMinimizedRef = useRef(isMinimized); + const currentRoomIdRef = useRef(null); + + // 当房间变化时重置状态 + useEffect(() => { + const roomId = watchRoom?.currentRoom?.id || null; + if (roomId !== currentRoomIdRef.current) { + console.log('[ChatFloatingWindow] Room changed:', currentRoomIdRef.current, '->', roomId); + currentRoomIdRef.current = roomId; + lastMessageCountRef.current = 0; + setUnreadCount(0); + setIsOpen(false); + setIsMinimized(false); + } + }, [watchRoom?.currentRoom?.id]); + + // 同步窗口状态到 ref + useEffect(() => { + isOpenRef.current = isOpen; + isMinimizedRef.current = isMinimized; + }, [isOpen, isMinimized]); // 自动滚动到底部 useEffect(() => { @@ -27,18 +49,36 @@ export default function ChatFloatingWindow() { // 跟踪未读消息数量 useEffect(() => { - if (!watchRoom?.chatMessages) return; + if (!watchRoom?.chatMessages) { + lastMessageCountRef.current = 0; + return; + } const currentCount = watchRoom.chatMessages.length; + + // 如果消息数量减少了(比如切换房间),重置计数器和未读数 + if (currentCount < lastMessageCountRef.current) { + lastMessageCountRef.current = currentCount; + setUnreadCount(0); + return; + } + if (currentCount > lastMessageCountRef.current) { // 有新消息 - if (!isOpen && !isMinimized) { + const newMessageCount = currentCount - lastMessageCountRef.current; + console.log('[ChatFloatingWindow] New messages:', newMessageCount, 'isOpen:', isOpenRef.current, 'isMinimized:', isMinimizedRef.current); + + if (!isOpenRef.current && !isMinimizedRef.current) { // 只有在聊天窗口完全关闭时才增加未读计数 - setUnreadCount(prev => prev + (currentCount - lastMessageCountRef.current)); + setUnreadCount(prev => { + const newCount = prev + newMessageCount; + console.log('[ChatFloatingWindow] Updating unread count:', prev, '->', newCount); + return newCount; + }); } } lastMessageCountRef.current = currentCount; - }, [watchRoom?.chatMessages, isOpen, isMinimized]); + }, [watchRoom?.chatMessages]); // 打开聊天窗口时清空未读计数 useEffect(() => {