优化SidebarMenu组件的内容渲染逻辑,使用useRef缓存选项卡内容以提高性能。更新联系人状态管理,添加搜索关键词的防抖处理,确保搜索请求不会频繁触发。引入当前用户ID的检查以优化搜索结果的处理。

This commit is contained in:
2025-11-14 16:19:18 +08:00
parent 01bf7ee271
commit eca5549592
2 changed files with 63 additions and 12 deletions

View File

@@ -3,6 +3,10 @@ import { persist } from "zustand/middleware";
import { ContactGroupByLabel } from "@/pages/pc/ckbox/data";
import { Contact } from "@/utils/db";
import { ContactManager } from "@/utils/dbAction";
import { useUserStore } from "@/store/module/user";
const SEARCH_DEBOUNCE_DELAY = 300;
let searchDebounceTimer: ReturnType<typeof setTimeout> | null = null;
/**
* 联系人状态管理接口
@@ -171,8 +175,16 @@ export const useContactStore = create<ContactState>()(
setSearchKeyword: (keyword: string) => {
set({ searchKeyword: keyword });
if (searchDebounceTimer) {
clearTimeout(searchDebounceTimer);
searchDebounceTimer = null;
}
if (keyword.trim()) {
get().searchContacts(keyword);
searchDebounceTimer = setTimeout(() => {
get().searchContacts(keyword);
}, SEARCH_DEBOUNCE_DELAY);
} else {
set({ isSearchMode: false, searchResults: [] });
}
@@ -204,8 +216,15 @@ export const useContactStore = create<ContactState>()(
set({ loading: true, isSearchMode: true });
try {
const currentUserId = useUserStore.getState().user?.id;
if (!currentUserId) {
set({ searchResults: [], isSearchMode: false, loading: false });
return;
}
const results = await ContactManager.searchContacts(
get().currentContact?.userId || 0,
currentUserId,
keyword,
);
set({ searchResults: results });