mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
fix(sessions): scroll to the latest message once the transcript is ready
This commit is contained in:
1 parent
055e4bbf55
commit
577d7e8b5b
4 files changed
+157
-50
No files matched your search
@@ -76,29 +76,29 @@ describe('useAutoScroll', () => {
|
||||
time: { created: Date.now() },
|
||||
} as SessionMessageInfo)
|
||||
|
||||
const setupHook = (messages: SessionMessageInfo[], sessionId = 'session-1') => {
|
||||
const setupHook = (
|
||||
messages: SessionMessageInfo[],
|
||||
options: {
|
||||
sessionId?: string
|
||||
isContentReady?: boolean
|
||||
container?: HTMLDivElement | null
|
||||
} = {}
|
||||
) => {
|
||||
containerHarness = createScrollContainer()
|
||||
const containerRef = { current: containerHarness.div }
|
||||
const onScrollStateChange = vi.fn()
|
||||
|
||||
const initialProps = {
|
||||
container: options.container === undefined ? containerHarness.div : options.container,
|
||||
messages,
|
||||
sessionId: options.sessionId ?? 'session-1',
|
||||
contentVersion: messages.length,
|
||||
isContentReady: options.isContentReady ?? true,
|
||||
onScrollStateChange,
|
||||
}
|
||||
|
||||
const renderResult = renderHook(
|
||||
(props) =>
|
||||
useAutoScroll({
|
||||
containerRef: props.containerRef,
|
||||
messages: props.messages,
|
||||
sessionId: props.sessionId,
|
||||
contentVersion: props.contentVersion,
|
||||
onScrollStateChange: props.onScrollStateChange,
|
||||
}),
|
||||
{
|
||||
initialProps: {
|
||||
containerRef,
|
||||
messages,
|
||||
sessionId,
|
||||
contentVersion: messages.length,
|
||||
onScrollStateChange,
|
||||
},
|
||||
}
|
||||
(props: typeof initialProps) => useAutoScroll(props),
|
||||
{ initialProps }
|
||||
)
|
||||
|
||||
return { renderResult, containerHarness, onScrollStateChange }
|
||||
@@ -131,7 +131,8 @@ describe('useAutoScroll', () => {
|
||||
|
||||
act(() => {
|
||||
renderResult.rerender({
|
||||
containerRef: { current: containerHarness.div },
|
||||
container: containerHarness.div,
|
||||
isContentReady: true,
|
||||
messages: newMessages,
|
||||
sessionId: 'session-1',
|
||||
contentVersion: newMessages.length,
|
||||
@@ -165,7 +166,8 @@ describe('useAutoScroll', () => {
|
||||
|
||||
act(() => {
|
||||
renderResult.rerender({
|
||||
containerRef: { current: containerHarness.div },
|
||||
container: containerHarness.div,
|
||||
isContentReady: true,
|
||||
messages,
|
||||
sessionId: 'session-1',
|
||||
contentVersion: messages.length + 1,
|
||||
@@ -362,7 +364,8 @@ describe('useAutoScroll', () => {
|
||||
|
||||
act(() => {
|
||||
renderResult.rerender({
|
||||
containerRef: { current: containerHarness.div },
|
||||
container: containerHarness.div,
|
||||
isContentReady: true,
|
||||
messages,
|
||||
sessionId: 'session-1',
|
||||
contentVersion: messages.length + 1,
|
||||
@@ -401,7 +404,8 @@ describe('useAutoScroll', () => {
|
||||
|
||||
act(() => {
|
||||
renderResult.rerender({
|
||||
containerRef: { current: containerHarness.div },
|
||||
container: containerHarness.div,
|
||||
isContentReady: true,
|
||||
messages,
|
||||
sessionId: 'session-1',
|
||||
contentVersion: messages.length + 1,
|
||||
@@ -436,7 +440,8 @@ describe('useAutoScroll', () => {
|
||||
|
||||
act(() => {
|
||||
renderResult.rerender({
|
||||
containerRef: { current: containerHarness.div },
|
||||
container: containerHarness.div,
|
||||
isContentReady: true,
|
||||
messages,
|
||||
sessionId: 'session-1',
|
||||
contentVersion: messages.length + 1,
|
||||
@@ -495,7 +500,8 @@ describe('useAutoScroll', () => {
|
||||
|
||||
act(() => {
|
||||
renderResult.rerender({
|
||||
containerRef: { current: containerHarness.div },
|
||||
container: containerHarness.div,
|
||||
isContentReady: true,
|
||||
messages: newMessages,
|
||||
sessionId: 'session-1',
|
||||
contentVersion: newMessages.length,
|
||||
@@ -537,7 +543,8 @@ describe('useAutoScroll', () => {
|
||||
|
||||
act(() => {
|
||||
renderResult.rerender({
|
||||
containerRef: { current: containerHarness.div },
|
||||
container: containerHarness.div,
|
||||
isContentReady: true,
|
||||
messages: olderPage,
|
||||
sessionId: 'session-1',
|
||||
contentVersion: olderPage.length,
|
||||
@@ -576,7 +583,8 @@ describe('useAutoScroll', () => {
|
||||
|
||||
act(() => {
|
||||
renderResult.rerender({
|
||||
containerRef: { current: containerHarness.div },
|
||||
container: containerHarness.div,
|
||||
isContentReady: true,
|
||||
messages: appended,
|
||||
sessionId: 'session-1',
|
||||
contentVersion: appended.length,
|
||||
@@ -593,7 +601,7 @@ describe('useAutoScroll', () => {
|
||||
|
||||
it('resets state when sessionId changes', () => {
|
||||
const messages = [createMessage('1', 'user')]
|
||||
const { renderResult, containerHarness } = setupHook(messages, 'session-1')
|
||||
const { renderResult, containerHarness } = setupHook(messages, { sessionId: 'session-1' })
|
||||
|
||||
act(() => {
|
||||
containerHarness.div.dispatchEvent(
|
||||
@@ -608,7 +616,8 @@ describe('useAutoScroll', () => {
|
||||
|
||||
act(() => {
|
||||
renderResult.rerender({
|
||||
containerRef: { current: containerHarness.div },
|
||||
container: containerHarness.div,
|
||||
isContentReady: true,
|
||||
messages: newMessages,
|
||||
sessionId: 'session-2',
|
||||
contentVersion: newMessages.length,
|
||||
@@ -622,4 +631,72 @@ describe('useAutoScroll', () => {
|
||||
|
||||
expect(containerHarness.getScrollTop()).toBe(containerHarness.div.scrollHeight - containerHarness.div.clientHeight)
|
||||
})
|
||||
|
||||
it('scrolls to bottom and tracks user scroll once the container mounts after messages arrive', () => {
|
||||
const messages = [createMessage('1', 'user'), createMessage('2', 'assistant')]
|
||||
const { renderResult, containerHarness, onScrollStateChange } = setupHook(messages, { container: null })
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(100)
|
||||
})
|
||||
|
||||
expect(containerHarness.getScrollTop()).toBe(0)
|
||||
|
||||
act(() => {
|
||||
renderResult.rerender({
|
||||
container: containerHarness.div,
|
||||
isContentReady: true,
|
||||
messages,
|
||||
sessionId: 'session-1',
|
||||
contentVersion: messages.length,
|
||||
onScrollStateChange,
|
||||
})
|
||||
})
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(100)
|
||||
})
|
||||
|
||||
expect(containerHarness.getScrollTop()).toBe(containerHarness.div.scrollHeight - containerHarness.div.clientHeight)
|
||||
|
||||
act(() => {
|
||||
containerHarness.setScrollTop(100)
|
||||
containerHarness.div.dispatchEvent(
|
||||
new WheelEvent('wheel', {
|
||||
deltaY: -50,
|
||||
bubbles: true,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
expect(onScrollStateChange).toHaveBeenCalledWith(true)
|
||||
})
|
||||
|
||||
it('waits for ready content before the initial scroll', () => {
|
||||
const messages = [createMessage('1', 'user'), createMessage('2', 'assistant')]
|
||||
const { renderResult, containerHarness, onScrollStateChange } = setupHook(messages, { isContentReady: false })
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(100)
|
||||
})
|
||||
|
||||
expect(containerHarness.getScrollTop()).toBe(0)
|
||||
|
||||
act(() => {
|
||||
renderResult.rerender({
|
||||
container: containerHarness.div,
|
||||
isContentReady: true,
|
||||
messages,
|
||||
sessionId: 'session-1',
|
||||
contentVersion: messages.length,
|
||||
onScrollStateChange,
|
||||
})
|
||||
})
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(100)
|
||||
})
|
||||
|
||||
expect(containerHarness.getScrollTop()).toBe(containerHarness.div.scrollHeight - containerHarness.div.clientHeight)
|
||||
})
|
||||
})
|
||||
@@ -7,10 +7,11 @@ const SHOW_SCROLL_BUTTON_THRESHOLD_PX = 120
|
||||
const SCROLL_TO_BOTTOM_FRAME_COUNT = 2
|
||||
|
||||
interface UseAutoScrollOptions {
|
||||
containerRef?: React.RefObject<HTMLDivElement | null>
|
||||
container?: HTMLDivElement | null
|
||||
messages?: SessionMessageInfo[]
|
||||
sessionId?: string
|
||||
contentVersion?: number
|
||||
isContentReady: boolean
|
||||
onScrollStateChange?: (isScrolledUp: boolean) => void
|
||||
}
|
||||
|
||||
@@ -20,10 +21,11 @@ interface UseAutoScrollReturn {
|
||||
}
|
||||
|
||||
export function useAutoScroll({
|
||||
containerRef,
|
||||
container,
|
||||
messages,
|
||||
sessionId,
|
||||
contentVersion,
|
||||
isContentReady,
|
||||
onScrollStateChange
|
||||
}: UseAutoScrollOptions): UseAutoScrollReturn {
|
||||
const lastMessageCountRef = useRef(0)
|
||||
@@ -42,7 +44,7 @@ export function useAutoScroll({
|
||||
onScrollStateChangeRef.current = onScrollStateChange
|
||||
|
||||
const scrollToBottom = useCallback(() => {
|
||||
if (!containerRef?.current) return
|
||||
if (!container) return
|
||||
userScrolledAtRef.current = 0
|
||||
userDisengagedRef.current = false
|
||||
isScrollButtonVisibleRef.current = false
|
||||
@@ -52,8 +54,7 @@ export function useAutoScroll({
|
||||
let frameCount = 0
|
||||
const scrollAfterLayout = () => {
|
||||
if (scrollRequestIdRef.current !== scrollRequestId) return
|
||||
if (!containerRef?.current) return
|
||||
containerRef.current.scrollTop = containerRef.current.scrollHeight
|
||||
container.scrollTop = container.scrollHeight
|
||||
frameCount += 1
|
||||
if (frameCount < SCROLL_TO_BOTTOM_FRAME_COUNT) {
|
||||
requestAnimationFrame(scrollAfterLayout)
|
||||
@@ -61,10 +62,9 @@ export function useAutoScroll({
|
||||
}
|
||||
requestAnimationFrame(scrollAfterLayout)
|
||||
onScrollStateChangeRef.current?.(false)
|
||||
}, [containerRef])
|
||||
}, [container])
|
||||
|
||||
const markDisengaged = useCallback(() => {
|
||||
const container = containerRef?.current
|
||||
if (!container) return
|
||||
const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight
|
||||
userScrolledAtRef.current = Date.now()
|
||||
@@ -74,16 +74,15 @@ export function useAutoScroll({
|
||||
onScrollStateChangeRef.current?.(true)
|
||||
}
|
||||
userDisengagedRef.current = true
|
||||
}, [containerRef])
|
||||
}, [container])
|
||||
|
||||
const scrollByUser = useCallback((delta: number) => {
|
||||
const container = containerRef?.current
|
||||
if (!container) return
|
||||
if (delta < 0) {
|
||||
markDisengaged()
|
||||
}
|
||||
container.scrollBy({ top: delta })
|
||||
}, [containerRef, markDisengaged])
|
||||
}, [container, markDisengaged])
|
||||
|
||||
useEffect(() => {
|
||||
lastMessageCountRef.current = 0
|
||||
@@ -96,12 +95,10 @@ export function useAutoScroll({
|
||||
}, [sessionId])
|
||||
|
||||
useEffect(() => {
|
||||
const container = containerRef?.current
|
||||
if (!container) return
|
||||
|
||||
const updateScrollButtonVisibility = () => {
|
||||
const container = containerRef?.current
|
||||
if (!container || !userDisengagedRef.current || isScrollButtonVisibleRef.current) return
|
||||
if (!userDisengagedRef.current || isScrollButtonVisibleRef.current) return
|
||||
const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight
|
||||
if (distanceFromBottom > SHOW_SCROLL_BUTTON_THRESHOLD_PX) {
|
||||
isScrollButtonVisibleRef.current = true
|
||||
@@ -206,10 +203,10 @@ export function useAutoScroll({
|
||||
container.removeEventListener('keydown', handleKeyDown)
|
||||
container.removeEventListener('scroll', handleScroll)
|
||||
}
|
||||
}, [containerRef, sessionId, messages, markDisengaged])
|
||||
}, [container, sessionId, messages, markDisengaged])
|
||||
|
||||
useEffect(() => {
|
||||
if (!containerRef?.current || !messages) return
|
||||
if (!container || !messages) return
|
||||
|
||||
const currentCount = messages.length
|
||||
const prevCount = lastMessageCountRef.current
|
||||
@@ -219,6 +216,8 @@ export function useAutoScroll({
|
||||
const previousLastMessageId = lastMessageIdRef.current
|
||||
lastMessageIdRef.current = lastMessage?.id
|
||||
|
||||
if (!isContentReady) return
|
||||
|
||||
if (!hasInitialScrolledRef.current && currentCount > 0) {
|
||||
hasInitialScrolledRef.current = true
|
||||
scrollToBottom()
|
||||
@@ -238,7 +237,7 @@ export function useAutoScroll({
|
||||
const recentlyScrolled = timeSinceUserScroll < SCROLL_LOCK_MS
|
||||
|
||||
if (userDisengagedRef.current) {
|
||||
const distanceFromBottom = containerRef.current.scrollHeight - containerRef.current.scrollTop - containerRef.current.clientHeight
|
||||
const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight
|
||||
if (!isScrollButtonVisibleRef.current && distanceFromBottom > SHOW_SCROLL_BUTTON_THRESHOLD_PX) {
|
||||
isScrollButtonVisibleRef.current = true
|
||||
onScrollStateChangeRef.current?.(true)
|
||||
@@ -251,7 +250,7 @@ export function useAutoScroll({
|
||||
}
|
||||
|
||||
scrollToBottom()
|
||||
}, [messages, containerRef, scrollToBottom, contentVersion])
|
||||
}, [messages, container, isContentReady, scrollToBottom, contentVersion])
|
||||
|
||||
return { scrollToBottom, scrollByUser }
|
||||
}
|
||||
@@ -136,6 +136,7 @@ export function SessionDetail() {
|
||||
const { preferences, updateSettings } = useSettings();
|
||||
const { open: openSettings, openTab: openSettingsTab } = useSettingsDialog();
|
||||
const messageContainerRef = useRef<HTMLDivElement>(null);
|
||||
const [messageContainer, setMessageContainer] = useState<HTMLDivElement | null>(null);
|
||||
const promptInputRef = useRef<PromptInputHandle>(null);
|
||||
const [sessionsDialogOpen, setSessionsDialogOpen] = useState(false);
|
||||
const [fileBrowserOpen, setFileBrowserOpen] = useDialogParam('files');
|
||||
@@ -302,12 +303,19 @@ export function SessionDetail() {
|
||||
}, [messagePickerMode, sessionId, revertMessageID, serverId]);
|
||||
|
||||
const messagesContentVersion = useMemo(() => getMessagesContentVersion(messages), [messages]);
|
||||
const isTranscriptLoading = repoLoading || sessionLoading || messagesLoading;
|
||||
|
||||
const attachMessageContainer = useCallback((element: HTMLDivElement | null) => {
|
||||
messageContainerRef.current = element;
|
||||
setMessageContainer(element);
|
||||
}, []);
|
||||
|
||||
const { scrollToBottom, scrollByUser } = useAutoScroll({
|
||||
containerRef: messageContainerRef,
|
||||
container: messageContainer,
|
||||
messages,
|
||||
sessionId,
|
||||
contentVersion: messagesContentVersion,
|
||||
isContentReady: !isTranscriptLoading,
|
||||
onScrollStateChange: setShowScrollButton
|
||||
});
|
||||
|
||||
@@ -861,8 +869,8 @@ export function SessionDetail() {
|
||||
|
||||
<div className="flex flex-1 min-h-0">
|
||||
<div className="relative flex-1 min-w-0 overflow-hidden flex flex-col">
|
||||
<div key={sessionId} data-testid="session-message-scroll" ref={messageContainerRef} onScroll={handleMessageScroll} className="flex-1 overflow-y-auto overflow-x-hidden overscroll-contain [mask-image:linear-gradient(to_bottom,transparent,black_16px,black)]" style={{ paddingBottom: promptOverlayHeight + inputBottomOffset + PROMPT_OVERLAY_CLEARANCE_PX }}>
|
||||
{repoLoading || sessionLoading || messagesLoading ? (
|
||||
<div key={sessionId} data-testid="session-message-scroll" ref={attachMessageContainer} onScroll={handleMessageScroll} className="flex-1 overflow-y-auto overflow-x-hidden overscroll-contain [mask-image:linear-gradient(to_bottom,transparent,black_16px,black)]" style={{ paddingBottom: promptOverlayHeight + inputBottomOffset + PROMPT_OVERLAY_CLEARANCE_PX }}>
|
||||
{isTranscriptLoading ? (
|
||||
<MessageSkeleton />
|
||||
) : sessionDirectory ? (
|
||||
<MessageThread
|
||||
|
||||
@@ -295,6 +295,7 @@ describe('SessionDetail scroll floating button', () => {
|
||||
showScrollButton: boolean
|
||||
promptContent?: boolean
|
||||
sessionActive?: boolean
|
||||
messagesLoading?: boolean
|
||||
}) => {
|
||||
mocks.useMobile.mockReturnValue(opts.mobile)
|
||||
mocks.useSessionStatusForSession.mockReturnValue({ type: opts.sessionActive ? 'busy' : 'idle' })
|
||||
@@ -302,7 +303,7 @@ describe('SessionDetail scroll floating button', () => {
|
||||
messages: [],
|
||||
pending: [],
|
||||
status: opts.sessionActive ? 'busy' : 'idle',
|
||||
isLoading: false,
|
||||
isLoading: opts.messagesLoading ?? false,
|
||||
fetchOlder: vi.fn(),
|
||||
hasOlder: false,
|
||||
})
|
||||
@@ -460,4 +461,26 @@ describe('SessionDetail scroll floating button', () => {
|
||||
reportObservedHeight(140)
|
||||
expect(paddingBottom()).toBe('156px')
|
||||
})
|
||||
|
||||
it('marks the transcript content as not ready while messages load and ready afterwards', async () => {
|
||||
const { buildTree, rerender } = renderWith({ mobile: true, showScrollButton: false, messagesLoading: true })
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mocks.useAutoScroll.mock.calls.at(-1)?.[0]).toMatchObject({ isContentReady: false })
|
||||
})
|
||||
|
||||
mocks.useSessionTranscript.mockReturnValue({
|
||||
messages: [],
|
||||
pending: [],
|
||||
status: 'idle',
|
||||
isLoading: false,
|
||||
fetchOlder: vi.fn(),
|
||||
hasOlder: false,
|
||||
})
|
||||
rerender(buildTree())
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mocks.useAutoScroll.mock.calls.at(-1)?.[0]).toMatchObject({ isContentReady: true })
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user