fix: scroll issues w/ code blocks (#1688)
* fix: scroll issues w/ code blocks * chore: name offset
This commit is contained in:
parent
54854bd819
commit
ceb8da0b20
4 changed files with 75 additions and 47 deletions
15
gui/package-lock.json
generated
15
gui/package-lock.json
generated
|
|
@ -37,6 +37,7 @@
|
|||
"react-dom": "^18.2.0",
|
||||
"react-error-boundary": "^4.0.11",
|
||||
"react-hook-form": "^7.47.0",
|
||||
"react-intersection-observer": "^9.10.3",
|
||||
"react-markdown": "^9.0.1",
|
||||
"react-redux": "^8.0.5",
|
||||
"react-remark": "^2.1.0",
|
||||
|
|
@ -6078,6 +6079,20 @@
|
|||
"react": "^16.8.0 || ^17 || ^18"
|
||||
}
|
||||
},
|
||||
"node_modules/react-intersection-observer": {
|
||||
"version": "9.10.3",
|
||||
"resolved": "https://registry.npmjs.org/react-intersection-observer/-/react-intersection-observer-9.10.3.tgz",
|
||||
"integrity": "sha512-9NYfKwPZRovB6QJee7fDg0zz/SyYrqXtn5xTZU0vwLtLVBtfu9aZt1pVmr825REE49VPDZ7Lm5SNHjJBOTZHpA==",
|
||||
"peerDependencies": {
|
||||
"react": "^15.0.0 || ^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
|
||||
"react-dom": "^15.0.0 || ^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"react-dom": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/react-is": {
|
||||
"version": "18.3.1",
|
||||
"resolved": "https://registry.npmjs.org/react-is/-/react-is-18.3.1.tgz",
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@
|
|||
"react-dom": "^18.2.0",
|
||||
"react-error-boundary": "^4.0.11",
|
||||
"react-hook-form": "^7.47.0",
|
||||
"react-intersection-observer": "^9.10.3",
|
||||
"react-markdown": "^9.0.1",
|
||||
"react-redux": "^8.0.5",
|
||||
"react-remark": "^2.1.0",
|
||||
|
|
|
|||
32
gui/src/components/ChatScrollAnchor.tsx
Normal file
32
gui/src/components/ChatScrollAnchor.tsx
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import { useEffect } from "react";
|
||||
import { useInView } from "react-intersection-observer";
|
||||
|
||||
interface ChatScrollAnchorProps {
|
||||
trackVisibility: boolean;
|
||||
isAtBottom: boolean;
|
||||
scrollAreaRef: React.RefObject<HTMLDivElement>;
|
||||
}
|
||||
|
||||
export function ChatScrollAnchor({
|
||||
trackVisibility,
|
||||
isAtBottom,
|
||||
scrollAreaRef,
|
||||
}: ChatScrollAnchorProps) {
|
||||
const { ref, inView, entry } = useInView({
|
||||
trackVisibility,
|
||||
delay: 100,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (isAtBottom && trackVisibility && !inView) {
|
||||
if (!scrollAreaRef.current) return;
|
||||
|
||||
const scrollAreaElement = scrollAreaRef.current;
|
||||
|
||||
scrollAreaElement.scrollTop =
|
||||
scrollAreaElement.scrollHeight - scrollAreaElement.clientHeight;
|
||||
}
|
||||
}, [inView, entry, isAtBottom, trackVisibility]);
|
||||
|
||||
return <div ref={ref} className="h-px w-full" />;
|
||||
}
|
||||
|
|
@ -55,6 +55,7 @@ import {
|
|||
} from "../util";
|
||||
import { getLocalStorage, setLocalStorage } from "../util/localStorage";
|
||||
import { FREE_TRIAL_LIMIT_REQUESTS } from "../util/freeTrial";
|
||||
import { ChatScrollAnchor } from "../components/ChatScrollAnchor";
|
||||
|
||||
const TopGuiDiv = styled.div`
|
||||
overflow-y: scroll;
|
||||
|
|
@ -148,74 +149,48 @@ interface GUIProps {
|
|||
}
|
||||
|
||||
function GUI(props: GUIProps) {
|
||||
// #region Hooks
|
||||
const posthog = usePostHog();
|
||||
const dispatch = useDispatch();
|
||||
const navigate = useNavigate();
|
||||
const ideMessenger = useContext(IdeMessengerContext);
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region Selectors
|
||||
const sessionState = useSelector((state: RootState) => state.state);
|
||||
|
||||
const defaultModel = useSelector(defaultModelSelector);
|
||||
|
||||
const active = useSelector((state: RootState) => state.state.active);
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region State
|
||||
const [stepsOpen, setStepsOpen] = useState<(boolean | undefined)[]>([]);
|
||||
const [showLoading, setShowLoading] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setTimeout(() => {
|
||||
setShowLoading(true);
|
||||
}, 5000);
|
||||
}, []);
|
||||
|
||||
// #endregion
|
||||
|
||||
const mainTextInputRef = useRef<HTMLInputElement>(null);
|
||||
const topGuiDivRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
// #region Effects
|
||||
const [userScrolledAwayFromBottom, setUserScrolledAwayFromBottom] =
|
||||
useState<boolean>(false);
|
||||
const [isAtBottom, setIsAtBottom] = useState<boolean>(false);
|
||||
|
||||
const state = useSelector((state: RootState) => state.state);
|
||||
|
||||
const handleScroll = () => {
|
||||
// Temporary fix to account for additional height when code blocks are added
|
||||
const OFFSET_HERUISTIC = 300;
|
||||
if (!topGuiDivRef.current) return;
|
||||
|
||||
const { scrollTop, scrollHeight, clientHeight } = topGuiDivRef.current;
|
||||
const atBottom =
|
||||
scrollHeight - clientHeight <= scrollTop + OFFSET_HERUISTIC;
|
||||
|
||||
setIsAtBottom(atBottom);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const handleScroll = () => {
|
||||
// Scroll only if user is within 200 pixels of the bottom of the window.
|
||||
const edgeOffset = -25;
|
||||
const scrollPosition = topGuiDivRef.current?.scrollTop || 0;
|
||||
const scrollHeight = topGuiDivRef.current?.scrollHeight || 0;
|
||||
const clientHeight = window.innerHeight || 0;
|
||||
if (!active || !topGuiDivRef.current) return;
|
||||
|
||||
if (scrollPosition + clientHeight + edgeOffset >= scrollHeight) {
|
||||
setUserScrolledAwayFromBottom(false);
|
||||
} else {
|
||||
setUserScrolledAwayFromBottom(true);
|
||||
}
|
||||
};
|
||||
const scrollAreaElement = topGuiDivRef.current;
|
||||
|
||||
topGuiDivRef.current?.addEventListener("scroll", handleScroll);
|
||||
scrollAreaElement.scrollTop =
|
||||
scrollAreaElement.scrollHeight - scrollAreaElement.clientHeight;
|
||||
|
||||
return () => {
|
||||
window.removeEventListener("scroll", handleScroll);
|
||||
};
|
||||
}, [topGuiDivRef.current]);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (userScrolledAwayFromBottom) return;
|
||||
|
||||
topGuiDivRef.current?.scrollTo({
|
||||
top: topGuiDivRef.current?.scrollHeight,
|
||||
behavior: "instant" as any,
|
||||
});
|
||||
}, [topGuiDivRef.current?.scrollHeight, sessionState.history]);
|
||||
setIsAtBottom(true);
|
||||
}, [active]);
|
||||
|
||||
useEffect(() => {
|
||||
// Cmd + Backspace to delete current step
|
||||
|
|
@ -365,7 +340,7 @@ function GUI(props: GUIProps) {
|
|||
|
||||
return (
|
||||
<>
|
||||
<TopGuiDiv ref={topGuiDivRef}>
|
||||
<TopGuiDiv ref={topGuiDivRef} onScroll={handleScroll}>
|
||||
<div className="max-w-3xl m-auto">
|
||||
<StepsDiv>
|
||||
{state.history.map((item, index: number) => {
|
||||
|
|
@ -463,7 +438,6 @@ function GUI(props: GUIProps) {
|
|||
);
|
||||
})}
|
||||
</StepsDiv>
|
||||
|
||||
<ContinueInputBox
|
||||
onEnter={(editorContent, modifiers) => {
|
||||
sendInput(editorContent, modifiers);
|
||||
|
|
@ -499,6 +473,12 @@ function GUI(props: GUIProps) {
|
|||
</NewSessionButton>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
<ChatScrollAnchor
|
||||
scrollAreaRef={topGuiDivRef}
|
||||
isAtBottom={isAtBottom}
|
||||
trackVisibility={active}
|
||||
/>
|
||||
</TopGuiDiv>
|
||||
{active && (
|
||||
<StopButton
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue