improv: clean up code
This commit is contained in:
parent
741ad35195
commit
dfd4628680
2 changed files with 54 additions and 52 deletions
|
|
@ -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();
|
||||
|
|
|
|||
53
gui/src/pages/gui/useAutoScroll.ts
Normal file
53
gui/src/pages/gui/useAutoScroll.ts
Normal 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]);
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue