better chat load

This commit is contained in:
PhilReact 2024-11-14 10:16:24 +02:00
parent 14b0806471
commit a44e51b5c1

View File

@ -10,15 +10,7 @@ export const ChatList = ({ initialMessages, myAddress, tempMessages, chatId, onR
const [showScrollButton, setShowScrollButton] = useState(false); const [showScrollButton, setShowScrollButton] = useState(false);
const hasLoadedInitialRef = useRef(false); const hasLoadedInitialRef = useRef(false);
const isAtBottomRef = useRef(true); const isAtBottomRef = useRef(true);
// const [ref, inView] = useInView({
// threshold: 0.7
// })
// useEffect(() => {
// if (inView) {
// }
// }, [inView])
// Update message list with unique signatures and tempMessages // Update message list with unique signatures and tempMessages
useEffect(() => { useEffect(() => {
let uniqueInitialMessagesMap = new Map(); let uniqueInitialMessagesMap = new Map();
@ -60,7 +52,7 @@ export const ChatList = ({ initialMessages, myAddress, tempMessages, chatId, onR
const scrollToBottom = (initialMsgs) => { const scrollToBottom = (initialMsgs) => {
const index = initialMsgs ? initialMsgs.length - 1 : messages.length - 1; const index = initialMsgs ? initialMsgs.length - 1 : messages.length - 1;
if (rowVirtualizer) { if (rowVirtualizer) {
rowVirtualizer.scrollToIndex(index, { align: 'end' }); rowVirtualizer.scrollToIndex(index, { align: 'end' })
} }
handleMessageSeen() handleMessageSeen()
}; };
@ -76,12 +68,6 @@ export const ChatList = ({ initialMessages, myAddress, tempMessages, chatId, onR
setShowScrollButton(false) setShowScrollButton(false)
}, []); }, []);
// const scrollToBottom = (initialMsgs) => {
// const index = initialMsgs ? initialMsgs.length - 1 : messages.length - 1;
// if (parentRef.current) {
// parentRef.current.scrollToIndex(index);
// }
// };
const sentNewMessageGroupFunc = useCallback(() => { const sentNewMessageGroupFunc = useCallback(() => {
@ -105,35 +91,48 @@ export const ChatList = ({ initialMessages, myAddress, tempMessages, chatId, onR
// Initialize the virtualizer // Initialize the virtualizer
const rowVirtualizer = useVirtualizer({ const rowVirtualizer = useVirtualizer({
count: messages.length, count: messages.length,
getItemKey: React.useCallback(
(index) => messages[index].signature,
[messages]
),
getScrollElement: () => parentRef.current, getScrollElement: () => parentRef.current,
estimateSize: () => 80, // Provide an estimated height of items, adjust this as needed estimateSize: () => 80, // Provide an estimated height of items, adjust this as needed
overscan: 10, // Number of items to render outside the visible area to improve smoothness overscan: 10, // Number of items to render outside the visible area to improve smoothness
measureElement:
typeof window !== 'undefined' &&
navigator.userAgent.indexOf('Firefox') === -1
? element => {
return element?.getBoundingClientRect().height
}
: undefined,
}); });
return ( return (
<div style={{ <div style={{
height: '100%', height: '100%',
position: 'relative' position: 'relative',
display: 'flex',
flexDirection: 'column'
}}> }}>
<div ref={parentRef} style={{ height: '100%', overflow: 'auto', position: 'relative', display: 'flex' }}>
<div <div
style={{ ref={parentRef}
width: '100%', className="List"
position: 'relative', style={{ flexGrow: 1, overflow: 'auto', position: 'relative', display: 'flex', height: '0px' }}
display: 'flex',
flexDirection: 'column',
alignItems: 'center', // Center items horizontally
gap: '10px', // Add gap between items
flexGrow: 1
}}
> >
<div
style={{
height: rowVirtualizer.getTotalSize(),
width: '100%',
}}
>
<div
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
}}
>
{rowVirtualizer.getVirtualItems().map((virtualRow) => { {rowVirtualizer.getVirtualItems().map((virtualRow) => {
const index = virtualRow.index; const index = virtualRow.index;
let message = messages[index]; let message = messages[index];
@ -172,7 +171,7 @@ export const ChatList = ({ initialMessages, myAddress, tempMessages, chatId, onR
return ( return (
<div <div
data-index={virtualRow.index} //needed for dynamic row height measurement data-index={virtualRow.index} //needed for dynamic row height measurement
ref={node => rowVirtualizer.measureElement(node)} //measure dynamic row height ref={rowVirtualizer.measureElement} //measure dynamic row height
key={message.signature} key={message.signature}
style={{ style={{
position: 'absolute', position: 'absolute',
@ -205,7 +204,7 @@ export const ChatList = ({ initialMessages, myAddress, tempMessages, chatId, onR
); );
})} })}
</div> </div>
</div>
</div> </div>
{showScrollButton && ( {showScrollButton && (