Skip to content

Commit

Permalink
k (#2772)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablodanswer authored Oct 12, 2024
1 parent b75b833 commit 301032f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
20 changes: 18 additions & 2 deletions web/src/app/chat/ChatPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,11 @@ export function ChatPage({

const handleInputResize = () => {
setTimeout(() => {
if (inputRef.current && lastMessageRef.current) {
if (
inputRef.current &&
lastMessageRef.current &&
!waitForScrollRef.current
) {
const newHeight: number =
inputRef.current?.getBoundingClientRect().height!;
const heightDifference = newHeight - previousHeight.current;
Expand Down Expand Up @@ -806,8 +810,11 @@ export function ChatPage({
};

const clientScrollToBottom = (fast?: boolean) => {
waitForScrollRef.current = true;

setTimeout(() => {
if (!endDivRef.current || !scrollableDivRef.current) {
console.error("endDivRef or scrollableDivRef not found");
return;
}

Expand All @@ -818,6 +825,7 @@ export function ChatPage({

// Check if all messages are currently rendered
if (currentVisibleRange.end < messageHistory.length) {
console.log("Updating visible range");
// Update visible range to include the last messages
updateCurrentVisibleRange({
start: Math.max(
Expand All @@ -835,15 +843,21 @@ export function ChatPage({
behavior: fast ? "auto" : "smooth",
});
setHasPerformedInitialScroll(true);
}, 0);
}, 100);
} else {
console.log("All messages are already rendered, scrolling immediately");
// If all messages are already rendered, scroll immediately
endDivRef.current.scrollIntoView({
behavior: fast ? "auto" : "smooth",
});
setHasPerformedInitialScroll(true);
}
}, 50);

// Reset waitForScrollRef after 1.5 seconds
setTimeout(() => {
waitForScrollRef.current = false;
}, 1500);
};

const distance = 500; // distance that should "engage" the scroll
Expand Down Expand Up @@ -1553,6 +1567,7 @@ export function ChatPage({
toggle(false);
};

const waitForScrollRef = useRef(false);
const sidebarElementRef = useRef<HTMLDivElement>(null);

useSidebarVisibility({
Expand All @@ -1571,6 +1586,7 @@ export function ChatPage({
endDivRef,
distance,
debounceNumber,
waitForScrollRef,
});

// Virtualization + Scrolling related effects and functions
Expand Down
2 changes: 2 additions & 0 deletions web/src/app/chat/lib.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,11 @@ export async function useScrollonStream({
endDivRef,
distance,
debounceNumber,
waitForScrollRef,
}: {
chatState: ChatState;
scrollableDivRef: RefObject<HTMLDivElement>;
waitForScrollRef: RefObject<boolean>;
scrollDist: MutableRefObject<number>;
endDivRef: RefObject<HTMLDivElement>;
distance: number;
Expand Down

0 comments on commit 301032f

Please sign in to comment.