2023-03-17 01:49:09 +08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2023-04-06 02:06:39 +08:00
|
|
|
import { useRecoilState } from 'recoil';
|
2023-03-15 10:42:45 -04:00
|
|
|
import Message from './Message';
|
|
|
|
|
|
2023-04-06 02:06:39 +08:00
|
|
|
import store from '~/store';
|
|
|
|
|
|
2023-03-15 10:42:45 -04:00
|
|
|
export default function MultiMessage({
|
2023-04-06 02:06:39 +08:00
|
|
|
messageId,
|
2023-03-28 22:39:27 +08:00
|
|
|
conversation,
|
|
|
|
|
messagesTree,
|
2023-03-15 10:42:45 -04:00
|
|
|
scrollToBottom,
|
|
|
|
|
currentEditId,
|
2023-03-29 02:29:15 +08:00
|
|
|
setCurrentEditId,
|
|
|
|
|
isSearchView
|
2023-03-15 10:42:45 -04:00
|
|
|
}) {
|
2023-04-06 02:06:39 +08:00
|
|
|
// const [siblingIdx, setSiblingIdx] = useState(0);
|
|
|
|
|
|
|
|
|
|
const [siblingIdx, setSiblingIdx] = useRecoilState(store.messagesSiblingIdxFamily(messageId));
|
2023-03-15 10:42:45 -04:00
|
|
|
|
2023-05-18 11:09:31 -07:00
|
|
|
const setSiblingIdxRev = (value) => {
|
2023-03-28 22:39:27 +08:00
|
|
|
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-29 02:29:15 +08:00
|
|
|
if (isSearchView)
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{messagesTree
|
|
|
|
|
? messagesTree.map(message => (
|
2023-05-18 11:09:31 -07:00
|
|
|
<Message
|
|
|
|
|
key={message.messageId}
|
|
|
|
|
conversation={conversation}
|
|
|
|
|
message={message}
|
|
|
|
|
scrollToBottom={scrollToBottom}
|
|
|
|
|
currentEditId={currentEditId}
|
|
|
|
|
setCurrentEditId={null}
|
|
|
|
|
siblingIdx={1}
|
|
|
|
|
siblingCount={1}
|
|
|
|
|
setSiblingIdx={null}
|
|
|
|
|
/>
|
|
|
|
|
))
|
2023-03-29 02:29:15 +08:00
|
|
|
: null}
|
|
|
|
|
</>
|
|
|
|
|
);
|
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}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|