Merge pull request #7 from chriswritescode-dev/fix-scroll

Fix scroll
This commit is contained in:
Chris Scott authored and GitHub committed 2025-11-29 12:17:11 -05:00
commit 15ce1ba9c1
3 files changed
+116 -78

No files matched your search

@@ -1,5 +1,4 @@
import { memo, useRef, useEffect, useCallback } from 'react'
import { useMessages } from '@/hooks/useOpenCode'
import { memo } from 'react'
import { MessagePart } from './MessagePart'
import type { MessageWithParts } from '@/api/types'
@@ -15,9 +14,8 @@ interface MessageThreadProps {
opcodeUrl: string
sessionID: string
directory?: string
messages?: MessageWithParts[]
onFileClick?: (filePath: string, lineNumber?: number) => void
containerRef?: React.RefObject<HTMLDivElement | null>
onScrollStateChange?: (isScrolledUp: boolean) => void
}
const isMessageStreaming = (msg: MessageWithParts): boolean => {
@@ -30,68 +28,7 @@ const isMessageThinking = (msg: MessageWithParts): boolean => {
return msg.parts.length === 0 && isMessageStreaming(msg)
}
const SCROLL_THRESHOLD = 100
export const MessageThread = memo(function MessageThread({ opcodeUrl, sessionID, directory, onFileClick, containerRef, onScrollStateChange }: MessageThreadProps) {
const { data: messages, isLoading, error } = useMessages(opcodeUrl, sessionID, directory)
const userScrolledUpRef = useRef(false)
const hasInitialScrolledRef = useRef(false)
useEffect(() => {
hasInitialScrolledRef.current = false
userScrolledUpRef.current = false
}, [sessionID])
const scrollToBottom = useCallback(() => {
if (!containerRef?.current) return
containerRef.current.scrollTop = containerRef.current.scrollHeight
userScrolledUpRef.current = false
onScrollStateChange?.(false)
}, [containerRef, onScrollStateChange])
useEffect(() => {
if (!containerRef?.current) return
const container = containerRef.current
const handleScroll = () => {
const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight
const isScrolledUp = distanceFromBottom > SCROLL_THRESHOLD
if (userScrolledUpRef.current !== isScrolledUp) {
userScrolledUpRef.current = isScrolledUp
onScrollStateChange?.(isScrolledUp)
}
}
container.addEventListener('scroll', handleScroll, { passive: true })
return () => container.removeEventListener('scroll', handleScroll)
}, [containerRef, onScrollStateChange])
useEffect(() => {
if (!containerRef?.current || !messages) return
if (!hasInitialScrolledRef.current && messages.length > 0) {
hasInitialScrolledRef.current = true
scrollToBottom()
}
}, [messages, containerRef, scrollToBottom])
if (isLoading) {
return (
<div className="flex items-center justify-center h-full text-zinc-500">
Loading messages...
</div>
)
}
if (error) {
return (
<div className="flex items-center justify-center h-full text-red-400">
Error loading messages: {error instanceof Error ? error.message : 'Unknown error'}
</div>
)
}
export const MessageThread = memo(function MessageThread({ messages, onFileClick }: MessageThreadProps) {
if (!messages || messages.length === 0) {
return (
<div className="flex items-center justify-center h-full text-zinc-600">
+99
View File
@@ -0,0 +1,99 @@
import { useRef, useEffect, useCallback } from 'react'
const SCROLL_THRESHOLD = 100
interface MessageInfo {
role: string
}
interface Message {
info: MessageInfo
}
interface UseAutoScrollOptions<T extends Message> {
containerRef?: React.RefObject<HTMLDivElement | null>
messages?: T[]
sessionId?: string
onScrollStateChange?: (isScrolledUp: boolean) => void
}
interface UseAutoScrollReturn {
scrollToBottom: () => void
}
export function useAutoScroll<T extends Message>({
containerRef,
messages,
sessionId,
onScrollStateChange
}: UseAutoScrollOptions<T>): UseAutoScrollReturn {
const isFollowingRef = useRef(true)
const lastMessageCountRef = useRef(0)
const hasInitialScrolledRef = useRef(false)
const isProgrammaticScrollRef = useRef(false)
const scrollToBottom = useCallback(() => {
if (!containerRef?.current) return
isProgrammaticScrollRef.current = true
containerRef.current.scrollTop = containerRef.current.scrollHeight
isFollowingRef.current = true
onScrollStateChange?.(false)
}, [containerRef, onScrollStateChange])
useEffect(() => {
isFollowingRef.current = true
lastMessageCountRef.current = 0
hasInitialScrolledRef.current = false
}, [sessionId])
useEffect(() => {
if (!containerRef?.current) return
const container = containerRef.current
const handleScroll = () => {
if (isProgrammaticScrollRef.current) {
isProgrammaticScrollRef.current = false
return
}
isFollowingRef.current = false
const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight
const isScrolledUp = distanceFromBottom > SCROLL_THRESHOLD
onScrollStateChange?.(isScrolledUp)
}
container.addEventListener('scroll', handleScroll, { passive: true })
return () => container.removeEventListener('scroll', handleScroll)
}, [containerRef, onScrollStateChange])
useEffect(() => {
if (!containerRef?.current || !messages) return
const currentCount = messages.length
const prevCount = lastMessageCountRef.current
lastMessageCountRef.current = currentCount
if (!hasInitialScrolledRef.current && currentCount > 0) {
hasInitialScrolledRef.current = true
scrollToBottom()
return
}
if (currentCount > prevCount) {
const newMessage = messages[currentCount - 1]
if (newMessage?.info.role === 'user') {
isFollowingRef.current = true
scrollToBottom()
return
}
}
if (isFollowingRef.current) {
scrollToBottom()
}
}, [messages, containerRef, scrollToBottom])
return { scrollToBottom }
}
+14 -12
View File
@@ -10,13 +10,14 @@ import { SessionList } from "@/components/session/SessionList";
import { PermissionRequestDialog } from "@/components/session/PermissionRequestDialog";
import { FileBrowserSheet } from "@/components/file-browser/FileBrowserSheet";
import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog";
import { useSession, useAbortSession, useUpdateSession, useOpenCodeClient } from "@/hooks/useOpenCode";
import { useSession, useAbortSession, useUpdateSession, useOpenCodeClient, useMessages } from "@/hooks/useOpenCode";
import { OPENCODE_API_ENDPOINT } from "@/config";
import { useSSE } from "@/hooks/useSSE";
import { useSettings } from "@/hooks/useSettings";
import { useKeyboardShortcuts } from "@/hooks/useKeyboardShortcuts";
import { useSettingsDialog } from "@/hooks/useSettingsDialog";
import { usePermissionRequests } from "@/hooks/usePermissionRequests";
import { useAutoScroll } from "@/hooks/useAutoScroll";
import { useEffect, useRef, useCallback } from "react";
import { Loader2 } from "lucide-react";
import type { PermissionResponse } from "@/api/types";
@@ -39,13 +40,22 @@ export function SessionDetail() {
enabled: !!repoId,
});
const { currentPermission, pendingCount, dismissPermission } = usePermissionRequests();
const { currentPermission, pendingCount, dismissPermission } = usePermissionRequests();
const opcodeUrl = OPENCODE_API_ENDPOINT;
const openCodeClient = useOpenCodeClient(opcodeUrl, repo?.fullPath);
const repoDirectory = repo?.fullPath;
const { data: messages } = useMessages(opcodeUrl, sessionId, repoDirectory);
const { scrollToBottom } = useAutoScroll({
containerRef: messageContainerRef,
messages,
sessionId,
onScrollStateChange: setShowScrollButton
});
const { data: session, isLoading: sessionLoading } = useSession(
opcodeUrl,
sessionId,
@@ -112,13 +122,6 @@ const { currentPermission, pendingCount, dismissPermission } = usePermissionRequ
setSelectedFilePath(undefined)
}, []);
const handleScrollToBottom = useCallback(() => {
if (messageContainerRef.current) {
messageContainerRef.current.scrollTop = messageContainerRef.current.scrollHeight;
setShowScrollButton(false);
}
}, []);
const handlePermissionResponse = useCallback(async (
permissionID: string,
permissionSessionID: string,
@@ -176,9 +179,8 @@ const { currentPermission, pendingCount, dismissPermission } = usePermissionRequ
opcodeUrl={opcodeUrl}
sessionID={sessionId}
directory={repoDirectory}
messages={messages}
onFileClick={handleFileClick}
containerRef={messageContainerRef}
onScrollStateChange={setShowScrollButton}
/>
)}
</div>
@@ -190,7 +192,7 @@ const { currentPermission, pendingCount, dismissPermission } = usePermissionRequ
sessionID={sessionId}
disabled={!isConnected}
showScrollButton={showScrollButton}
onScrollToBottom={handleScrollToBottom}
onScrollToBottom={scrollToBottom}
onShowModelsDialog={() => setModelDialogOpen(true)}
onShowSessionsDialog={() => setSessionsDialogOpen(true)}
onShowHelpDialog={() => {