Skip to content

Commit

Permalink
better scroll control on messagelist
Browse files Browse the repository at this point in the history
  • Loading branch information
allgood committed Jun 30, 2024
1 parent 682b090 commit aa755ff
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions frontend/src/components/MessagesList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ const MessagesList = ({ ticket, ticketId, isGroup }) => {
const [pageNumber, setPageNumber] = useState(1);
const [hasMore, setHasMore] = useState(false);
const [loading, setLoading] = useState(false);
const lastMessageRef = useRef();
const scrollRef = useRef();

const [selectedMessage, setSelectedMessage] = useState({});
const [selectedMessageData, setSelectedMessageData] = useState({});
Expand Down Expand Up @@ -564,8 +564,16 @@ const MessagesList = ({ ticket, ticketId, isGroup }) => {
socket.on(`company-${companyId}-appMessage`, onAppMessage);

socket.on(`company-${companyId}-presence`, (data) => {
const { scrollTop, clientHeight, scrollHeight } = scrollRef.current;
console.log({ presence: data.presence, scrollTop, clientHeight, scrollHeight});
const isAtBottom = scrollTop + clientHeight >= (scrollHeight - clientHeight/4);
setContactPresence(data.presence);
if (data?.ticketId === ticket.id) {
setContactPresence(data.presence);
if ([ "composing", "recording"].includes(data.presence)) {
if (isAtBottom) {
scrollToBottom();
}
}
}
});

Expand All @@ -579,8 +587,8 @@ const MessagesList = ({ ticket, ticketId, isGroup }) => {
};

const scrollToBottom = () => {
if (lastMessageRef.current) {
lastMessageRef.current.scrollIntoView({});
if (scrollRef.current) {
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
}
};

Expand Down Expand Up @@ -704,7 +712,6 @@ const MessagesList = ({ ticket, ticketId, isGroup }) => {
return (
<div
key={`ref-${message.createdAt}`}
ref={lastMessageRef}
style={{ float: "left", clear: "both" }}
/>
);
Expand Down Expand Up @@ -982,6 +989,7 @@ const MessagesList = ({ ticket, ticketId, isGroup }) => {
id="messagesList"
className={classes.messagesList}
onScroll={handleScroll}
ref={scrollRef}
>
{messagesList.length > 0 ? renderMessages() : []}
{contactPresence === "composing" && (
Expand Down

0 comments on commit aa755ff

Please sign in to comment.