增强SearchAnyone组件以更有效地管理聊天会话:为新联系人添加会话创建逻辑,改进错误处理,并改进搜索结果的状态管理。
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import React, { useState, useEffect, useRef, useCallback } from "react";
|
||||
import { Input, Avatar, Spin } from "antd";
|
||||
import { SearchOutlined } from "@ant-design/icons";
|
||||
import { Contact } from "@/utils/db";
|
||||
import { Contact, ChatSession } from "@/utils/db";
|
||||
import { getFriendList } from "@/components/FriendSelection/api";
|
||||
import { getGroupList } from "@/components/GroupSelection/api";
|
||||
import { useContactStore } from "@/store/module/weChat/contacts";
|
||||
@@ -25,7 +25,9 @@ const SearchAnyone: React.FC<SearchAnyoneProps> = ({ onContactClick }) => {
|
||||
const { user } = useUserStore();
|
||||
const currentUserId = user?.id || 0;
|
||||
|
||||
const searchTimeoutRef = useRef<ReturnType<typeof setTimeout>>();
|
||||
const searchTimeoutRef = useRef<ReturnType<typeof setTimeout> | undefined>(
|
||||
undefined,
|
||||
);
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
// 搜索防抖处理
|
||||
@@ -146,12 +148,80 @@ const SearchAnyone: React.FC<SearchAnyoneProps> = ({ onContactClick }) => {
|
||||
// 处理点击搜索结果
|
||||
const handleResultClick = useCallback(
|
||||
async (contact: Contact) => {
|
||||
// 设置当前联系人(这会触发 SidebarMenu 中的 useEffect,自动切换到聊天tab并选中会话)
|
||||
setCurrentContact(contact);
|
||||
try {
|
||||
// 1. 先检查会话是否已存在
|
||||
let session = await MessageManager.getSessionByContactId(
|
||||
currentUserId,
|
||||
contact.id,
|
||||
contact.type,
|
||||
);
|
||||
|
||||
// 如果有自定义点击处理,调用它
|
||||
if (onContactClick) {
|
||||
onContactClick(contact);
|
||||
if (!session) {
|
||||
// 2. 如果会话不存在,创建新会话(不置顶,但排在非置顶区域第一个)
|
||||
// 使用当前时间,确保在非置顶会话中排在最前面
|
||||
const now = new Date();
|
||||
const newSession: ChatSession = {
|
||||
serverId: `${contact.type}_${contact.id}`,
|
||||
userId: currentUserId,
|
||||
id: contact.id,
|
||||
type: contact.type,
|
||||
wechatAccountId:
|
||||
contact.wechatAccountId || currentCustomer?.id || 0,
|
||||
nickname: contact.nickname || "",
|
||||
conRemark: contact.conRemark || "",
|
||||
avatar: contact.avatar || "",
|
||||
content: "",
|
||||
lastUpdateTime: now.toISOString(), // 使用当前时间,确保在非置顶区域排第一
|
||||
aiType: contact.aiType || 0,
|
||||
phone: contact.phone || "",
|
||||
region: contact.region || "",
|
||||
config: {
|
||||
unreadCount: 0,
|
||||
top: 0, // 不置顶,排在非置顶区域
|
||||
},
|
||||
sortKey: "",
|
||||
...(contact.type === "group"
|
||||
? {
|
||||
chatroomId: (contact as any).chatroomId || "",
|
||||
chatroomOwner: (contact as any).chatroomOwner || "",
|
||||
selfDisplayName: (contact as any).selfDisplayName || "",
|
||||
notice: (contact as any).notice || "",
|
||||
}
|
||||
: {
|
||||
wechatFriendId: contact.id,
|
||||
wechatId: (contact as any).wechatId || "",
|
||||
alias: (contact as any).alias || "",
|
||||
}),
|
||||
extendFields: "{}",
|
||||
};
|
||||
|
||||
// 创建会话(会自动生成 sortKey,由于使用当前时间,会在非置顶会话中排在最前面)
|
||||
await MessageManager.createSession(currentUserId, newSession);
|
||||
|
||||
// 重新获取会话(包含生成的 sortKey)
|
||||
session = await MessageManager.getSessionByContactId(
|
||||
currentUserId,
|
||||
contact.id,
|
||||
contact.type,
|
||||
);
|
||||
}
|
||||
|
||||
// 5. 设置当前联系人(这会触发 SidebarMenu 中的 useEffect,自动切换到聊天tab并选中会话)
|
||||
if (session) {
|
||||
setCurrentContact(session as any);
|
||||
} else {
|
||||
// 如果还是没有会话,使用联系人
|
||||
setCurrentContact(contact);
|
||||
}
|
||||
|
||||
// 如果有自定义点击处理,调用它
|
||||
if (onContactClick) {
|
||||
onContactClick(contact);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("处理搜索结果点击失败:", error);
|
||||
// 即使出错,也尝试设置联系人
|
||||
setCurrentContact(contact);
|
||||
}
|
||||
|
||||
// 清空搜索并隐藏结果
|
||||
@@ -159,7 +229,7 @@ const SearchAnyone: React.FC<SearchAnyoneProps> = ({ onContactClick }) => {
|
||||
setSearchResults([]);
|
||||
setShowResults(false);
|
||||
},
|
||||
[setCurrentContact, onContactClick],
|
||||
[currentUserId, currentCustomer, setCurrentContact, onContactClick],
|
||||
);
|
||||
|
||||
// 点击外部区域关闭搜索结果
|
||||
|
||||
Reference in New Issue
Block a user