|
|
|
@ -21,7 +21,10 @@ export default function MessageDetail({}) {
|
|
|
|
|
// const [hasMore, setHasMore] = useState(true);
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
const searchParams = useSearchParams();
|
|
|
|
|
const [oldMessages, setOldMessages] = useState([]);
|
|
|
|
|
const [oldMessages, setOldMessages] = useState({
|
|
|
|
|
list: [],
|
|
|
|
|
isGetHistory: false,
|
|
|
|
|
});
|
|
|
|
|
const [messages, setMessages] = useState([]);
|
|
|
|
|
const [handledmessages, setHandledmessages] = useState([]);
|
|
|
|
|
const [sessionId, setSessionId] = useState();
|
|
|
|
@ -61,9 +64,10 @@ export default function MessageDetail({}) {
|
|
|
|
|
}, []);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const intervalId = setInterval(() => {
|
|
|
|
|
// debugger;
|
|
|
|
|
// console.log("oldMessages[0]", oldMessages[0]);
|
|
|
|
|
if (oldMessages[0]) {
|
|
|
|
|
updateMessages(oldMessages[0]?.id, 0, oldMessages);
|
|
|
|
|
if (oldMessages.list[0]) {
|
|
|
|
|
updateMessages(oldMessages.list[0]?.id, 0, oldMessages.list);
|
|
|
|
|
// toScrollBottom.current = 1;
|
|
|
|
|
}
|
|
|
|
|
}, 3000); // 间隔时间为3秒
|
|
|
|
@ -72,13 +76,14 @@ export default function MessageDetail({}) {
|
|
|
|
|
return () => {
|
|
|
|
|
clearInterval(intervalId);
|
|
|
|
|
};
|
|
|
|
|
}, [oldMessages]);
|
|
|
|
|
}, [oldMessages.list]);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (toScrollBottom.current) {
|
|
|
|
|
// debugger;
|
|
|
|
|
if (toScrollBottom.current && !oldMessages.isGetHistory) {
|
|
|
|
|
scrollBox.current?.scrollTo(0, scrollBox.current.scrollHeight + 50);
|
|
|
|
|
toScrollBottom.current = 0;
|
|
|
|
|
}
|
|
|
|
|
}, [messages]);
|
|
|
|
|
}, [oldMessages.list, oldMessages.isGetHistory]);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (sessionId && userInfo.mid) {
|
|
|
|
|
loadEarlierHistory().then((res) => {
|
|
|
|
@ -191,7 +196,10 @@ export default function MessageDetail({}) {
|
|
|
|
|
}
|
|
|
|
|
setOffset(data.data.offset);
|
|
|
|
|
setMore(data.data.more);
|
|
|
|
|
let mathNewMessages = handleData([...oldMessages, ...data.data.list]);
|
|
|
|
|
let mathNewMessages = handleData(
|
|
|
|
|
[...oldMessages.list, ...data.data.list],
|
|
|
|
|
oldMessages.list.length > 0
|
|
|
|
|
);
|
|
|
|
|
// setMessages((prev) => [...prev, ...temMessages]);
|
|
|
|
|
setLoading(false);
|
|
|
|
|
return mathNewMessages;
|
|
|
|
@ -269,7 +277,7 @@ export default function MessageDetail({}) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleData = (list) => {
|
|
|
|
|
const handleData = (list, isGetHistory) => {
|
|
|
|
|
// console.log("list", list);
|
|
|
|
|
const account = get("account");
|
|
|
|
|
const temMessages = list.map((item) => {
|
|
|
|
@ -297,7 +305,9 @@ export default function MessageDetail({}) {
|
|
|
|
|
user: {
|
|
|
|
|
_id: 0,
|
|
|
|
|
name: "客服",
|
|
|
|
|
avatar: process.env.NEXT_PUBLIC_WEB_ASSETS_URL + "/images/icon.png",
|
|
|
|
|
avatar:
|
|
|
|
|
process.env.NEXT_PUBLIC_WEB_ASSETS_URL +
|
|
|
|
|
"/images/icon_border.png",
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
@ -305,7 +315,16 @@ export default function MessageDetail({}) {
|
|
|
|
|
// console.log("handledmessages......", handledmessages);
|
|
|
|
|
// console.log("[...messages, ...temMessages]", temMessages);
|
|
|
|
|
setHandledmessages(temMessages);
|
|
|
|
|
setOldMessages(list);
|
|
|
|
|
setOldMessages((old) => {
|
|
|
|
|
// debugger;
|
|
|
|
|
if (old.list.length === list.length) {
|
|
|
|
|
return old;
|
|
|
|
|
} else {
|
|
|
|
|
// debugger;
|
|
|
|
|
return { list, isGetHistory: !!isGetHistory };
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let newMessages = temMessages.reverse();
|
|
|
|
|
let mathNewMessages = newMessages.reduce(
|
|
|
|
|
(accumulator, currentValue, index, sourceArray) => {
|
|
|
|
@ -445,7 +464,7 @@ export default function MessageDetail({}) {
|
|
|
|
|
}
|
|
|
|
|
// data.m_type===1?it?.text.split("|")it?.text
|
|
|
|
|
},
|
|
|
|
|
[sessionId]
|
|
|
|
|
[sessionId, oldMessages.list]
|
|
|
|
|
);
|
|
|
|
|
return (
|
|
|
|
|
<div className="bg-[#13121F] h-screen overflow-y-auto" ref={scrollBox}>
|
|
|
|
|