feat: 添加聊天会话页面,支持会话ID和标题显示,优化会话列表和搜索功能
This commit is contained in:
@@ -120,10 +120,19 @@ function toViewModel(item: OpenIMConversationItem): ConversationItem {
|
||||
export function useConversationList() {
|
||||
const [conversations, setConversations] = useState<OpenIMConversationItem[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [isRefreshing, setIsRefreshing] = useState(false);
|
||||
const [isLoggedIn, setIsLoggedIn] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const reload = useCallback(async () => {
|
||||
const loadConversations = useCallback(async (mode: "initial" | "refresh" | "silent") => {
|
||||
const startedAt = Date.now();
|
||||
if (mode === "initial") {
|
||||
setIsLoading(true);
|
||||
}
|
||||
if (mode === "refresh") {
|
||||
setIsRefreshing(true);
|
||||
}
|
||||
|
||||
try {
|
||||
setError(null);
|
||||
const status = await OpenIMSDK.getLoginStatus();
|
||||
@@ -141,13 +150,26 @@ export function useConversationList() {
|
||||
const message = err instanceof Error ? err.message : "拉取会话失败";
|
||||
setError(message);
|
||||
} finally {
|
||||
if (mode === "refresh") {
|
||||
const elapsed = Date.now() - startedAt;
|
||||
const minVisibleMs = 420;
|
||||
if (elapsed < minVisibleMs) {
|
||||
await new Promise((resolve) => setTimeout(resolve, minVisibleMs - elapsed));
|
||||
}
|
||||
}
|
||||
|
||||
setIsLoading(false);
|
||||
setIsRefreshing(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const reload = useCallback(async () => {
|
||||
await loadConversations("refresh");
|
||||
}, [loadConversations]);
|
||||
|
||||
useEffect(() => {
|
||||
reload();
|
||||
}, [reload]);
|
||||
loadConversations("initial");
|
||||
}, [loadConversations]);
|
||||
|
||||
useEffect(() => {
|
||||
const onConversationChanged = (changed: OpenIMConversationItem[]) => {
|
||||
@@ -159,7 +181,7 @@ export function useConversationList() {
|
||||
};
|
||||
|
||||
const onRecvNewMessages = () => {
|
||||
reload();
|
||||
loadConversations("silent");
|
||||
};
|
||||
|
||||
OpenIMSDK.on(OpenIMEvent.OnConversationChanged, onConversationChanged);
|
||||
@@ -171,13 +193,14 @@ export function useConversationList() {
|
||||
OpenIMSDK.off(OpenIMEvent.OnNewConversation, onNewConversation);
|
||||
OpenIMSDK.off(OpenIMEvent.OnRecvNewMessages, onRecvNewMessages);
|
||||
};
|
||||
}, [reload]);
|
||||
}, [loadConversations]);
|
||||
|
||||
const items = useMemo(() => conversations.map(toViewModel), [conversations]);
|
||||
|
||||
return {
|
||||
items,
|
||||
isLoading,
|
||||
isRefreshing,
|
||||
isLoggedIn,
|
||||
error,
|
||||
reload,
|
||||
|
||||
Reference in New Issue
Block a user