'use client';
// 私信管理器:会话列表 modal + 聊天窗口 modal + 未读 badge
import { useState, useEffect, useCallback } from 'react';
import { API } from '@/lib/api';
import { useApp } from '../AppProvider';
import { Icon } from '../Icons';
import ConversationList from './ConversationList';
import ChatWindow from './ChatWindow';
// 私信按钮(含未读 badge),渲染在导航栏内
export function DMButton() {
const { currentUser } = useApp();
const [unread, setUnread] = useState(0);
useEffect(() => {
if (!currentUser) { setUnread(0); return; }
const update = () => {
API.getConversations()
.then((cs) => setUnread((cs || []).reduce((s, c) => s + (c.unread_count || 0), 0)))
.catch(() => {});
};
update();
const t = setInterval(update, 30000);
return () => clearInterval(t);
}, [currentUser]);
if (!currentUser) return null; // 未登录不显示私信按钮
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);
setListOpen(false);
};
const openList = () => setListOpen(true);
window.addEventListener('forumlify-open-chat', handler);
window.addEventListener('forumlify-open-messages', openList);
return () => {
window.removeEventListener('forumlify-open-chat', handler);
window.removeEventListener('forumlify-open-messages', openList);
};
}, []);
const openChat = (conversationId, otherUserId, otherUsername) => {
setChat({ conversationId, otherUserId, otherUsername });
setListOpen(false);
};
const closeChat = () => {
setChat(null);
refreshList();
};
return (
<>
{listOpen && (
setListOpen(false)}
/>
)}
{chat && (
)}
>
);
}
// 供其他组件调用的打开私信(通过自定义事件桥接)
export function useOpenPrivateChat() {
const { currentUser } = useApp();
const open = async (otherUserId, otherUsername) => {
if (!currentUser) { alert('请先登录'); return; }
try {
const result = await API.getOrCreateConversation(otherUserId);
window.dispatchEvent(new CustomEvent('forumlify-open-chat', {
detail: { conversationId: result.id, otherUserId, otherUsername },
}));
} catch (err) {
alert('打开私信失败:' + err.message);
}
};
return open;
}