Skip to content

Commit

Permalink
feat: scrolling to bottom during streaming is now instant (#461)
Browse files Browse the repository at this point in the history
* feat: scrolling to bottom during streaming is now instant

this fixes a long tail of edge-case issues where the chat would simply stop scrolling during streaming mode

* changelog
  • Loading branch information
Yonom authored Jul 11, 2024
1 parent d097e49 commit 7d4798a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
7 changes: 7 additions & 0 deletions packages/react/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @assistant-ui/react

## 0.4.3

### Minor Changes

- feat: scrolling to bottom during streaming is now instant
- fix: useSmooth gets triggered during branch switch

## 0.4.2

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"conversational-ui",
"conversational-ai"
],
"version": "0.4.2",
"version": "0.4.3",
"license": "MIT",
"exports": {
".": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,16 @@ export const useThreadViewportAutoScroll = <TElement extends HTMLElement>({

const { useViewport } = useThreadContext();

const firstRenderRef = useRef(true);
const lastScrollTop = useRef<number>(0);

// bug: when ScrollToBottom's button changes its disabled state, the scroll stops
// fix: delay the state change until the scroll is done
const isScrollingToBottomRef = useRef(false);

const scrollToBottom = () => {
const scrollToBottom = (behavior: ScrollBehavior) => {
const div = divRef.current;
if (!div || !autoScroll) return;

const behavior = firstRenderRef.current ? "instant" : "auto";
firstRenderRef.current = false;

isScrollingToBottomRef.current = true;
div.scrollTo({ top: div.scrollHeight, behavior });
};
Expand All @@ -47,7 +43,9 @@ export const useThreadViewportAutoScroll = <TElement extends HTMLElement>({
if (!newIsAtBottom && lastScrollTop.current < div.scrollTop) {
// ignore scroll down
} else {
isScrollingToBottomRef.current = newIsAtBottom;
if (newIsAtBottom) {
isScrollingToBottomRef.current = false;
}

if (newIsAtBottom !== isAtBottom) {
(useViewport as unknown as StoreApi<ThreadViewportState>).setState({
Expand All @@ -60,15 +58,11 @@ export const useThreadViewportAutoScroll = <TElement extends HTMLElement>({
};

const resizeRef = useOnResizeContent(() => {
if (
!isScrollingToBottomRef.current &&
!useViewport.getState().isAtBottom &&
!firstRenderRef.current
) {
handleScroll();
} else {
scrollToBottom();
if (isScrollingToBottomRef.current || useViewport.getState().isAtBottom) {
scrollToBottom("instant");
}

handleScroll();
});

const scrollRef = useManagedRef<HTMLElement>((el) => {
Expand All @@ -81,7 +75,7 @@ export const useThreadViewportAutoScroll = <TElement extends HTMLElement>({
const autoScrollRef = useComposedRefs<TElement>(resizeRef, scrollRef, divRef);

useOnScrollToBottom(() => {
scrollToBottom();
scrollToBottom("auto");
});

return autoScrollRef;
Expand Down

0 comments on commit 7d4798a

Please sign in to comment.