mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Consolidate all scroll logic into useAutoScroll hook
This commit is contained in:
1 parent
6c514120c3
commit
2d7de3496f
3 files changed
+63
-93
No files matched your search
@@ -1,5 +1,4 @@
|
||||
import { memo, useRef, useEffect } from 'react'
|
||||
import { useMessages } from '@/hooks/useOpenCode'
|
||||
import { memo } from 'react'
|
||||
import { MessagePart } from './MessagePart'
|
||||
import type { MessageWithParts } from '@/api/types'
|
||||
|
||||
@@ -15,10 +14,8 @@ interface MessageThreadProps {
|
||||
opcodeUrl: string
|
||||
sessionID: string
|
||||
directory?: string
|
||||
messages?: MessageWithParts[]
|
||||
onFileClick?: (filePath: string, lineNumber?: number) => void
|
||||
scrollToBottom: () => void
|
||||
setFollowing: (following: boolean) => void
|
||||
isFollowing: () => boolean
|
||||
}
|
||||
|
||||
const isMessageStreaming = (msg: MessageWithParts): boolean => {
|
||||
@@ -31,59 +28,7 @@ const isMessageThinking = (msg: MessageWithParts): boolean => {
|
||||
return msg.parts.length === 0 && isMessageStreaming(msg)
|
||||
}
|
||||
|
||||
export const MessageThread = memo(function MessageThread({ opcodeUrl, sessionID, directory, onFileClick, scrollToBottom, setFollowing, isFollowing }: MessageThreadProps) {
|
||||
const { data: messages, isLoading, error } = useMessages(opcodeUrl, sessionID, directory)
|
||||
const hasInitialScrolledRef = useRef(false)
|
||||
const lastMessageCountRef = useRef(0)
|
||||
|
||||
useEffect(() => {
|
||||
hasInitialScrolledRef.current = false
|
||||
lastMessageCountRef.current = 0
|
||||
}, [sessionID])
|
||||
|
||||
useEffect(() => {
|
||||
if (!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') {
|
||||
setFollowing(true)
|
||||
scrollToBottom()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if (isFollowing()) {
|
||||
scrollToBottom()
|
||||
}
|
||||
}, [messages, scrollToBottom, setFollowing, isFollowing])
|
||||
|
||||
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">
|
||||
|
||||
@@ -2,45 +2,47 @@ import { useRef, useEffect, useCallback } from 'react'
|
||||
|
||||
const SCROLL_THRESHOLD = 100
|
||||
|
||||
interface UseAutoScrollOptions {
|
||||
interface MessageInfo {
|
||||
role: string
|
||||
}
|
||||
|
||||
interface Message {
|
||||
info: MessageInfo
|
||||
}
|
||||
|
||||
interface UseAutoScrollOptions<T extends Message> {
|
||||
containerRef?: React.RefObject<HTMLDivElement | null>
|
||||
dependency?: unknown
|
||||
messages?: T[]
|
||||
sessionId?: string
|
||||
onScrollStateChange?: (isScrolledUp: boolean) => void
|
||||
}
|
||||
|
||||
interface UseAutoScrollReturn {
|
||||
scrollToBottom: () => void
|
||||
setFollowing: (following: boolean) => void
|
||||
isFollowing: () => boolean
|
||||
}
|
||||
|
||||
export function useAutoScroll({
|
||||
export function useAutoScroll<T extends Message>({
|
||||
containerRef,
|
||||
dependency,
|
||||
messages,
|
||||
sessionId,
|
||||
onScrollStateChange
|
||||
}: UseAutoScrollOptions): UseAutoScrollReturn {
|
||||
}: UseAutoScrollOptions<T>): UseAutoScrollReturn {
|
||||
const isFollowingRef = useRef(true)
|
||||
const isProgrammaticScrollRef = useRef(false)
|
||||
const lastMessageCountRef = useRef(0)
|
||||
const hasInitialScrolledRef = 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])
|
||||
|
||||
const setFollowing = useCallback((following: boolean) => {
|
||||
isFollowingRef.current = following
|
||||
}, [])
|
||||
|
||||
const isFollowing = useCallback(() => {
|
||||
return isFollowingRef.current
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
isFollowingRef.current = true
|
||||
}, [dependency])
|
||||
lastMessageCountRef.current = 0
|
||||
hasInitialScrolledRef.current = false
|
||||
}, [sessionId])
|
||||
|
||||
useEffect(() => {
|
||||
if (!containerRef?.current) return
|
||||
@@ -48,11 +50,6 @@ export function useAutoScroll({
|
||||
const container = containerRef.current
|
||||
|
||||
const handleScroll = () => {
|
||||
if (isProgrammaticScrollRef.current) {
|
||||
isProgrammaticScrollRef.current = false
|
||||
return
|
||||
}
|
||||
|
||||
const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight
|
||||
const isScrolledUp = distanceFromBottom > SCROLL_THRESHOLD
|
||||
|
||||
@@ -67,5 +64,32 @@ export function useAutoScroll({
|
||||
return () => container.removeEventListener('scroll', handleScroll)
|
||||
}, [containerRef, onScrollStateChange])
|
||||
|
||||
return { scrollToBottom, setFollowing, isFollowing }
|
||||
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 }
|
||||
}
|
||||
@@ -10,7 +10,7 @@ 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";
|
||||
@@ -34,25 +34,28 @@ export function SessionDetail() {
|
||||
const [selectedFilePath, setSelectedFilePath] = useState<string | undefined>();
|
||||
const [showScrollButton, setShowScrollButton] = useState(false);
|
||||
|
||||
const { scrollToBottom, setFollowing, isFollowing } = useAutoScroll({
|
||||
containerRef: messageContainerRef,
|
||||
dependency: sessionId,
|
||||
onScrollStateChange: setShowScrollButton
|
||||
});
|
||||
|
||||
const { data: repo, isLoading: repoLoading } = useQuery({
|
||||
queryKey: ["repo", repoId],
|
||||
queryFn: () => getRepo(repoId),
|
||||
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,
|
||||
@@ -176,10 +179,8 @@ const { currentPermission, pendingCount, dismissPermission } = usePermissionRequ
|
||||
opcodeUrl={opcodeUrl}
|
||||
sessionID={sessionId}
|
||||
directory={repoDirectory}
|
||||
messages={messages}
|
||||
onFileClick={handleFileClick}
|
||||
scrollToBottom={scrollToBottom}
|
||||
setFollowing={setFollowing}
|
||||
isFollowing={isFollowing}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user