优化SidebarMenu组件的内容渲染逻辑,使用useRef缓存选项卡内容以提高性能。更新联系人状态管理,添加搜索关键词的防抖处理,确保搜索请求不会频繁触发。引入当前用户ID的检查以优化搜索结果的处理。
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useEffect } from "react";
|
||||
import React, { useState, useEffect, useRef } from "react";
|
||||
import { Input, Skeleton, Button, Dropdown, MenuProps } from "antd";
|
||||
import {
|
||||
SearchOutlined,
|
||||
@@ -193,6 +193,27 @@ const SidebarMenu: React.FC<SidebarMenuProps> = ({ loading = false }) => {
|
||||
</div>
|
||||
);
|
||||
|
||||
const tabContentCacheRef = useRef<Record<string, React.ReactNode>>({});
|
||||
|
||||
const getTabContent = (tabKey: string) => {
|
||||
if (!tabContentCacheRef.current[tabKey]) {
|
||||
switch (tabKey) {
|
||||
case "chats":
|
||||
tabContentCacheRef.current[tabKey] = <MessageList />;
|
||||
break;
|
||||
case "contracts":
|
||||
tabContentCacheRef.current[tabKey] = <WechatFriends />;
|
||||
break;
|
||||
case "friendsCicle":
|
||||
tabContentCacheRef.current[tabKey] = <FriendsCircle />;
|
||||
break;
|
||||
default:
|
||||
tabContentCacheRef.current[tabKey] = null;
|
||||
}
|
||||
}
|
||||
return tabContentCacheRef.current[tabKey];
|
||||
};
|
||||
|
||||
// 渲染内容部分
|
||||
const renderContent = () => {
|
||||
// 如果正在切换tab到聊天,显示骨架屏
|
||||
@@ -200,16 +221,27 @@ const SidebarMenu: React.FC<SidebarMenuProps> = ({ loading = false }) => {
|
||||
return renderSkeleton();
|
||||
}
|
||||
|
||||
switch (activeTab) {
|
||||
case "chats":
|
||||
return <MessageList />;
|
||||
case "contracts":
|
||||
return <WechatFriends />;
|
||||
case "friendsCicle":
|
||||
return <FriendsCircle />;
|
||||
default:
|
||||
return null;
|
||||
const availableTabs = ["chats", "contracts"];
|
||||
if (currentCustomer && currentCustomer.id !== 0) {
|
||||
availableTabs.push("friendsCicle");
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
{availableTabs.map(tabKey => (
|
||||
<div
|
||||
key={tabKey}
|
||||
style={{
|
||||
display: activeTab === tabKey ? "block" : "none",
|
||||
height: "100%",
|
||||
}}
|
||||
aria-hidden={activeTab !== tabKey}
|
||||
>
|
||||
{getTabContent(tabKey)}
|
||||
</div>
|
||||
))}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
|
||||
@@ -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 });
|
||||
|
||||
Reference in New Issue
Block a user