Skip to content

Commit

Permalink
Fix: build failed on eslint rule (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshithmullapudi authored Jan 13, 2025
1 parent 13b13ca commit edfe7bc
Showing 1 changed file with 32 additions and 38 deletions.
70 changes: 32 additions & 38 deletions packages/ui/src/components/ui/chat/hooks/useAutoScroll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,52 +37,47 @@ export function useAutoScroll(options: UseAutoScrollOptions = {}) {

const scrollToBottom = useCallback(
(instant?: boolean) => {
if (!scrollRef.current) {
return;
}

const targetScrollTop =
scrollRef.current.scrollHeight - scrollRef.current.clientHeight;

if (instant) {
scrollRef.current.scrollTop = targetScrollTop;
} else {
scrollRef.current.scrollTo({
top: targetScrollTop,
behavior: smooth ? 'smooth' : 'auto',
if (scrollRef.current) {
const targetScrollTop =
scrollRef.current.scrollHeight - scrollRef.current.clientHeight;

if (instant) {
scrollRef.current.scrollTop = targetScrollTop;
} else {
scrollRef.current.scrollTo({
top: targetScrollTop,
behavior: smooth ? 'smooth' : 'auto',
});
}

setScrollState({
isAtBottom: true,
autoScrollEnabled: true,
});
userHasScrolled.current = false;
}

setScrollState({
isAtBottom: true,
autoScrollEnabled: true,
});
userHasScrolled.current = false;
},
[smooth],
);

const handleScroll = useCallback(() => {
if (!scrollRef.current) {
return;
}

const atBottom = checkIsAtBottom(scrollRef.current);
if (scrollRef.current) {
const atBottom = checkIsAtBottom(scrollRef.current);

setScrollState((prev) => ({
isAtBottom: atBottom,
// Re-enable auto-scroll if at the bottom
autoScrollEnabled: atBottom ? true : prev.autoScrollEnabled,
}));
setScrollState((prev) => ({
isAtBottom: atBottom,
// Re-enable auto-scroll if at the bottom
autoScrollEnabled: atBottom ? true : prev.autoScrollEnabled,
}));
}
}, [checkIsAtBottom]);

useEffect(() => {
const element = scrollRef.current;
if (!element) {
return;
if (element) {
element.addEventListener('scroll', handleScroll, { passive: true });
}

element.addEventListener('scroll', handleScroll, { passive: true });
return () => element.removeEventListener('scroll', handleScroll);
}, [handleScroll]);

Expand All @@ -106,18 +101,17 @@ export function useAutoScroll(options: UseAutoScrollOptions = {}) {
}, [content, scrollState.autoScrollEnabled, scrollToBottom]);

useEffect(() => {
const element = scrollRef.current;
if (!element) {
return;
}

const resizeObserver = new ResizeObserver(() => {
if (scrollState.autoScrollEnabled) {
scrollToBottom(true);
}
});

resizeObserver.observe(element);
const element = scrollRef.current;
if (element) {
resizeObserver.observe(element);
}

return () => resizeObserver.disconnect();
}, [scrollState.autoScrollEnabled, scrollToBottom]);

Expand Down

0 comments on commit edfe7bc

Please sign in to comment.