Skip to content
Source
Sign inSign up
pingdotgg/t3code

Preserve chat scroll position while streaming new messages #51

Mergedjuliusmarmingemerged 1 commit intomain fromcodething/25696837merged by juliusmarminge View on GitHub

Showing 3 changed files with 115 additions and 9 deletions.

x
0 of 3 viewed, 3 left
26 additions and 9 deletionsapps/web/src/components/ChatView.tsxModified
View file
@@ -45,6 +45,7 @@
formatElapsed,
formatTimestamp,
} from "../session-logic";
+import { isScrollContainerNearBottom } from "../chat-scroll";
import { useStore } from "../store";
import type { ChatImageAttachment } from "../types";
import BranchToolbar from "./BranchToolbar";
@@ -145,6 +146,7 @@
const [terminalFocusRequestId, setTerminalFocusRequestId] = useState(0);
const messagesScrollRef = useRef<HTMLDivElement>(null);
const messagesEndRef = useRef<HTMLDivElement>(null);
+ const shouldAutoScrollRef = useRef(true);
const textareaRef = useRef<HTMLTextAreaElement>(null);
const composerImagesRef = useRef<ComposerImageAttachment[]>([]);
const dragDepthRef = useRef(0);
@@ -366,19 +368,30 @@
// Auto-scroll on new messages
const messageCount = activeThread?.messages.length ?? 0;
const workLogCount = workLogEntries.length;
- useLayoutEffect(() => {
- if (!activeThread?.id) return;
+ const scrollMessagesToBottom = useCallback((behavior: ScrollBehavior = "auto") => {
const scrollContainer = messagesScrollRef.current;
if (!scrollContainer) return;
- scrollContainer.scrollTop = scrollContainer.scrollHeight;
- }, [activeThread?.id]);
+ scrollContainer.scrollTo({ top: scrollContainer.scrollHeight, behavior });