Skip to content

Commit f2100dc

Browse files
committed
fix(app): scroll jacking
1 parent b0b88f6 commit f2100dc

1 file changed

Lines changed: 46 additions & 33 deletions

File tree

packages/ui/src/components/session-review.tsx

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,18 @@ import { useFileComponent } from "../context/file"
1616
import { useI18n } from "../context/i18n"
1717
import { getDirectory, getFilename } from "@opencode-ai/util/path"
1818
import { checksum } from "@opencode-ai/util/encode"
19-
import { createEffect, createMemo, createSignal, For, Match, onCleanup, Show, Switch, type JSX } from "solid-js"
19+
import {
20+
createEffect,
21+
createMemo,
22+
createSignal,
23+
For,
24+
Match,
25+
onCleanup,
26+
Show,
27+
Switch,
28+
untrack,
29+
type JSX,
30+
} from "solid-js"
2031
import { createStore } from "solid-js/store"
2132
import { type FileContent, type FileDiff } from "@opencode-ai/sdk/v2"
2233
import { PreloadMultiFileDiffResult } from "@pierre/diffs/ssr"
@@ -442,50 +453,52 @@ export const SessionReview = (props: SessionReviewProps) => {
442453
const focus = props.focusedComment
443454
if (!focus) return
444455

445-
focusToken++
446-
const token = focusToken
456+
untrack(() => {
457+
focusToken++
458+
const token = focusToken
447459

448-
setOpened(focus)
460+
setOpened(focus)
449461

450-
const comment = (props.comments ?? []).find((c) => c.file === focus.file && c.id === focus.id)
451-
if (comment) setSelection({ file: comment.file, range: cloneSelectedLineRange(comment.selection) })
462+
const comment = (props.comments ?? []).find((c) => c.file === focus.file && c.id === focus.id)
463+
if (comment) setSelection({ file: comment.file, range: cloneSelectedLineRange(comment.selection) })
452464

453-
const current = open()
454-
if (!current.includes(focus.file)) {
455-
handleChange([...current, focus.file])
456-
}
465+
const current = open()
466+
if (!current.includes(focus.file)) {
467+
handleChange([...current, focus.file])
468+
}
469+
470+
const scrollTo = (attempt: number) => {
471+
if (token !== focusToken) return
457472

458-
const scrollTo = (attempt: number) => {
459-
if (token !== focusToken) return
473+
const root = scroll
474+
if (!root) return
460475

461-
const root = scroll
462-
if (!root) return
476+
const wrapper = anchors.get(focus.file)
477+
const anchor = wrapper?.querySelector(`[data-comment-id="${focus.id}"]`)
478+
const ready = anchor instanceof HTMLElement
479+
480+
const target = ready ? anchor : wrapper
481+
if (!target) {
482+
if (attempt >= 120) return
483+
requestAnimationFrame(() => scrollTo(attempt + 1))
484+
return
485+
}
463486

464-
const wrapper = anchors.get(focus.file)
465-
const anchor = wrapper?.querySelector(`[data-comment-id="${focus.id}"]`)
466-
const ready = anchor instanceof HTMLElement
487+
const rootRect = root.getBoundingClientRect()
488+
const targetRect = target.getBoundingClientRect()
489+
const offset = targetRect.top - rootRect.top
490+
const next = root.scrollTop + offset - rootRect.height / 2 + targetRect.height / 2
491+
root.scrollTop = Math.max(0, next)
467492

468-
const target = ready ? anchor : wrapper
469-
if (!target) {
493+
if (ready) return
470494
if (attempt >= 120) return
471495
requestAnimationFrame(() => scrollTo(attempt + 1))
472-
return
473496
}
474497

475-
const rootRect = root.getBoundingClientRect()
476-
const targetRect = target.getBoundingClientRect()
477-
const offset = targetRect.top - rootRect.top
478-
const next = root.scrollTop + offset - rootRect.height / 2 + targetRect.height / 2
479-
root.scrollTop = Math.max(0, next)
480-
481-
if (ready) return
482-
if (attempt >= 120) return
483-
requestAnimationFrame(() => scrollTo(attempt + 1))
484-
}
498+
requestAnimationFrame(() => scrollTo(0))
485499

486-
requestAnimationFrame(() => scrollTo(0))
487-
488-
requestAnimationFrame(() => props.onFocusedCommentChange?.(null))
500+
requestAnimationFrame(() => props.onFocusedCommentChange?.(null))
501+
})
489502
})
490503

491504
const handleReviewKeyDown = (event: KeyboardEvent) => {

0 commit comments

Comments
 (0)