improv: clean up code

This commit is contained in:
tomasz-io 2025-03-11 08:30:37 -07:00
parent 741ad35195
commit dfd4628680
2 changed files with 54 additions and 52 deletions

View file

@ -81,6 +81,7 @@ import { ExploreDialogWatcher } from "./ExploreDialogWatcher";
import { ToolCallDiv } from "./ToolCallDiv";
import { ToolCallButtons } from "./ToolCallDiv/ToolCallButtonsDiv";
import ToolOutput from "./ToolCallDiv/ToolOutput";
import { useAutoScroll } from "./useAutoScroll";
const StopButton = styled.div`
background-color: ${vscBackground};
@ -138,58 +139,6 @@ function fallbackRender({ error, resetErrorBoundary }: any) {
);
}
const useAutoScroll = (
ref: React.RefObject<HTMLDivElement>,
history: unknown[],
) => {
const [userHasScrolled, setUserHasScrolled] = useState(false);
useEffect(() => {
if (history.length) {
setUserHasScrolled(false);
}
}, [history.length]);
useEffect(() => {
if (!ref.current || history.length === 0) return;
const handleScroll = () => {
const elem = ref.current;
if (!elem) return;
const isAtBottom =
Math.abs(elem.scrollHeight - elem.scrollTop - elem.clientHeight) < 1;
/**
* We stop auto scrolling if a user manually scrolled up.
* We resume auto scrolling if a user manually scrolled to the bottom.
*/
setUserHasScrolled(!isAtBottom);
};
const resizeObserver = new ResizeObserver(() => {
const elem = ref.current;
if (!elem || userHasScrolled) return;
elem.scrollTop = elem.scrollHeight;
});
ref.current.addEventListener("scroll", handleScroll);
// Observe the container
resizeObserver.observe(ref.current);
// Observe all immediate children
Array.from(ref.current.children).forEach((child) => {
resizeObserver.observe(child);
});
return () => {
resizeObserver.disconnect();
ref.current?.removeEventListener("scroll", handleScroll);
};
}, [ref, history.length, userHasScrolled]);
};
export function Chat() {
const posthog = usePostHog();
const dispatch = useAppDispatch();

View file

@ -0,0 +1,53 @@
import { useEffect, useState } from "react";
export const useAutoScroll = (
ref: React.RefObject<HTMLDivElement>,
history: unknown[],
) => {
const [userHasScrolled, setUserHasScrolled] = useState(false);
useEffect(() => {
if (history.length) {
setUserHasScrolled(false);
}
}, [history.length]);
useEffect(() => {
if (!ref.current || history.length === 0) return;
const handleScroll = () => {
const elem = ref.current;
if (!elem) return;
const isAtBottom =
Math.abs(elem.scrollHeight - elem.scrollTop - elem.clientHeight) < 1;
/**
* We stop auto scrolling if a user manually scrolled up.
* We resume auto scrolling if a user manually scrolled to the bottom.
*/
setUserHasScrolled(!isAtBottom);
};
const resizeObserver = new ResizeObserver(() => {
const elem = ref.current;
if (!elem || userHasScrolled) return;
elem.scrollTop = elem.scrollHeight;
});
ref.current.addEventListener("scroll", handleScroll);
// Observe the container
resizeObserver.observe(ref.current);
// Observe all immediate children
Array.from(ref.current.children).forEach((child) => {
resizeObserver.observe(child);
});
return () => {
resizeObserver.disconnect();
ref.current?.removeEventListener("scroll", handleScroll);
};
}, [ref, history.length, userHasScrolled]);
};