Skip to content
This repository was archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
Improve [UI/UX] [Front End] [Chat] Scrolling (#274)
Browse files Browse the repository at this point in the history
- [+] feat(chat.tsx): enhance chat scrolling functionality

  - Add check for user manually scrolling to disable auto-scroll
  - Enable auto-scroll only if config.autoScrollMessage is true and user is not at top or bottom edge
  - Update message render index only when auto-scroll is enabled
  - Determine if user has scrolled to the bottom of the chat
  • Loading branch information
H0llyW00dzZ authored Feb 12, 2024
1 parent 3b23997 commit eacbda7
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1176,23 +1176,36 @@ function _Chat() {
const bottomHeight = target.scrollTop + target.clientHeight;
const edgeThreshold = target.clientHeight;

// Determine if the user is at the top or bottom edge of the chat.
const isTouchTopEdge = target.scrollTop <= edgeThreshold;
const isTouchBottomEdge = bottomHeight >= target.scrollHeight - edgeThreshold;
const isHitBottom = bottomHeight >= target.scrollHeight - (isMobileScreen ? 4 : 10);

// Calculate page indices directly inside the function
const nextPageMsgIndex = msgRenderIndex + CHAT_PAGE_SIZE;
const prevPageMsgIndex = msgRenderIndex - CHAT_PAGE_SIZE;
// If the user is manually scrolling, disable auto-scroll.
if (isTouchTopEdge || isTouchBottomEdge) {
setAutoScroll(false);
} else {
// Only enable auto-scroll if the `config.autoScrollMessage` is true
// and the user has not manually scrolled to the top or bottom edge.
if (config.autoScrollMessage) {
setAutoScroll(true);
}
}

if (isTouchTopEdge && !isTouchBottomEdge) {
setMsgRenderIndex(prevPageMsgIndex);
} else if (isTouchBottomEdge) {
setMsgRenderIndex(nextPageMsgIndex);
// Update the message render index only if auto-scroll is enabled.
if (config.autoScrollMessage) {
const nextPageMsgIndex = msgRenderIndex + CHAT_PAGE_SIZE;
const prevPageMsgIndex = msgRenderIndex - CHAT_PAGE_SIZE;

if (isTouchTopEdge && !isTouchBottomEdge) {
setMsgRenderIndex(prevPageMsgIndex);
} else if (isTouchBottomEdge) {
setMsgRenderIndex(nextPageMsgIndex);
}
}

// Determine if the user has scrolled to the bottom of the chat.
const isHitBottom = bottomHeight >= target.scrollHeight - (isMobileScreen ? 4 : 10);
setHitBottom(isHitBottom);
let isAutoScrollEnabled: boolean = config.autoScrollMessage;
setAutoScroll(isAutoScrollEnabled);
}, [
setHitBottom,
setAutoScroll,
Expand Down

0 comments on commit eacbda7

Please sign in to comment.