fix(web): improve logs panel scroll handling (#2305)

- forward refs through ScrollArea so logs can access the viewport
- keep logs pinned to the bottom only when the user is already near it
- apply import and className ordering cleanup across frontend components
This commit is contained in:
wenjie authored and GitHub committed 2026-04-03 15:37:23 +08:00
1 parent 7f7b4c430b
commit bd56e10bb8
13 files changed
+97 -53

No files matched your search

@@ -4,6 +4,15 @@ import { useTranslation } from "react-i18next"
import { AnsiLogLine } from "@/components/logs/ansi-log-line"
import { ScrollArea } from "@/components/ui/scroll-area"
const AUTO_SCROLL_THRESHOLD_PX = 24
function isNearBottom(viewport: HTMLDivElement) {
const distanceToBottom =
viewport.scrollHeight - viewport.scrollTop - viewport.clientHeight
return distanceToBottom <= AUTO_SCROLL_THRESHOLD_PX
}
type LogsPanelProps = {
logs: string[]
wrapColumns: number
@@ -18,17 +27,57 @@ export function LogsPanel({
measureRef,
}: LogsPanelProps) {
const { t } = useTranslation()
const scrollRef = useRef<HTMLDivElement>(null)
const scrollAreaRef = useRef<HTMLDivElement>(null)
const viewportRef = useRef<HTMLDivElement | null>(null)
const shouldStickToBottomRef = useRef(true)
useEffect(() => {
if (scrollRef.current) {
scrollRef.current.scrollIntoView({ behavior: "smooth" })
const scrollArea = scrollAreaRef.current
const viewport = scrollArea?.querySelector<HTMLDivElement>(
'[data-slot="scroll-area-viewport"]',
)
if (!viewport) {
return
}
viewportRef.current = viewport
const updateStickToBottom = () => {
shouldStickToBottomRef.current = isNearBottom(viewport)
}
updateStickToBottom()
viewport.addEventListener("scroll", updateStickToBottom)
return () => {
viewport.removeEventListener("scroll", updateStickToBottom)
if (viewportRef.current === viewport) {
viewportRef.current = null
}
}
}, [])
useEffect(() => {
const viewport = viewportRef.current
if (!viewport) {
return
}
// Clearing logs or switching runs can replace the buffer with much shorter
// content, so a previously stale "not sticky" state needs to be rechecked.
if (!shouldStickToBottomRef.current) {
shouldStickToBottomRef.current = isNearBottom(viewport)
}
if (shouldStickToBottomRef.current) {
viewport.scrollTop = viewport.scrollHeight
}
}, [logs])
return (
<div className="relative flex-1 overflow-hidden rounded-lg border border-zinc-800 bg-zinc-950 text-zinc-100">
<ScrollArea className="h-full">
<ScrollArea ref={scrollAreaRef} className="h-full">
<div
ref={contentRef}
className="relative p-4 font-mono text-sm leading-relaxed"
@@ -47,7 +96,6 @@ export function LogsPanel({
<AnsiLogLine key={index} line={log} wrapColumns={wrapColumns} />
))
)}
<div ref={scrollRef} />
</div>
</ScrollArea>
</div>