优化会话置顶功能,更新状态切换逻辑并添加删除会话功能;新增样式以突出置顶会话,提升用户体验。
This commit is contained in:
@@ -429,6 +429,88 @@ export const useCkChatStore = createPersistStore<CkChatState>(
|
||||
};
|
||||
});
|
||||
},
|
||||
// 切换会话置顶状态
|
||||
toggleChatSessionPin: (sessionId: number, isPinned: boolean) => {
|
||||
set(state => {
|
||||
const updatedSessions = state.chatSessions.map(item => {
|
||||
if (item.id === sessionId) {
|
||||
return {
|
||||
...item,
|
||||
config: {
|
||||
...item.config,
|
||||
top: isPinned,
|
||||
},
|
||||
};
|
||||
}
|
||||
return item;
|
||||
});
|
||||
|
||||
// 如果置顶,将会话移到顶部
|
||||
if (isPinned) {
|
||||
const sessionIndex = updatedSessions.findIndex(
|
||||
item => item.id === sessionId,
|
||||
);
|
||||
if (sessionIndex !== -1) {
|
||||
const session = updatedSessions[sessionIndex];
|
||||
const otherSessions = updatedSessions.filter(
|
||||
item => item.id !== sessionId,
|
||||
);
|
||||
return {
|
||||
chatSessions: [session, ...otherSessions],
|
||||
};
|
||||
}
|
||||
} else {
|
||||
// 如果取消置顶,重新排序(置顶>未读>名称>时间)
|
||||
const sortedSessions = updatedSessions.sort((a, b) => {
|
||||
// 获取置顶状态
|
||||
const aTop = (a.config as any)?.top || false;
|
||||
const bTop = (b.config as any)?.top || false;
|
||||
|
||||
// 首先按置顶状态排序(置顶的排在前面)
|
||||
if (aTop !== bTop) {
|
||||
return aTop ? -1 : 1;
|
||||
}
|
||||
|
||||
// 如果都是置顶或都不是置顶,则按未读消息数量降序排列
|
||||
const aUnread = a.config?.unreadCount || 0;
|
||||
const bUnread = b.config?.unreadCount || 0;
|
||||
|
||||
if (aUnread !== bUnread) {
|
||||
return bUnread - aUnread;
|
||||
}
|
||||
|
||||
// 如果未读消息数量相同,则按显示名称排序
|
||||
const getDisplayName = (session: any) => {
|
||||
return session.conRemark || session.nickname || "";
|
||||
};
|
||||
const aName = getDisplayName(a).toLowerCase();
|
||||
const bName = getDisplayName(b).toLowerCase();
|
||||
|
||||
if (aName !== bName) {
|
||||
return aName.localeCompare(bName, "zh-CN");
|
||||
}
|
||||
|
||||
// 如果名称也相同,则按时间降序排列
|
||||
if (!a.lastUpdateTime) return 1;
|
||||
if (!b.lastUpdateTime) return -1;
|
||||
|
||||
const timeCompare =
|
||||
new Date(b.lastUpdateTime).getTime() -
|
||||
new Date(a.lastUpdateTime).getTime();
|
||||
|
||||
return timeCompare;
|
||||
});
|
||||
|
||||
return {
|
||||
chatSessions: sortedSessions,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
chatSessions: updatedSessions,
|
||||
};
|
||||
});
|
||||
},
|
||||
// 设置用户信息
|
||||
setUserInfo: (userInfo: CkUserInfo) => {
|
||||
set({ userInfo, isLoggedIn: true });
|
||||
@@ -561,3 +643,5 @@ export const updateIsLoadWeChat = (isLoadWeChat: boolean) =>
|
||||
useCkChatStore.getState().updateIsLoadWeChat(isLoadWeChat);
|
||||
export const getIsLoadWeChat = () =>
|
||||
useCkChatStore.getState().getIsLoadWeChat();
|
||||
export const toggleChatSessionPin = (sessionId: number, isPinned: boolean) =>
|
||||
useCkChatStore.getState().toggleChatSessionPin(sessionId, isPinned);
|
||||
|
||||
Reference in New Issue
Block a user