<div class="direct-chat-messages" ref={(el) => { messagesBox = el; }} onScroll={debouncedChangeHandler}>
{prevMessages.map((message, i, arr)=>{
useEffect(() => {
socket.on('getPrevMessages', data => {
setPrevMessages(prev => [...prev, ...data]);
if(prevMessages.length) {
const sliced = prevMessages.slice(-20);
setDataState(data[0].id === sliced[0].id);
console.log(data[0].id === sliced[0].id);
}
});
}, [prevMessages])
{prevMessages.map((message, i, arr)=>{
let sender = '';
if(message.senderId === roomId) {
sender = 'messageSender';
}
return sender ? (
<div key={message.id} id={`msg${message.id}`} className={`direct-chat-msg msg-to ${sender}
${message.readStatus !== 0 ? 'isRead' : 'isNotRead'}`}>
<img class="direct-chat-img" src="../img/user3-128x128.jpg" alt="User avatar"/>
<div className="direct-chat-text">
<span className="direct-chat-timestamp">{
`${message.createDate.split('T')[0]} | ${message.createDate.split('T')[1].split(':')[0]}:${message.createDate.split('T')[1].split(':')[1]}`
}</span>
{message.text}
</div>
</div>
) : (
<div key={message.id} id={`msg${message.id}`} className={`direct-chat-msg msg-to ${sender}
${message.readStatus !== 0 ? 'isRead' : 'isNotRead'}`}>
<div className="direct-chat-text">
<i className={'fas fa-check faCheck'}></i>
<span className="direct-chat-timestamp">{
`${message.createDate.split('T')[0]} | ${message.createDate.split('T')[1].split(':')[0]}:${message.createDate.split('T')[1].split(':')[1]}`
}</span>
{message.text}
</div>
<img className="direct-chat-img" src="../img/user3-128x128.jpg" alt="User avatar" />
</div>
)
})}
[Violation] 'message' handler took 208ms
index.js:242 4492
index.js:79 false
30index.js:79 true
2.9ce607ef.chunk.js:2 [Violation] 'message' handler took 669ms
index.js:242 4492
index.js:79 false
62index.js:79 true
2.9ce607ef.chunk.js:2 [Violation] 'message' handler took 2065ms
function debounce( f, delay = 40 ) {
let t;
return function( ...a ) {
clearTimeout( t );
t = setTimeout( f.bind( this ), delay, ...a );
}
}
const changeHandler = (e) => {
if(e.target.scrollTop < 1) {
const msgID = prevMessages.length ? parseInt(prevMessages[0].id) : parseInt(messages[0].id);
console.log(msgID);
socket.emit('getPrevMessages', {roomId, msgID});
e.target.scrollTo(0, e.target.querySelector('.direct-chat-msg:nth-child(20)').offsetTop);
e.stopPropagation();
}
}
const debouncedChangeHandler = useCallback((e) => debounce(changeHandler(e), 250), [prevMessages, messages]);
function debounce(func, wait) {
let timeout;
return () => {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(func, wait)
}
}
const changeHandler = (e) => {
if(e.target.scrollTop < 1) {
const msgID = prevMessages.length ? parseInt(prevMessages[0].id) : parseInt(messages[0].id);
console.log(msgID);
socket.emit('getPrevMessages', {roomId, msgID});
//e.stopPropagation();
}
}
const debouncedChangeHandler = useCallback((e) => {
debounce(() => changeHandler(e), 250);
debounce(() => {
if(e.target.scrollTop < 1) e.target.scrollTo(0, e.target.querySelector('.direct-chat-msg:nth-child(20)').offsetTop)
}, 250);
}, []);