mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
fix(frontend): improve mobile scroll to bottom
This commit is contained in:
1 parent
03bd3dac95
commit
d4129afd58
4 files changed
+177
-33
No files matched your search
@@ -230,6 +230,7 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
|
||||
setIsVoiceAutoSendPending(false)
|
||||
|
||||
if (isStreamingResponse) {
|
||||
onScrollToBottom()
|
||||
const parts = parsePromptToParts(prompt, attachedFiles, imageAttachments)
|
||||
const agentUsed = selectedAgent || currentMode
|
||||
sendPrompt.mutate({
|
||||
@@ -294,6 +295,8 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
|
||||
variant: currentVariant
|
||||
})
|
||||
|
||||
onScrollToBottom()
|
||||
|
||||
setStoredAgent(sessionID, agentUsed)
|
||||
if (model) {
|
||||
setStoredModel({ providerID: model.providerID, modelID: model.modelID })
|
||||
@@ -1210,32 +1213,39 @@ return (
|
||||
<div className="flex gap-1.5 md:gap-2 items-center justify-between">
|
||||
<div className="flex gap-1.5 md:gap-2 items-center min-w-0">
|
||||
{showMobileScrollButton ? (
|
||||
<button
|
||||
type="button"
|
||||
onClick={onScrollToBottom}
|
||||
className="flex items-center gap-1.5 px-3 min-h-[36px] rounded-lg text-xs font-medium border bg-zinc-950/80 hover:bg-zinc-900/90 text-blue-300 hover:text-blue-200 border-blue-400/20 shadow-md backdrop-blur-md transition-all duration-200 active:scale-95 ring-1 ring-blue-400/15"
|
||||
title="Scroll to bottom"
|
||||
aria-label="Scroll to bottom"
|
||||
>
|
||||
<ArrowDown className="w-4 h-4" />
|
||||
<span>Latest</span>
|
||||
</button>
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
onClick={onScrollToBottom}
|
||||
className="flex items-center gap-1.5 px-3 min-h-[36px] rounded-lg text-xs font-medium border bg-zinc-950/80 hover:bg-zinc-900/90 text-blue-300 hover:text-blue-200 border-blue-400/20 shadow-md backdrop-blur-md transition-all duration-200 active:scale-95 ring-1 ring-blue-400/15"
|
||||
title="Scroll to bottom"
|
||||
aria-label="Scroll to bottom"
|
||||
>
|
||||
<ArrowDown className="w-4 h-4" />
|
||||
<span>Latest</span>
|
||||
</button>
|
||||
{isSessionActive && (
|
||||
<div className="px-2.5 py-1.5 rounded-lg text-xs font-medium text-muted-foreground max-w-[120px]">
|
||||
<SessionStatusIndicator sessionID={sessionID} showLabel />
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
) : (
|
||||
<AgentQuickSelect
|
||||
opcodeUrl={opcodeUrl}
|
||||
directory={directory}
|
||||
currentAgent={currentMode}
|
||||
onAgentChange={handleAgentChange}
|
||||
isBashMode={isBashMode}
|
||||
disabled={disabled}
|
||||
/>
|
||||
)}
|
||||
{isSessionActive ? (
|
||||
<>
|
||||
<AgentQuickSelect
|
||||
opcodeUrl={opcodeUrl}
|
||||
directory={directory}
|
||||
currentAgent={currentMode}
|
||||
onAgentChange={handleAgentChange}
|
||||
isBashMode={isBashMode}
|
||||
disabled={disabled}
|
||||
/>
|
||||
{isSessionActive ? (
|
||||
<div className="px-2.5 py-1.5 md:px-3 md:py-2 rounded-lg text-xs md:text-sm font-medium text-muted-foreground max-w-[120px] md:max-w-[180px]">
|
||||
<SessionStatusIndicator sessionID={sessionID} showLabel />
|
||||
</div>
|
||||
) : (
|
||||
!hideSecondaryButtons && !showMobileScrollButton && (
|
||||
!hideSecondaryButtons && (
|
||||
<ModelQuickSelect
|
||||
opcodeUrl={opcodeUrl}
|
||||
directory={directory}
|
||||
@@ -1250,8 +1260,9 @@ return (
|
||||
</button>
|
||||
</ModelQuickSelect>
|
||||
)
|
||||
)}
|
||||
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-1.5 md:gap-2 flex-shrink-0">
|
||||
{!isMobile && (
|
||||
|
||||
@@ -307,6 +307,75 @@ describe('useAutoScroll', () => {
|
||||
expect(currentScrollTop).not.toBe(containerHarness.div.scrollHeight - containerHarness.div.clientHeight)
|
||||
})
|
||||
|
||||
it('disengages when mobile touch scrolls upward without pointer events', () => {
|
||||
const messages = [createMessage('1', 'user')]
|
||||
const { renderResult, containerHarness, onScrollStateChange } = setupHook(messages)
|
||||
|
||||
act(() => {
|
||||
containerHarness.setScrollTop(100)
|
||||
containerHarness.div.dispatchEvent(new TouchEvent('touchstart', {
|
||||
touches: [{ clientY: 200 } as Touch],
|
||||
bubbles: true,
|
||||
}))
|
||||
containerHarness.div.dispatchEvent(new TouchEvent('touchmove', {
|
||||
touches: [{ clientY: 260 } as Touch],
|
||||
bubbles: true,
|
||||
}))
|
||||
})
|
||||
|
||||
expect(onScrollStateChange).toHaveBeenCalledWith(true)
|
||||
|
||||
containerHarness.setScrollHeight(containerHarness.div.scrollHeight + 200)
|
||||
|
||||
act(() => {
|
||||
renderResult.rerender({
|
||||
containerRef: { current: containerHarness.div },
|
||||
messages,
|
||||
sessionId: 'session-1',
|
||||
contentVersion: messages.length + 1,
|
||||
onScrollStateChange,
|
||||
})
|
||||
vi.runOnlyPendingTimers()
|
||||
})
|
||||
|
||||
expect(containerHarness.getScrollTop()).toBe(100)
|
||||
})
|
||||
|
||||
it('shows scroll button when streaming growth carries disengaged user past threshold', () => {
|
||||
const messages = [createMessage('1', 'user')]
|
||||
const { renderResult, containerHarness, onScrollStateChange } = setupHook(messages)
|
||||
const nearBottomPosition = containerHarness.div.scrollHeight - containerHarness.div.clientHeight - 60
|
||||
|
||||
act(() => {
|
||||
containerHarness.setScrollTop(nearBottomPosition)
|
||||
containerHarness.div.dispatchEvent(new TouchEvent('touchstart', {
|
||||
touches: [{ clientY: 200 } as Touch],
|
||||
bubbles: true,
|
||||
}))
|
||||
containerHarness.div.dispatchEvent(new TouchEvent('touchmove', {
|
||||
touches: [{ clientY: 260 } as Touch],
|
||||
bubbles: true,
|
||||
}))
|
||||
})
|
||||
|
||||
expect(onScrollStateChange).not.toHaveBeenCalledWith(true)
|
||||
|
||||
containerHarness.setScrollHeight(containerHarness.div.scrollHeight + 200)
|
||||
|
||||
act(() => {
|
||||
renderResult.rerender({
|
||||
containerRef: { current: containerHarness.div },
|
||||
messages,
|
||||
sessionId: 'session-1',
|
||||
contentVersion: messages.length + 1,
|
||||
onScrollStateChange,
|
||||
})
|
||||
})
|
||||
|
||||
expect(onScrollStateChange).toHaveBeenCalledWith(true)
|
||||
expect(containerHarness.getScrollTop()).toBe(nearBottomPosition)
|
||||
})
|
||||
|
||||
it('re-engages when user scrolls back to within threshold', () => {
|
||||
const messages = [createMessage('1', 'user')]
|
||||
const { containerHarness, onScrollStateChange } = setupHook(messages)
|
||||
|
||||
@@ -31,6 +31,8 @@ export function useAutoScroll({
|
||||
const userDisengagedRef = useRef(false)
|
||||
const pointerStartYRef = useRef<number | null>(null)
|
||||
const pointerActiveRef = useRef(false)
|
||||
const touchStartYRef = useRef<number | null>(null)
|
||||
const touchActiveRef = useRef(false)
|
||||
const onScrollStateChangeRef = useRef(onScrollStateChange)
|
||||
const scrollRequestIdRef = useRef(0)
|
||||
const isScrollButtonVisibleRef = useRef(false)
|
||||
@@ -68,6 +70,8 @@ export function useAutoScroll({
|
||||
hasInitialScrolledRef.current = false
|
||||
userScrolledAtRef.current = 0
|
||||
userDisengagedRef.current = false
|
||||
touchStartYRef.current = null
|
||||
touchActiveRef.current = false
|
||||
}, [sessionId])
|
||||
|
||||
useEffect(() => {
|
||||
@@ -87,6 +91,16 @@ export function useAutoScroll({
|
||||
userDisengagedRef.current = true
|
||||
}
|
||||
|
||||
const updateScrollButtonVisibility = () => {
|
||||
const container = containerRef?.current
|
||||
if (!container || !userDisengagedRef.current || isScrollButtonVisibleRef.current) return
|
||||
const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight
|
||||
if (distanceFromBottom > SHOW_SCROLL_BUTTON_THRESHOLD_PX) {
|
||||
isScrollButtonVisibleRef.current = true
|
||||
onScrollStateChangeRef.current?.(true)
|
||||
}
|
||||
}
|
||||
|
||||
const handlePointerDown = (e: PointerEvent) => {
|
||||
pointerStartYRef.current = e.clientY
|
||||
pointerActiveRef.current = true
|
||||
@@ -105,6 +119,27 @@ export function useAutoScroll({
|
||||
pointerActiveRef.current = false
|
||||
}
|
||||
|
||||
const handleTouchStart = (e: TouchEvent) => {
|
||||
const touch = e.touches[0]
|
||||
if (!touch) return
|
||||
touchStartYRef.current = touch.clientY
|
||||
touchActiveRef.current = true
|
||||
scrollRequestIdRef.current += 1
|
||||
}
|
||||
|
||||
const handleTouchMove = (e: TouchEvent) => {
|
||||
const touch = e.touches[0]
|
||||
if (!touch || touchStartYRef.current === null) return
|
||||
if (touch.clientY > touchStartYRef.current + 4) {
|
||||
markDisengaged()
|
||||
}
|
||||
}
|
||||
|
||||
const handleTouchEnd = () => {
|
||||
touchStartYRef.current = null
|
||||
touchActiveRef.current = false
|
||||
}
|
||||
|
||||
const handleWheel = (e: WheelEvent) => {
|
||||
if (e.deltaY < 0) {
|
||||
markDisengaged()
|
||||
@@ -129,16 +164,12 @@ export function useAutoScroll({
|
||||
isScrollButtonVisibleRef.current = false
|
||||
onScrollStateChangeRef.current?.(false)
|
||||
}
|
||||
} else if (userDisengagedRef.current && !isScrollButtonVisibleRef.current && scrollHeight - scrollTop - clientHeight > SHOW_SCROLL_BUTTON_THRESHOLD_PX) {
|
||||
isScrollButtonVisibleRef.current = true
|
||||
onScrollStateChangeRef.current?.(true)
|
||||
} else if (pointerActiveRef.current && !userDisengagedRef.current) {
|
||||
} else if (userDisengagedRef.current && !isScrollButtonVisibleRef.current) {
|
||||
updateScrollButtonVisibility()
|
||||
} else if ((pointerActiveRef.current || touchActiveRef.current) && !userDisengagedRef.current) {
|
||||
userScrolledAtRef.current = Date.now()
|
||||
userDisengagedRef.current = true
|
||||
if (scrollHeight - scrollTop - clientHeight > SHOW_SCROLL_BUTTON_THRESHOLD_PX) {
|
||||
isScrollButtonVisibleRef.current = true
|
||||
onScrollStateChangeRef.current?.(true)
|
||||
}
|
||||
updateScrollButtonVisibility()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,6 +177,10 @@ export function useAutoScroll({
|
||||
container.addEventListener('pointermove', handlePointerMove, { passive: true })
|
||||
container.addEventListener('pointerup', handlePointerUp, { passive: true })
|
||||
container.addEventListener('pointercancel', handlePointerUp, { passive: true })
|
||||
container.addEventListener('touchstart', handleTouchStart, { passive: true })
|
||||
container.addEventListener('touchmove', handleTouchMove, { passive: true })
|
||||
container.addEventListener('touchend', handleTouchEnd, { passive: true })
|
||||
container.addEventListener('touchcancel', handleTouchEnd, { passive: true })
|
||||
container.addEventListener('wheel', handleWheel, { passive: true })
|
||||
container.addEventListener('keydown', handleKeyDown)
|
||||
container.addEventListener('scroll', handleScroll, { passive: true })
|
||||
@@ -155,6 +190,10 @@ export function useAutoScroll({
|
||||
container.removeEventListener('pointermove', handlePointerMove)
|
||||
container.removeEventListener('pointerup', handlePointerUp)
|
||||
container.removeEventListener('pointercancel', handlePointerUp)
|
||||
container.removeEventListener('touchstart', handleTouchStart)
|
||||
container.removeEventListener('touchmove', handleTouchMove)
|
||||
container.removeEventListener('touchend', handleTouchEnd)
|
||||
container.removeEventListener('touchcancel', handleTouchEnd)
|
||||
container.removeEventListener('wheel', handleWheel)
|
||||
container.removeEventListener('keydown', handleKeyDown)
|
||||
container.removeEventListener('scroll', handleScroll)
|
||||
@@ -185,7 +224,16 @@ export function useAutoScroll({
|
||||
const timeSinceUserScroll = Date.now() - userScrolledAtRef.current
|
||||
const recentlyScrolled = timeSinceUserScroll < SCROLL_LOCK_MS
|
||||
|
||||
if (recentlyScrolled || userDisengagedRef.current || pointerActiveRef.current) {
|
||||
if (userDisengagedRef.current) {
|
||||
const distanceFromBottom = containerRef.current.scrollHeight - containerRef.current.scrollTop - containerRef.current.clientHeight
|
||||
if (!isScrollButtonVisibleRef.current && distanceFromBottom > SHOW_SCROLL_BUTTON_THRESHOLD_PX) {
|
||||
isScrollButtonVisibleRef.current = true
|
||||
onScrollStateChangeRef.current?.(true)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if (recentlyScrolled || pointerActiveRef.current || touchActiveRef.current) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -61,6 +61,22 @@ const compareMessageIds = (id1: string, id2: string): number => {
|
||||
|
||||
const PENDING_ACTION_SYNC_INTERVAL_MS = 30000
|
||||
const PROMPT_OVERLAY_CLEARANCE_PX = 16
|
||||
|
||||
const getMessagesContentVersion = (messages?: MessageWithParts[]): number => {
|
||||
if (!messages) return 0
|
||||
return messages.reduce((sum, message) => {
|
||||
return sum + message.parts.reduce((partSum, part) => {
|
||||
if ('text' in part && typeof part.text === 'string') {
|
||||
return partSum + part.text.length
|
||||
}
|
||||
if (part.type === 'tool') {
|
||||
return partSum + JSON.stringify(part.state).length
|
||||
}
|
||||
return partSum + 1
|
||||
}, 0)
|
||||
}, messages.length)
|
||||
}
|
||||
|
||||
export function SessionDetail() {
|
||||
const { id, sessionId } = useParams<{ id: string; sessionId: string }>();
|
||||
const navigate = useNavigate();
|
||||
@@ -152,7 +168,7 @@ export function SessionDetail() {
|
||||
containerRef: messageContainerRef,
|
||||
messages: messages?.map(m => m.info),
|
||||
sessionId,
|
||||
contentVersion: messages?.reduce((sum, m) => sum + m.parts.length, 0) ?? 0,
|
||||
contentVersion: getMessagesContentVersion(messages),
|
||||
onScrollStateChange: setShowScrollButton
|
||||
});
|
||||
const abortSession = useAbortSession(opcodeUrl, repoDirectory, sessionId);
|
||||
|
||||
Reference in new issue
Block a user