2023-03-17 01:49:09 +08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2023-03-15 10:42:45 -04:00
|
|
|
import Message from './Message';
|
|
|
|
|
|
|
|
|
|
export default function MultiMessage({
|
2023-03-28 22:39:27 +08:00
|
|
|
conversation,
|
|
|
|
|
messagesTree,
|
2023-03-15 10:42:45 -04:00
|
|
|
scrollToBottom,
|
|
|
|
|
currentEditId,
|
2023-03-28 22:39:27 +08:00
|
|
|
setCurrentEditId
|
2023-03-15 10:42:45 -04:00
|
|
|
}) {
|
|
|
|
|
const [siblingIdx, setSiblingIdx] = useState(0);
|
|
|
|
|
|
2023-03-28 22:39:27 +08:00
|
|
|
const setSiblingIdxRev = value => {
|
|
|
|
|
setSiblingIdx(messagesTree?.length - value - 1);
|
2023-03-15 10:42:45 -04:00
|
|
|
};
|
|
|
|
|
|
2023-03-17 01:49:09 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
// reset siblingIdx when changes, mostly a new message is submitting.
|
|
|
|
|
setSiblingIdx(0);
|
2023-03-28 22:39:27 +08:00
|
|
|
}, [messagesTree?.length]);
|
2023-03-17 01:49:09 +08:00
|
|
|
|
2023-03-15 10:42:45 -04:00
|
|
|
// if (!messageList?.length) return null;
|
2023-03-28 22:39:27 +08:00
|
|
|
if (!(messagesTree && messagesTree.length)) {
|
2023-03-15 10:42:45 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-28 22:39:27 +08:00
|
|
|
if (siblingIdx >= messagesTree?.length) {
|
2023-03-15 10:42:45 -04:00
|
|
|
setSiblingIdx(0);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-28 22:39:27 +08:00
|
|
|
const message = messagesTree[messagesTree.length - siblingIdx - 1];
|
2023-03-15 10:42:45 -04:00
|
|
|
return (
|
|
|
|
|
<Message
|
2023-03-18 23:18:36 -04:00
|
|
|
key={message.messageId}
|
2023-03-28 22:39:27 +08:00
|
|
|
conversation={conversation}
|
2023-03-18 23:18:36 -04:00
|
|
|
message={message}
|
2023-03-15 10:42:45 -04:00
|
|
|
scrollToBottom={scrollToBottom}
|
|
|
|
|
currentEditId={currentEditId}
|
|
|
|
|
setCurrentEditId={setCurrentEditId}
|
2023-03-28 22:39:27 +08:00
|
|
|
siblingIdx={messagesTree.length - siblingIdx - 1}
|
|
|
|
|
siblingCount={messagesTree.length}
|
2023-03-15 10:42:45 -04:00
|
|
|
setSiblingIdx={setSiblingIdxRev}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|