refactor(weChat/ckchat): 优化会话更新逻辑并移除冗余代码
- 修复未读消息计数时可能出现的类型问题 - 直接使用chatSessions状态而非getChatSessions方法 - 简化会话更新逻辑,移除不必要的缓存处理 - 使用更简洁的路径引用deepCopy工具
This commit is contained in:
@@ -11,7 +11,7 @@ interface MessageListProps {}
|
||||
|
||||
const MessageList: React.FC<MessageListProps> = () => {
|
||||
const { setCurrentContact, currentContract } = useWeChatStore();
|
||||
const chatSessions = useCkChatStore(state => state.getChatSessions());
|
||||
const chatSessions = useCkChatStore(state => state.chatSessions);
|
||||
const onContactClick = (session: ContractData | weChatGroup) => {
|
||||
setCurrentContact(session);
|
||||
};
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
} from "@/pages/pc/ckbox/data";
|
||||
import { kfUserService, weChatGroupService, contractService } from "@/utils/db";
|
||||
import { createContractList } from "@/store/module/ckchat/api";
|
||||
import { deepCopy } from "../../../utils/common";
|
||||
import { deepCopy } from "@/utils/common";
|
||||
|
||||
export const useCkChatStore = createPersistStore<CkChatState>(
|
||||
set => ({
|
||||
@@ -373,39 +373,20 @@ export const useCkChatStore = createPersistStore<CkChatState>(
|
||||
: [...state.chatSessions, session as ContractData | weChatGroup],
|
||||
};
|
||||
});
|
||||
// 清除getChatSessions缓存
|
||||
const state = useCkChatStore.getState();
|
||||
if (
|
||||
state.getChatSessions &&
|
||||
typeof state.getChatSessions === "function"
|
||||
) {
|
||||
// 触发缓存重新计算
|
||||
state.getChatSessions();
|
||||
}
|
||||
},
|
||||
// 更新聊天会话
|
||||
updateChatSession: (session: ContractData | weChatGroup) => {
|
||||
const state = deepCopy(useCkChatStore.getState());
|
||||
const newSession = state.chatSessions.map(item =>
|
||||
item.id === session.id ? session : item,
|
||||
);
|
||||
console.log("新数组", newSession);
|
||||
set({ chatSessions: newSession });
|
||||
set(state => ({
|
||||
chatSessions: state.chatSessions.map(item =>
|
||||
item.id === session.id ? { ...item, ...session } : item,
|
||||
),
|
||||
}));
|
||||
},
|
||||
// 删除聊天会话
|
||||
deleteChatSession: (sessionId: string) => {
|
||||
set(state => ({
|
||||
chatSessions: state.chatSessions.filter(item => item.id !== sessionId),
|
||||
}));
|
||||
// 清除getChatSessions缓存
|
||||
const state = useCkChatStore.getState();
|
||||
if (
|
||||
state.getChatSessions &&
|
||||
typeof state.getChatSessions === "function"
|
||||
) {
|
||||
// 触发缓存重新计算
|
||||
state.getChatSessions();
|
||||
}
|
||||
},
|
||||
// 设置用户信息
|
||||
setUserInfo: (userInfo: CkUserInfo) => {
|
||||
|
||||
@@ -88,9 +88,7 @@ export const useWeChatStore = create<WeChatState>()(
|
||||
const session = chatSessions.find(
|
||||
item => item.id == message.wechatFriendId,
|
||||
);
|
||||
session.unreadCount++;
|
||||
console.log("新消息", session);
|
||||
|
||||
session.unreadCount = Number(session.unreadCount) + 1;
|
||||
updateChatSession(session);
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user