2026-03-31 18:22:24 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 消息模块 React Hooks
|
|
|
|
|
|
*
|
|
|
|
|
|
* 提供React组件与消息状态管理的集成
|
2026-03-31 19:15:13 +08:00
|
|
|
|
* 所有hooks都基于zustand store的selector机制
|
2026-03-31 18:22:24 +08:00
|
|
|
|
* 确保组件能实时获取状态更新
|
|
|
|
|
|
*
|
2026-03-31 19:15:13 +08:00
|
|
|
|
* 重构说明:
|
|
|
|
|
|
* - 移除了 SubscriptionManager,改用 Zustand selector
|
|
|
|
|
|
* - Zustand 会自动处理依赖追踪和组件重渲染
|
|
|
|
|
|
* - 不需要手动管理订阅/取消订阅
|
2026-03-31 18:22:24 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import { useState, useEffect, useCallback, useRef } from 'react';
|
2026-03-31 19:15:13 +08:00
|
|
|
|
import { useShallow } from 'zustand/react/shallow';
|
2026-03-31 18:22:24 +08:00
|
|
|
|
import { ConversationResponse, MessageResponse, MessageSegment } from '../../types/dto';
|
2026-03-31 19:15:13 +08:00
|
|
|
|
import { useMessageStore, normalizeConversationId } from './store';
|
2026-03-31 18:22:24 +08:00
|
|
|
|
|
|
|
|
|
|
// ==================== useConversations - 获取会话列表 ====================
|
|
|
|
|
|
|
|
|
|
|
|
interface UseConversationsReturn {
|
|
|
|
|
|
conversations: ConversationResponse[];
|
|
|
|
|
|
isLoading: boolean;
|
|
|
|
|
|
refresh: () => Promise<void>;
|
|
|
|
|
|
loadMore: () => Promise<void>;
|
|
|
|
|
|
hasMore: boolean;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取会话列表
|
|
|
|
|
|
* 用于 MessageListScreen 等需要显示会话列表的组件
|
2026-03-31 19:15:13 +08:00
|
|
|
|
* 使用 Zustand selector 自动订阅状态变化
|
2026-03-31 18:22:24 +08:00
|
|
|
|
*/
|
|
|
|
|
|
export function useConversations(
|
|
|
|
|
|
fetchConversations: (forceRefresh: boolean, source: string) => Promise<void>,
|
|
|
|
|
|
loadMoreConversations: () => Promise<void>,
|
|
|
|
|
|
canLoadMore: () => boolean,
|
|
|
|
|
|
initialize: () => Promise<void>
|
|
|
|
|
|
): UseConversationsReturn {
|
2026-03-31 19:15:13 +08:00
|
|
|
|
// 使用 Zustand selector 直接订阅状态
|
|
|
|
|
|
const conversations = useMessageStore(state => state.conversationList);
|
|
|
|
|
|
const isLoading = useMessageStore(state => state.isLoadingConversations);
|
2026-03-31 18:22:24 +08:00
|
|
|
|
const [hasMore, setHasMore] = useState(() => canLoadMore());
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
// 初始化
|
|
|
|
|
|
initialize().catch(error => {
|
|
|
|
|
|
console.error('[useConversations] 初始化失败:', error);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 冷启动兜底:延迟做一次强制刷新
|
|
|
|
|
|
const coldStartSyncTimer = setTimeout(() => {
|
|
|
|
|
|
fetchConversations(true, 'hooks-initial-refresh').catch(error => {
|
|
|
|
|
|
console.error('[useConversations] 冷启动强制刷新失败:', error);
|
|
|
|
|
|
});
|
|
|
|
|
|
}, 1200);
|
|
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
clearTimeout(coldStartSyncTimer);
|
|
|
|
|
|
};
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
2026-03-31 19:15:13 +08:00
|
|
|
|
// 监听 hasMore 变化
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
setHasMore(canLoadMore());
|
|
|
|
|
|
}, [conversations, canLoadMore]);
|
|
|
|
|
|
|
2026-03-31 18:22:24 +08:00
|
|
|
|
const refresh = useCallback(async () => {
|
|
|
|
|
|
await fetchConversations(true, 'hooks-manual-refresh');
|
|
|
|
|
|
}, [fetchConversations]);
|
|
|
|
|
|
|
|
|
|
|
|
const loadMore = useCallback(async () => {
|
|
|
|
|
|
await loadMoreConversations();
|
|
|
|
|
|
}, [loadMoreConversations]);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
conversations,
|
|
|
|
|
|
isLoading,
|
|
|
|
|
|
refresh,
|
|
|
|
|
|
loadMore,
|
|
|
|
|
|
hasMore,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== useMessages - 获取指定会话的消息 ====================
|
|
|
|
|
|
|
|
|
|
|
|
interface UseMessagesReturn {
|
|
|
|
|
|
messages: MessageResponse[];
|
|
|
|
|
|
isLoading: boolean;
|
|
|
|
|
|
hasMore: boolean;
|
|
|
|
|
|
loadMore: () => Promise<void>;
|
|
|
|
|
|
refresh: () => Promise<void>;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取指定会话的消息
|
2026-03-31 19:15:13 +08:00
|
|
|
|
* 使用 Zustand selector 自动订阅消息变化
|
2026-03-31 18:22:24 +08:00
|
|
|
|
*/
|
|
|
|
|
|
export function useMessages(
|
|
|
|
|
|
conversationId: string,
|
|
|
|
|
|
fetchMessages: (conversationId: string) => Promise<void>,
|
|
|
|
|
|
loadMoreMessages: (conversationId: string, beforeSeq: number, limit?: number) => Promise<MessageResponse[]>
|
|
|
|
|
|
): UseMessagesReturn {
|
|
|
|
|
|
const normalizedConversationId = normalizeConversationId(conversationId);
|
2026-03-31 19:15:13 +08:00
|
|
|
|
const store = useMessageStore();
|
2026-03-31 18:22:24 +08:00
|
|
|
|
|
2026-03-31 19:15:13 +08:00
|
|
|
|
// 使用 useShallow 避免每次返回新数组引用导致的无限循环
|
|
|
|
|
|
const messages = useMessageStore(
|
|
|
|
|
|
useShallow(state => state.messagesMap.get(normalizedConversationId) || [])
|
2026-03-31 18:22:24 +08:00
|
|
|
|
);
|
2026-03-31 19:15:13 +08:00
|
|
|
|
const isLoadingMessages = useMessageStore(state => state.loadingMessagesSet.has(normalizedConversationId));
|
2026-03-31 18:22:24 +08:00
|
|
|
|
const [hasMore, setHasMore] = useState(true);
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
// 激活会话
|
|
|
|
|
|
fetchMessages(normalizedConversationId).catch(error => {
|
|
|
|
|
|
console.error('[useMessages] 激活会话失败:', error);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
// 清理活动会话
|
|
|
|
|
|
if (store.getActiveConversation() === normalizedConversationId) {
|
|
|
|
|
|
store.setCurrentConversation(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}, [normalizedConversationId]);
|
|
|
|
|
|
|
|
|
|
|
|
const loadMore = useCallback(async () => {
|
2026-03-31 19:15:13 +08:00
|
|
|
|
const currentMessages = messages;
|
2026-03-31 18:22:24 +08:00
|
|
|
|
if (currentMessages.length === 0) return;
|
|
|
|
|
|
|
|
|
|
|
|
// 消息数组在 store 内保持 seq 升序,首项即最早消息
|
|
|
|
|
|
const minSeq = currentMessages[0]?.seq;
|
|
|
|
|
|
if (minSeq === undefined) return;
|
|
|
|
|
|
|
|
|
|
|
|
const loadedMessages = await loadMoreMessages(conversationId, minSeq, 20);
|
|
|
|
|
|
setHasMore(loadedMessages.length > 0);
|
2026-03-31 19:15:13 +08:00
|
|
|
|
}, [conversationId, messages, loadMoreMessages]);
|
2026-03-31 18:22:24 +08:00
|
|
|
|
|
|
|
|
|
|
const refresh = useCallback(async () => {
|
|
|
|
|
|
await fetchMessages(conversationId);
|
|
|
|
|
|
}, [conversationId, fetchMessages]);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
messages,
|
2026-03-31 19:15:13 +08:00
|
|
|
|
isLoading: isLoadingMessages,
|
2026-03-31 18:22:24 +08:00
|
|
|
|
hasMore,
|
|
|
|
|
|
loadMore,
|
|
|
|
|
|
refresh,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== useSendMessage - 发送消息 ====================
|
|
|
|
|
|
|
|
|
|
|
|
interface UseSendMessageReturn {
|
|
|
|
|
|
sendMessage: (segments: MessageSegment[], options?: { replyToId?: string }) => Promise<MessageResponse | null>;
|
|
|
|
|
|
isSending: boolean;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 发送消息
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function useSendMessage(
|
|
|
|
|
|
conversationId: string,
|
|
|
|
|
|
sendMessageService: (conversationId: string, segments: MessageSegment[], options?: { replyToId?: string }) => Promise<MessageResponse | null>
|
|
|
|
|
|
): UseSendMessageReturn {
|
|
|
|
|
|
const [isSending, setIsSending] = useState(false);
|
|
|
|
|
|
|
|
|
|
|
|
const sendMessage = useCallback(async (segments: MessageSegment[], options?: { replyToId?: string }) => {
|
|
|
|
|
|
setIsSending(true);
|
|
|
|
|
|
try {
|
|
|
|
|
|
const message = await sendMessageService(conversationId, segments, options);
|
|
|
|
|
|
return message;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('[useSendMessage] 发送消息失败:', error);
|
|
|
|
|
|
return null;
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setIsSending(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [conversationId, sendMessageService]);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
sendMessage,
|
|
|
|
|
|
isSending,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== useMarkAsRead - 标记已读 ====================
|
|
|
|
|
|
|
|
|
|
|
|
interface UseMarkAsReadReturn {
|
|
|
|
|
|
markAsRead: (seq: number) => Promise<void>;
|
|
|
|
|
|
markAllAsRead: () => Promise<void>;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 标记已读
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function useMarkAsRead(
|
|
|
|
|
|
conversationId: string,
|
|
|
|
|
|
markAsReadService: (conversationId: string, seq: number) => Promise<void>,
|
|
|
|
|
|
markAllAsReadService: () => Promise<void>
|
|
|
|
|
|
): UseMarkAsReadReturn {
|
|
|
|
|
|
const markAsRead = useCallback(async (seq: number) => {
|
|
|
|
|
|
await markAsReadService(conversationId, seq);
|
|
|
|
|
|
}, [conversationId, markAsReadService]);
|
|
|
|
|
|
|
|
|
|
|
|
const markAllAsRead = useCallback(async () => {
|
|
|
|
|
|
await markAllAsReadService();
|
|
|
|
|
|
}, [markAllAsReadService]);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
markAsRead,
|
|
|
|
|
|
markAllAsRead,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== useUnreadCount - 获取未读数 ====================
|
|
|
|
|
|
|
|
|
|
|
|
interface UseUnreadCountReturn {
|
|
|
|
|
|
totalUnreadCount: number;
|
|
|
|
|
|
systemUnreadCount: number;
|
|
|
|
|
|
refresh: () => Promise<void>;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取未读数
|
2026-03-31 19:15:13 +08:00
|
|
|
|
* 使用 Zustand selector 直接订阅未读数变化
|
2026-03-31 18:22:24 +08:00
|
|
|
|
*/
|
|
|
|
|
|
export function useUnreadCount(fetchUnreadCount: () => Promise<void>): UseUnreadCountReturn {
|
2026-03-31 19:15:13 +08:00
|
|
|
|
// 使用 Zustand selector 直接订阅未读数
|
|
|
|
|
|
const totalUnreadCount = useMessageStore(state => state.totalUnreadCount);
|
|
|
|
|
|
const systemUnreadCount = useMessageStore(state => state.systemUnreadCount);
|
2026-03-31 18:22:24 +08:00
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
// 初始获取
|
|
|
|
|
|
fetchUnreadCount();
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
const refresh = useCallback(async () => {
|
|
|
|
|
|
await fetchUnreadCount();
|
|
|
|
|
|
}, [fetchUnreadCount]);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
2026-03-31 19:15:13 +08:00
|
|
|
|
totalUnreadCount,
|
|
|
|
|
|
systemUnreadCount,
|
2026-03-31 18:22:24 +08:00
|
|
|
|
refresh,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== useSystemUnreadCount - 获取系统消息未读数 ====================
|
|
|
|
|
|
|
|
|
|
|
|
interface UseSystemUnreadCountReturn {
|
|
|
|
|
|
systemUnreadCount: number;
|
|
|
|
|
|
setSystemUnreadCount: (count: number) => void;
|
|
|
|
|
|
incrementSystemUnreadCount: () => void;
|
|
|
|
|
|
decrementSystemUnreadCount: (count?: number) => void;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取系统消息未读数
|
2026-03-31 19:15:13 +08:00
|
|
|
|
* 使用 Zustand selector 直接订阅
|
2026-03-31 18:22:24 +08:00
|
|
|
|
*/
|
|
|
|
|
|
export function useSystemUnreadCount(
|
|
|
|
|
|
setSystemUnreadCountService: (count: number) => void,
|
|
|
|
|
|
incrementSystemUnreadCountService: () => void,
|
|
|
|
|
|
decrementSystemUnreadCountService: (count?: number) => void
|
|
|
|
|
|
): UseSystemUnreadCountReturn {
|
2026-03-31 19:15:13 +08:00
|
|
|
|
// 使用 Zustand selector 直接订阅
|
|
|
|
|
|
const systemUnreadCount = useMessageStore(state => state.systemUnreadCount);
|
2026-03-31 18:22:24 +08:00
|
|
|
|
|
|
|
|
|
|
const setSystemUnreadCount = useCallback((count: number) => {
|
|
|
|
|
|
setSystemUnreadCountService(count);
|
|
|
|
|
|
}, [setSystemUnreadCountService]);
|
|
|
|
|
|
|
|
|
|
|
|
const incrementSystemUnreadCount = useCallback(() => {
|
|
|
|
|
|
incrementSystemUnreadCountService();
|
|
|
|
|
|
}, [incrementSystemUnreadCountService]);
|
|
|
|
|
|
|
|
|
|
|
|
const decrementSystemUnreadCount = useCallback((count = 1) => {
|
|
|
|
|
|
decrementSystemUnreadCountService(count);
|
|
|
|
|
|
}, [decrementSystemUnreadCountService]);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
systemUnreadCount,
|
|
|
|
|
|
setSystemUnreadCount,
|
|
|
|
|
|
incrementSystemUnreadCount,
|
|
|
|
|
|
decrementSystemUnreadCount,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== useConversation - 获取单个会话 ====================
|
|
|
|
|
|
|
|
|
|
|
|
interface UseConversationReturn {
|
|
|
|
|
|
conversation: ConversationResponse | null;
|
|
|
|
|
|
refresh: () => Promise<void>;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取单个会话
|
2026-03-31 19:15:13 +08:00
|
|
|
|
* 使用 Zustand selector 直接订阅会话变化
|
2026-03-31 18:22:24 +08:00
|
|
|
|
*/
|
|
|
|
|
|
export function useConversation(
|
|
|
|
|
|
conversationId: string,
|
|
|
|
|
|
fetchConversationDetail: (conversationId: string) => Promise<ConversationResponse | null>
|
|
|
|
|
|
): UseConversationReturn {
|
2026-03-31 19:15:13 +08:00
|
|
|
|
// 使用 useShallow 避免每次返回新对象引用导致的无限循环
|
|
|
|
|
|
const conversation = useMessageStore(
|
|
|
|
|
|
useShallow(state => state.conversations.get(conversationId) || null)
|
2026-03-31 18:22:24 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const refresh = useCallback(async () => {
|
|
|
|
|
|
await fetchConversationDetail(conversationId);
|
|
|
|
|
|
}, [conversationId, fetchConversationDetail]);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
conversation,
|
|
|
|
|
|
refresh,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-31 19:15:13 +08:00
|
|
|
|
// ==================== useMessageManager - 通用状态 ====================
|
2026-03-31 18:22:24 +08:00
|
|
|
|
|
|
|
|
|
|
interface UseMessageManagerReturn {
|
|
|
|
|
|
isConnected: boolean;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-31 19:15:13 +08:00
|
|
|
|
* 通用消息管理状态
|
|
|
|
|
|
* 使用 Zustand selector 直接订阅连接状态
|
2026-03-31 18:22:24 +08:00
|
|
|
|
*/
|
|
|
|
|
|
export function useMessageManager(initialize: () => Promise<void>): UseMessageManagerReturn {
|
2026-03-31 19:15:13 +08:00
|
|
|
|
// 使用 Zustand selector 直接订阅连接状态
|
|
|
|
|
|
const isConnected = useMessageStore(state => state.isWSConnected);
|
2026-03-31 18:22:24 +08:00
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-03-31 19:15:13 +08:00
|
|
|
|
initialize().catch(error => {
|
2026-03-31 18:22:24 +08:00
|
|
|
|
console.error('[useMessageManager] 初始化失败:', error);
|
|
|
|
|
|
});
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
isConnected,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== useCreateConversation - 创建会话 ====================
|
|
|
|
|
|
|
|
|
|
|
|
interface UseCreateConversationReturn {
|
|
|
|
|
|
createConversation: (userId: string) => Promise<ConversationResponse | null>;
|
|
|
|
|
|
isCreating: boolean;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 创建会话
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function useCreateConversation(
|
|
|
|
|
|
createConversationService: (userId: string) => Promise<ConversationResponse | null>
|
|
|
|
|
|
): UseCreateConversationReturn {
|
|
|
|
|
|
const [isCreating, setIsCreating] = useState(false);
|
|
|
|
|
|
|
|
|
|
|
|
const createConversation = useCallback(async (userId: string) => {
|
|
|
|
|
|
setIsCreating(true);
|
|
|
|
|
|
try {
|
|
|
|
|
|
const conversation = await createConversationService(userId);
|
|
|
|
|
|
return conversation;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('[useCreateConversation] 创建会话失败:', error);
|
|
|
|
|
|
return null;
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setIsCreating(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [createConversationService]);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
createConversation,
|
|
|
|
|
|
isCreating,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== useUpdateConversation - 更新会话 ====================
|
|
|
|
|
|
|
|
|
|
|
|
interface UseUpdateConversationReturn {
|
|
|
|
|
|
updateConversation: (conversationId: string, updates: Partial<ConversationResponse>) => void;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 更新会话
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function useUpdateConversation(
|
|
|
|
|
|
updateConversationService: (conversationId: string, updates: Partial<ConversationResponse>) => void
|
|
|
|
|
|
): UseUpdateConversationReturn {
|
|
|
|
|
|
const updateConversation = useCallback((conversationId: string, updates: Partial<ConversationResponse>) => {
|
|
|
|
|
|
updateConversationService(conversationId, updates);
|
|
|
|
|
|
}, [updateConversationService]);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
updateConversation,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== useGroupTyping - 群聊输入状态 ====================
|
|
|
|
|
|
|
|
|
|
|
|
interface UseGroupTypingReturn {
|
|
|
|
|
|
typingUsers: string[];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取群聊输入状态
|
2026-03-31 19:15:13 +08:00
|
|
|
|
* 使用 Zustand selector 直接订阅
|
2026-03-31 18:22:24 +08:00
|
|
|
|
*/
|
|
|
|
|
|
export function useGroupTyping(groupId: string): UseGroupTypingReturn {
|
2026-03-31 19:15:13 +08:00
|
|
|
|
// 使用 useShallow 避免每次返回新数组引用导致的无限循环
|
|
|
|
|
|
const typingUsers = useMessageStore(
|
|
|
|
|
|
useShallow(state => state.typingUsersMap.get(groupId) || [])
|
2026-03-31 18:22:24 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
typingUsers,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== useGroupMuted - 群聊禁言状态 ====================
|
|
|
|
|
|
|
|
|
|
|
|
interface UseGroupMutedReturn {
|
|
|
|
|
|
isMuted: boolean;
|
|
|
|
|
|
setMutedStatus: (muted: boolean) => void;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取群聊禁言状态
|
2026-03-31 19:15:13 +08:00
|
|
|
|
* 使用 Zustand selector 直接订阅
|
2026-03-31 18:22:24 +08:00
|
|
|
|
*/
|
|
|
|
|
|
export function useGroupMuted(groupId: string): UseGroupMutedReturn {
|
2026-03-31 19:15:13 +08:00
|
|
|
|
// 使用 Zustand selector 直接订阅禁言状态
|
|
|
|
|
|
const isMuted = useMessageStore(state => state.mutedStatusMap.get(groupId) || false);
|
2026-03-31 18:22:24 +08:00
|
|
|
|
const store = useMessageStore();
|
|
|
|
|
|
|
|
|
|
|
|
const setMutedStatus = useCallback((muted: boolean) => {
|
|
|
|
|
|
store.setMutedStatus(groupId, muted);
|
2026-03-31 19:15:13 +08:00
|
|
|
|
}, [groupId, store]);
|
2026-03-31 18:22:24 +08:00
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
isMuted,
|
|
|
|
|
|
setMutedStatus,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-31 19:15:13 +08:00
|
|
|
|
// ==================== useConnectionStatus - 连接状态 ====================
|
2026-03-31 18:22:24 +08:00
|
|
|
|
|
2026-03-31 19:15:13 +08:00
|
|
|
|
interface UseConnectionStatusReturn {
|
|
|
|
|
|
isConnected: boolean;
|
2026-03-31 18:22:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-31 19:15:13 +08:00
|
|
|
|
* 获取连接状态
|
|
|
|
|
|
* 使用 Zustand selector 直接订阅
|
2026-03-31 18:22:24 +08:00
|
|
|
|
*/
|
2026-03-31 19:15:13 +08:00
|
|
|
|
export function useConnectionStatus(): UseConnectionStatusReturn {
|
|
|
|
|
|
const isConnected = useMessageStore(state => state.isWSConnected);
|
2026-03-31 18:22:24 +08:00
|
|
|
|
|
2026-03-31 19:15:13 +08:00
|
|
|
|
return {
|
|
|
|
|
|
isConnected,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== useIsInitialized - 初始化状态 ====================
|
|
|
|
|
|
|
|
|
|
|
|
interface UseIsInitializedReturn {
|
|
|
|
|
|
isInitialized: boolean;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取初始化状态
|
|
|
|
|
|
* 使用 Zustand selector 直接订阅
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function useIsInitialized(): UseIsInitializedReturn {
|
|
|
|
|
|
const isInitialized = useMessageStore(state => state.isInitialized);
|
2026-03-31 18:22:24 +08:00
|
|
|
|
|
|
|
|
|
|
return {
|
2026-03-31 19:15:13 +08:00
|
|
|
|
isInitialized,
|
2026-03-31 18:22:24 +08:00
|
|
|
|
};
|
2026-03-31 19:15:13 +08:00
|
|
|
|
}
|