mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
fix(session-ui): reveal a focused review comment through one state machine (#61819)
This commit is contained in:
committed by
GitHub
Unverified
parent
0d18f558b0
commit
331a3b9c7f
@@ -85,3 +85,31 @@ story("keeps typed comment text while other lines and comments are used", async
|
||||
await expect(review.getByText("edited comment", { exact: true })).toHaveCount(0)
|
||||
await expect(textbox).toHaveValue("draft text")
|
||||
})
|
||||
|
||||
story("a focus request opens the comment and clears once the comment shows", async ({ mount }) => {
|
||||
const root = await mount("components-session-review--focused-comment")
|
||||
const comment = root.locator('[data-comment-id="comment-1"]')
|
||||
const request = root.locator("output")
|
||||
|
||||
await expect(comment).toBeVisible()
|
||||
await expect(comment).not.toHaveAttribute("data-open")
|
||||
await root.getByRole("button", { name: "Focus comment", exact: true }).click()
|
||||
await expect(comment).toHaveAttribute("data-open", "")
|
||||
await expect(request).toHaveText("none")
|
||||
})
|
||||
|
||||
story("a focus request for a comment that never renders clears after the reveal times out", async ({ mount, page }) => {
|
||||
const root = await mount("components-session-review--focused-comment")
|
||||
const request = root.locator("output")
|
||||
|
||||
await expect(root.locator('[data-comment-id="comment-1"]')).toBeVisible()
|
||||
await page.clock.install()
|
||||
await root.getByRole("button", { name: "Focus missing comment", exact: true }).click()
|
||||
|
||||
// The request used to clear on the next frame, before the comment could render.
|
||||
await page.clock.runFor(1000)
|
||||
await expect(request).toHaveText("missing")
|
||||
|
||||
await page.clock.runFor(1100)
|
||||
await expect(request).toHaveText("none")
|
||||
})
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
import { createEffect, createSignal, onCleanup, untrack, type Accessor } from "solid-js"
|
||||
import {
|
||||
INITIAL_REVIEW_FOCUS_STATE,
|
||||
REVIEW_FOCUS_TIMEOUT_MS,
|
||||
transitionReviewFocus,
|
||||
type ReviewFocusEffect,
|
||||
type ReviewFocusEvent,
|
||||
} from "./review-focus-machine"
|
||||
|
||||
type ReviewFocusTarget = { file: string; id: string }
|
||||
|
||||
// Browser resources of the current reveal; the phase itself lives in the machine.
|
||||
type RevealHandles = {
|
||||
frame?: number
|
||||
ackFrame?: number
|
||||
timer?: ReturnType<typeof setTimeout>
|
||||
observer?: MutationObserver
|
||||
}
|
||||
|
||||
type ReviewFocusDriverInput = {
|
||||
focusedComment: Accessor<ReviewFocusTarget | null | undefined>
|
||||
isAvailable: (focus: ReviewFocusTarget) => boolean
|
||||
/** The element the comments render into; the driver watches it for the comment to appear. */
|
||||
root: () => HTMLElement | undefined
|
||||
/** The rendered, visible comment, if it is there yet. */
|
||||
findComment: (file: string, commentId: string) => HTMLElement | undefined
|
||||
/** Where to scroll while the comment is not rendered yet, such as its file's placeholder. */
|
||||
findFallback?: (file: string) => HTMLElement | undefined
|
||||
scrollTo: (target: HTMLElement) => void
|
||||
onOpenComment: (file: string, commentId: string) => void
|
||||
onExpandFile?: (file: string) => void
|
||||
onAcknowledge: VoidFunction
|
||||
}
|
||||
|
||||
// Reveals a parent's focused comment: waits for it to render (DOM mutations, `poke`), scrolls to it, and clears
|
||||
// the parent's request once it is shown, unavailable or timed out.
|
||||
export function createReviewFocusDriver(input: ReviewFocusDriverInput) {
|
||||
const [focusState, setFocusState] = createSignal(INITIAL_REVIEW_FOCUS_STATE)
|
||||
|
||||
const handles: RevealHandles = {}
|
||||
|
||||
const dispatch = (event: ReviewFocusEvent) => {
|
||||
const step = transitionReviewFocus(untrack(focusState), event)
|
||||
setFocusState(step.state)
|
||||
step.effects.forEach(run)
|
||||
}
|
||||
|
||||
const run = (effect: ReviewFocusEffect) => {
|
||||
if (effect.type === "cancel-reveal") {
|
||||
if (handles.frame !== undefined) cancelAnimationFrame(handles.frame)
|
||||
|
||||
if (handles.ackFrame !== undefined) cancelAnimationFrame(handles.ackFrame)
|
||||
|
||||
if (handles.timer !== undefined) clearTimeout(handles.timer)
|
||||
handles.observer?.disconnect()
|
||||
handles.frame = undefined
|
||||
handles.ackFrame = undefined
|
||||
handles.timer = undefined
|
||||
handles.observer = undefined
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (effect.type === "open-comment") return input.onOpenComment(effect.file, effect.commentId)
|
||||
|
||||
if (effect.type === "expand-file") return input.onExpandFile?.(effect.file)
|
||||
|
||||
if (effect.type === "reveal") {
|
||||
handles.timer = setTimeout(
|
||||
() => dispatch({ type: "reveal.timed-out", attempt: effect.attempt }),
|
||||
REVIEW_FOCUS_TIMEOUT_MS,
|
||||
)
|
||||
schedule()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (!effect.deferred) return acknowledge(effect.attempt)
|
||||
// An unavailable target is known inside the effect that read the request, so clear it on the next frame.
|
||||
handles.ackFrame = requestAnimationFrame(() => {
|
||||
handles.ackFrame = undefined
|
||||
acknowledge(effect.attempt)
|
||||
})
|
||||
}
|
||||
|
||||
const acknowledge = (attempt: number) => {
|
||||
const current = untrack(focusState)
|
||||
|
||||
if (current.status !== "settled" || current.attempt !== attempt) return
|
||||
input.onAcknowledge()
|
||||
}
|
||||
|
||||
const schedule = () => {
|
||||
if (handles.frame !== undefined) return
|
||||
handles.frame = requestAnimationFrame(check)
|
||||
}
|
||||
|
||||
const check = () => {
|
||||
handles.frame = undefined
|
||||
const current = untrack(focusState)
|
||||
|
||||
if (current.status !== "revealing") return
|
||||
const root = input.root()
|
||||
|
||||
if (!root) return
|
||||
const comment = input.findComment(current.file, current.commentId)
|
||||
|
||||
if (comment) {
|
||||
input.scrollTo(comment)
|
||||
dispatch({ type: "reveal.succeeded", attempt: current.attempt })
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
const fallback = input.findFallback?.(current.file)
|
||||
|
||||
if (fallback) input.scrollTo(fallback)
|
||||
|
||||
if (handles.observer) return
|
||||
handles.observer = new MutationObserver(schedule)
|
||||
handles.observer.observe(root, { childList: true, subtree: true })
|
||||
}
|
||||
|
||||
createEffect(() => {
|
||||
const focus = input.focusedComment()
|
||||
|
||||
untrack(() => {
|
||||
if (!focus) return dispatch({ type: "focus.cleared" })
|
||||
dispatch({ type: "focus.requested", file: focus.file, commentId: focus.id, available: input.isAvailable(focus) })
|
||||
})
|
||||
})
|
||||
|
||||
onCleanup(() => dispatch({ type: "disposed" }))
|
||||
|
||||
return {
|
||||
state: focusState,
|
||||
/** Checks again for the comment, for example after the diff rendered. */
|
||||
poke: () => {
|
||||
if (untrack(focusState).status === "revealing") schedule()
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import {
|
||||
INITIAL_REVIEW_FOCUS_STATE,
|
||||
reviewFocusPinnedFile,
|
||||
transitionReviewFocus,
|
||||
type ReviewFocusEvent,
|
||||
type ReviewFocusState,
|
||||
} from "./review-focus-machine"
|
||||
|
||||
const request = (file: string, commentId: string, available = true): ReviewFocusEvent => ({
|
||||
type: "focus.requested",
|
||||
file,
|
||||
commentId,
|
||||
available,
|
||||
})
|
||||
|
||||
function replay(events: ReviewFocusEvent[], state = INITIAL_REVIEW_FOCUS_STATE) {
|
||||
return events.reduce<ReviewFocusState>((current, event) => transitionReviewFocus(current, event).state, state)
|
||||
}
|
||||
|
||||
describe("transitionReviewFocus", () => {
|
||||
test("a second request supersedes the first, and only the current reveal acknowledges", () => {
|
||||
const first = transitionReviewFocus(INITIAL_REVIEW_FOCUS_STATE, request("a.ts", "c1"))
|
||||
|
||||
// The old reveal acknowledged on the next frame, before the comment rendered, and could clear a newer request.
|
||||
expect(first.effects.map((effect) => effect.type)).not.toContain("acknowledge")
|
||||
expect(reviewFocusPinnedFile(first.state)).toBe("a.ts")
|
||||
|
||||
const second = transitionReviewFocus(first.state, request("b.ts", "c2"))
|
||||
|
||||
expect(second).toEqual({
|
||||
state: { status: "revealing", attempt: 2, file: "b.ts", commentId: "c2" },
|
||||
effects: [
|
||||
{ type: "cancel-reveal" },
|
||||
{ type: "open-comment", file: "b.ts", commentId: "c2" },
|
||||
{ type: "expand-file", file: "b.ts" },
|
||||
{ type: "reveal", attempt: 2 },
|
||||
],
|
||||
})
|
||||
|
||||
for (const late of [
|
||||
{ type: "reveal.succeeded", attempt: 1 },
|
||||
{ type: "reveal.timed-out", attempt: 1 },
|
||||
] as const)
|
||||
expect(transitionReviewFocus(second.state, late)).toEqual({ state: second.state, effects: [] })
|
||||
|
||||
expect(transitionReviewFocus(second.state, { type: "reveal.succeeded", attempt: 2 })).toEqual({
|
||||
state: { status: "settled", attempt: 2, file: "b.ts", commentId: "c2", outcome: "revealed" },
|
||||
effects: [
|
||||
{ type: "cancel-reveal" },
|
||||
{ type: "open-comment", file: "b.ts", commentId: "c2" },
|
||||
{ type: "acknowledge", attempt: 2, deferred: false },
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
test("a reveal that never finds the comment times out and acknowledges", () => {
|
||||
const step = transitionReviewFocus(replay([request("a.ts", "c1")]), { type: "reveal.timed-out", attempt: 1 })
|
||||
|
||||
expect(step).toEqual({
|
||||
state: { status: "settled", attempt: 1, file: "a.ts", commentId: "c1", outcome: "timeout" },
|
||||
effects: [{ type: "cancel-reveal" }, { type: "acknowledge", attempt: 1, deferred: false }],
|
||||
})
|
||||
expect(reviewFocusPinnedFile(step.state)).toBeUndefined()
|
||||
})
|
||||
|
||||
test("an unavailable target settles at once, and a newer request outranks its acknowledgement", () => {
|
||||
const missing = transitionReviewFocus(INITIAL_REVIEW_FOCUS_STATE, request("missing.ts", "c1", false))
|
||||
|
||||
expect(missing).toEqual({
|
||||
state: { status: "settled", attempt: 1, file: "missing.ts", commentId: "c1", outcome: "unavailable" },
|
||||
effects: [{ type: "acknowledge", attempt: 1, deferred: true }],
|
||||
})
|
||||
|
||||
expect(transitionReviewFocus(missing.state, request("b.ts", "c2")).state).toEqual({
|
||||
status: "revealing",
|
||||
attempt: 2,
|
||||
file: "b.ts",
|
||||
commentId: "c2",
|
||||
})
|
||||
})
|
||||
|
||||
test("the same target again is ignored until the request is cleared", () => {
|
||||
const states = [
|
||||
replay([request("a.ts", "c1")]),
|
||||
replay([request("a.ts", "c1"), { type: "reveal.succeeded", attempt: 1 }]),
|
||||
replay([request("a.ts", "c1"), { type: "reveal.timed-out", attempt: 1 }]),
|
||||
]
|
||||
|
||||
for (const state of states)
|
||||
expect(transitionReviewFocus(state, request("a.ts", "c1"))).toEqual({ state, effects: [] })
|
||||
|
||||
const again = transitionReviewFocus(
|
||||
replay([request("a.ts", "c1"), { type: "focus.cleared" }]),
|
||||
request("a.ts", "c1"),
|
||||
)
|
||||
|
||||
expect(again.state).toEqual({ status: "revealing", attempt: 2, file: "a.ts", commentId: "c1" })
|
||||
})
|
||||
|
||||
test("clearing or disposing stops the reveal and ignores its late results", () => {
|
||||
const revealing = replay([request("a.ts", "c1")])
|
||||
|
||||
for (const exit of [{ type: "focus.cleared" }, { type: "disposed" }] as const) {
|
||||
const step = transitionReviewFocus(revealing, exit)
|
||||
|
||||
expect(step).toEqual({ state: { status: "idle", attempt: 1 }, effects: [{ type: "cancel-reveal" }] })
|
||||
expect(transitionReviewFocus(step.state, { type: "reveal.succeeded", attempt: 1 }).state).toBe(step.state)
|
||||
expect(transitionReviewFocus(step.state, { type: "reveal.timed-out", attempt: 1 }).state).toBe(step.state)
|
||||
}
|
||||
|
||||
expect(transitionReviewFocus(INITIAL_REVIEW_FOCUS_STATE, { type: "disposed" }).state).toBe(
|
||||
INITIAL_REVIEW_FOCUS_STATE,
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,95 @@
|
||||
export const REVIEW_FOCUS_TIMEOUT_MS = 2000
|
||||
|
||||
/** A parent's request to reveal one saved comment. `attempt` grows with each new target, so a late result or acknowledgement of an older target is ignored. */
|
||||
export type ReviewFocusState =
|
||||
| { readonly status: "idle"; readonly attempt: number }
|
||||
| { readonly status: "revealing"; readonly attempt: number; readonly file: string; readonly commentId: string }
|
||||
| {
|
||||
readonly status: "settled"
|
||||
readonly attempt: number
|
||||
readonly file: string
|
||||
readonly commentId: string
|
||||
readonly outcome: "revealed" | "unavailable" | "timeout"
|
||||
}
|
||||
|
||||
export type ReviewFocusEvent =
|
||||
| { readonly type: "focus.requested"; readonly file: string; readonly commentId: string; readonly available: boolean }
|
||||
| { readonly type: "focus.cleared" }
|
||||
| { readonly type: "reveal.succeeded"; readonly attempt: number }
|
||||
| { readonly type: "reveal.timed-out"; readonly attempt: number }
|
||||
| { readonly type: "disposed" }
|
||||
|
||||
export type ReviewFocusEffect =
|
||||
| { readonly type: "cancel-reveal" }
|
||||
| { readonly type: "open-comment"; readonly file: string; readonly commentId: string }
|
||||
| { readonly type: "expand-file"; readonly file: string }
|
||||
| { readonly type: "reveal"; readonly attempt: number }
|
||||
| { readonly type: "acknowledge"; readonly attempt: number; readonly deferred: boolean }
|
||||
|
||||
export type ReviewFocusTransition = {
|
||||
readonly state: ReviewFocusState
|
||||
readonly effects: readonly ReviewFocusEffect[]
|
||||
}
|
||||
|
||||
export const INITIAL_REVIEW_FOCUS_STATE: ReviewFocusState = Object.freeze({ status: "idle", attempt: 0 })
|
||||
|
||||
const CANCEL_REVEAL: ReviewFocusEffect = Object.freeze({ type: "cancel-reveal" })
|
||||
|
||||
const NO_EFFECTS: readonly ReviewFocusEffect[] = Object.freeze([])
|
||||
|
||||
export function transitionReviewFocus(state: ReviewFocusState, event: ReviewFocusEvent): ReviewFocusTransition {
|
||||
if (event.type === "focus.requested") {
|
||||
// A parent that sets the same target again, before it clears the request, does not restart the reveal.
|
||||
if (state.status !== "idle" && state.file === event.file && state.commentId === event.commentId)
|
||||
return { state, effects: NO_EFFECTS }
|
||||
|
||||
const attempt = state.attempt + 1
|
||||
const cancel = state.status === "idle" ? [] : [CANCEL_REVEAL]
|
||||
const target = { attempt, file: event.file, commentId: event.commentId }
|
||||
|
||||
if (!event.available)
|
||||
return {
|
||||
state: { status: "settled", ...target, outcome: "unavailable" },
|
||||
effects: [...cancel, { type: "acknowledge", attempt, deferred: true }],
|
||||
}
|
||||
|
||||
return {
|
||||
state: { status: "revealing", ...target },
|
||||
effects: [
|
||||
...cancel,
|
||||
{ type: "open-comment", file: event.file, commentId: event.commentId },
|
||||
{ type: "expand-file", file: event.file },
|
||||
{ type: "reveal", attempt },
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
if (event.type === "focus.cleared" || event.type === "disposed") {
|
||||
if (state.status === "idle") return { state, effects: NO_EFFECTS }
|
||||
|
||||
return { state: { status: "idle", attempt: state.attempt }, effects: [CANCEL_REVEAL] }
|
||||
}
|
||||
|
||||
if (state.status !== "revealing" || event.attempt !== state.attempt) return { state, effects: NO_EFFECTS }
|
||||
|
||||
if (event.type === "reveal.succeeded")
|
||||
return {
|
||||
state: { ...state, status: "settled", outcome: "revealed" },
|
||||
effects: [
|
||||
CANCEL_REVEAL,
|
||||
// The comment may have arrived after the request, so open it again now that it rendered.
|
||||
{ type: "open-comment", file: state.file, commentId: state.commentId },
|
||||
{ type: "acknowledge", attempt: state.attempt, deferred: false },
|
||||
],
|
||||
}
|
||||
|
||||
return {
|
||||
state: { ...state, status: "settled", outcome: "timeout" },
|
||||
effects: [CANCEL_REVEAL, { type: "acknowledge", attempt: state.attempt, deferred: false }],
|
||||
}
|
||||
}
|
||||
|
||||
/** The file a reveal keeps mounted while it waits for the comment to render. */
|
||||
export function reviewFocusPinnedFile(state: ReviewFocusState) {
|
||||
return state.status === "revealing" ? state.file : undefined
|
||||
}
|
||||
@@ -3,7 +3,8 @@ import { parseDiffFromFile } from "@pierre/diffs"
|
||||
import { CurrentSessionProviders } from "../storybook/current-session-story"
|
||||
import { editThenTestDocument, reviewDiffs } from "../storybook/current-session-fixtures"
|
||||
import { File } from "./file"
|
||||
import { SessionReview, type SessionReviewComment } from "./session-review"
|
||||
import { Button } from "@opencode/ui/button"
|
||||
import { SessionReview, type SessionReviewComment, type SessionReviewFocus } from "./session-review"
|
||||
|
||||
function ReviewStory(props: { split?: boolean }) {
|
||||
return (
|
||||
@@ -48,28 +49,29 @@ export const UnifiedDark = {
|
||||
render: () => <ReviewStory />,
|
||||
}
|
||||
|
||||
const commentFile = "src/review.ts"
|
||||
|
||||
const commentDiffs = [
|
||||
{
|
||||
file: commentFile,
|
||||
additions: 1,
|
||||
deletions: 1,
|
||||
status: "modified" as const,
|
||||
patch:
|
||||
"diff --git a/src/review.ts b/src/review.ts\n--- a/src/review.ts\n+++ b/src/review.ts\n@@ -1,3 +1,3 @@\n export const first = 1\n-export const value = 'before'\n+export const value = 'after'\n export const last = 3\n",
|
||||
},
|
||||
]
|
||||
|
||||
function InteractiveCommentsStory() {
|
||||
const [state, setState] = createStore<{ comments: SessionReviewComment[]; next: number }>({ comments: [], next: 1 })
|
||||
const file = "src/review.ts"
|
||||
|
||||
const diffs = [
|
||||
{
|
||||
file,
|
||||
additions: 1,
|
||||
deletions: 1,
|
||||
status: "modified" as const,
|
||||
patch:
|
||||
"diff --git a/src/review.ts b/src/review.ts\n--- a/src/review.ts\n+++ b/src/review.ts\n@@ -1,3 +1,3 @@\n export const first = 1\n-export const value = 'before'\n+export const value = 'after'\n export const last = 3\n",
|
||||
},
|
||||
]
|
||||
|
||||
return (
|
||||
<CurrentSessionProviders document={editThenTestDocument}>
|
||||
<div class="mx-auto h-screen min-h-[620px] w-full max-w-[900px] overflow-hidden bg-background-base">
|
||||
<SessionReview
|
||||
title="Changes in this Session"
|
||||
diffs={diffs}
|
||||
open={[file]}
|
||||
diffs={commentDiffs}
|
||||
open={[commentFile]}
|
||||
comments={state.comments}
|
||||
onLineComment={(comment) => {
|
||||
setState("comments", (comments) => [...comments, { id: `comment-${state.next}`, ...comment }])
|
||||
@@ -95,6 +97,50 @@ function InteractiveCommentsStory() {
|
||||
|
||||
export const InteractiveComments = { render: () => <InteractiveCommentsStory /> }
|
||||
|
||||
// A parent asks the review to reveal a saved comment, as a composer chip does. "Focus missing comment" names a
|
||||
// comment that never renders, so the request settles by timing out.
|
||||
function FocusedCommentStory() {
|
||||
const [state, setState] = createStore<{ focus: SessionReviewFocus | null }>({ focus: null })
|
||||
|
||||
const comments: SessionReviewComment[] = [
|
||||
{
|
||||
id: "comment-1",
|
||||
file: commentFile,
|
||||
selection: { start: 2, end: 2, side: "additions" },
|
||||
comment: "Saved comment",
|
||||
},
|
||||
]
|
||||
|
||||
return (
|
||||
<CurrentSessionProviders document={editThenTestDocument}>
|
||||
<div class="mx-auto flex h-screen min-h-[620px] w-full max-w-[900px] flex-col overflow-hidden bg-background-base">
|
||||
<div class="flex items-center gap-2 p-2">
|
||||
<Button size="small" onClick={() => setState("focus", { file: commentFile, id: "comment-1" })}>
|
||||
Focus comment
|
||||
</Button>
|
||||
<Button size="small" onClick={() => setState("focus", { file: commentFile, id: "missing" })}>
|
||||
Focus missing comment
|
||||
</Button>
|
||||
<span>
|
||||
Focus request: <output>{state.focus?.id ?? "none"}</output>
|
||||
</span>
|
||||
</div>
|
||||
<SessionReview
|
||||
title="Changes in this Session"
|
||||
diffs={commentDiffs}
|
||||
open={[commentFile]}
|
||||
comments={comments}
|
||||
focusedComment={state.focus}
|
||||
onFocusedCommentChange={(focus) => setState("focus", focus)}
|
||||
onLineComment={() => undefined}
|
||||
/>
|
||||
</div>
|
||||
</CurrentSessionProviders>
|
||||
)
|
||||
}
|
||||
|
||||
export const FocusedComment = { render: () => <FocusedCommentStory /> }
|
||||
|
||||
const gitDiffs = [
|
||||
{
|
||||
// OpenCode 93e1f383dd79683af4fc5ad139cea0516603c838, unchanged git-show output.
|
||||
|
||||
@@ -40,6 +40,8 @@ import {
|
||||
transitionReviewCommentModes,
|
||||
type LineCommentEvent,
|
||||
} from "./line-comment-machine"
|
||||
import { createReviewFocusDriver } from "./review-focus-driver"
|
||||
import { reviewFocusPinnedFile } from "./review-focus-machine"
|
||||
import type { LineCommentEditorProps } from "./line-comment"
|
||||
import { normalize, text, type ViewDiff } from "./session-diff"
|
||||
|
||||
@@ -158,7 +160,6 @@ function diffId(file: string): string | undefined {
|
||||
|
||||
export const SessionReview = (props: SessionReviewProps) => {
|
||||
let scroll: HTMLDivElement | undefined
|
||||
let focusToken = 0
|
||||
let frame: number | undefined
|
||||
const i18n = useI18n()
|
||||
const fileComponent = useFileComponent()
|
||||
@@ -249,7 +250,7 @@ export const SessionReview = (props: SessionReviewProps) => {
|
||||
}
|
||||
|
||||
const pinned = (file: string) =>
|
||||
props.focusedComment?.file === file ||
|
||||
reviewFocusPinnedFile(focus.state()) === file ||
|
||||
props.focusedFile === file ||
|
||||
reviewFileCommentMode(commentModes(), file).status !== "idle"
|
||||
|
||||
@@ -300,63 +301,44 @@ export const SessionReview = (props: SessionReviewProps) => {
|
||||
return previewSelectedLines(contents, range)
|
||||
}
|
||||
|
||||
createEffect(() => {
|
||||
const focus = props.focusedComment
|
||||
const focus = createReviewFocusDriver({
|
||||
focusedComment: () => props.focusedComment,
|
||||
// Before the diffs load, wait for them instead of failing the request.
|
||||
isAvailable: (target) => {
|
||||
if (!hasDiffs()) return true
|
||||
const diff = itemsMap()[target.file]
|
||||
|
||||
if (!focus) return
|
||||
return diff !== undefined && (diff.additions !== 0 || diff.deletions !== 0)
|
||||
},
|
||||
root: () => scroll,
|
||||
findComment: (file, commentId) => {
|
||||
const anchor = anchors.get(file)?.querySelector(`[data-comment-id="${CSS.escape(commentId)}"]`)
|
||||
|
||||
untrack(() => {
|
||||
focusToken++
|
||||
const token = focusToken
|
||||
if (!(anchor instanceof HTMLElement)) return
|
||||
|
||||
const comment = (props.comments ?? []).find((c) => c.file === focus.file && c.id === focus.id)
|
||||
if (anchor.style.pointerEvents === "none" || anchor.style.opacity === "0") return
|
||||
|
||||
if (comment) dispatchComment(focus.file, { type: "comment.focused", id: comment.id, range: comment.selection })
|
||||
return anchor
|
||||
},
|
||||
findFallback: (file) => anchors.get(file),
|
||||
scrollTo: (target) => {
|
||||
if (!scroll) return
|
||||
const rootRect = scroll.getBoundingClientRect()
|
||||
const targetRect = target.getBoundingClientRect()
|
||||
const next = scroll.scrollTop + targetRect.top - rootRect.top - rootRect.height / 2 + targetRect.height / 2
|
||||
scroll.scrollTop = Math.max(0, next)
|
||||
},
|
||||
onOpenComment: (file, commentId) => {
|
||||
const comment = (props.comments ?? []).find((item) => item.file === file && item.id === commentId)
|
||||
|
||||
if (comment) dispatchComment(file, { type: "comment.focused", id: comment.id, range: comment.selection })
|
||||
},
|
||||
onExpandFile: (file) => {
|
||||
const current = open()
|
||||
|
||||
if (!current.includes(focus.file)) {
|
||||
handleChange([...current, focus.file])
|
||||
}
|
||||
|
||||
const scrollTo = (attempt: number) => {
|
||||
if (token !== focusToken) return
|
||||
|
||||
const root = scroll
|
||||
|
||||
if (!root) return
|
||||
|
||||
const wrapper = anchors.get(focus.file)
|
||||
const anchor = wrapper?.querySelector(`[data-comment-id="${focus.id}"]`)
|
||||
|
||||
const ready =
|
||||
anchor instanceof HTMLElement && anchor.style.pointerEvents !== "none" && anchor.style.opacity !== "0"
|
||||
|
||||
const target = ready ? anchor : wrapper
|
||||
|
||||
if (!target) {
|
||||
if (attempt >= 120) return
|
||||
requestAnimationFrame(() => scrollTo(attempt + 1))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
const rootRect = root.getBoundingClientRect()
|
||||
const targetRect = target.getBoundingClientRect()
|
||||
const offset = targetRect.top - rootRect.top
|
||||
const next = root.scrollTop + offset - rootRect.height / 2 + targetRect.height / 2
|
||||
root.scrollTop = Math.max(0, next)
|
||||
|
||||
if (ready) return
|
||||
|
||||
if (attempt >= 120) return
|
||||
requestAnimationFrame(() => scrollTo(attempt + 1))
|
||||
}
|
||||
|
||||
requestAnimationFrame(() => scrollTo(0))
|
||||
|
||||
requestAnimationFrame(() => props.onFocusedCommentChange?.(null))
|
||||
})
|
||||
if (!current.includes(file)) handleChange([...current, file])
|
||||
},
|
||||
onAcknowledge: () => props.onFocusedCommentChange?.(null),
|
||||
})
|
||||
|
||||
return (
|
||||
@@ -619,6 +601,7 @@ export const SessionReview = (props: SessionReviewProps) => {
|
||||
anchors.set(file, el)
|
||||
nodes.set(file, el)
|
||||
queue()
|
||||
focus.poke()
|
||||
}}
|
||||
>
|
||||
<Show when={expanded()}>
|
||||
@@ -663,6 +646,7 @@ export const SessionReview = (props: SessionReviewProps) => {
|
||||
disableLineNumbers={props.disableLineNumbers}
|
||||
onRendered={() => {
|
||||
props.onDiffRendered?.()
|
||||
focus.poke()
|
||||
}}
|
||||
enableLineSelection={props.onLineComment != null}
|
||||
enableGutterUtility={props.onLineComment != null}
|
||||
|
||||
@@ -8,7 +8,7 @@ import { mediaKindFromPath } from "../../pierre/media"
|
||||
import { previewSelectedLines } from "../../pierre/selection-bridge"
|
||||
import type { FileDiffInfo } from "@opencode/client/promise"
|
||||
import type { PresentationFileContent, PresentationFileDiff } from "../../file-presentation"
|
||||
import { createEffect, createMemo, onCleanup, Show, untrack } from "solid-js"
|
||||
import { createMemo, Show } from "solid-js"
|
||||
import { Dynamic } from "solid-js/web"
|
||||
import { normalize, text, type ViewDiff } from "../../components/session-diff"
|
||||
import type {
|
||||
@@ -22,6 +22,7 @@ import type {
|
||||
} from "../../components/session-review"
|
||||
import type { SessionReviewExpandMode } from "./session-review-v2"
|
||||
import { createLineCommentControllerV2 } from "./line-comment-annotations-v2"
|
||||
import { createReviewFocusDriver } from "../../components/review-focus-driver"
|
||||
import { shouldVirtualizeReviewDiff } from "./session-review-file-preview-v2-virtualize"
|
||||
import { LineCommentOverflowIcon } from "@opencode/ui/line-comment"
|
||||
import { Menu } from "@opencode/ui/menu"
|
||||
@@ -104,7 +105,6 @@ export function SessionReviewFilePreviewV2(props: SessionReviewFilePreviewV2Prop
|
||||
const i18n = useI18n()
|
||||
const fileComponent = useFileComponent()
|
||||
let scrollRef: HTMLDivElement | undefined
|
||||
let focusToken = 0
|
||||
|
||||
const view = createMemo(() => ({
|
||||
...normalize(props.diff),
|
||||
@@ -153,61 +153,23 @@ export function SessionReviewFilePreviewV2(props: SessionReviewFilePreviewV2Prop
|
||||
: undefined,
|
||||
})
|
||||
|
||||
onCleanup(() => {
|
||||
focusToken++
|
||||
})
|
||||
const focus = createReviewFocusDriver({
|
||||
focusedComment: () => props.focusedComment,
|
||||
// A focus on another file has no comment here; settle it so it cannot hijack a later diff refresh.
|
||||
isAvailable: (target) => target.file === props.file && (diffCanRender() || !!mediaKind()),
|
||||
root: () => scrollRef,
|
||||
findComment: (_, commentId) => {
|
||||
const anchor = scrollRef?.querySelector(`[data-comment-id="${CSS.escape(commentId)}"]`)
|
||||
|
||||
createEffect(() => {
|
||||
const focus = props.focusedComment
|
||||
|
||||
if (!focus) return
|
||||
|
||||
if (focus.file !== props.file) {
|
||||
// The focused file has no mounted preview (e.g. not in the current diff
|
||||
// set); clear the focus anyway so it cannot hijack a later diff refresh.
|
||||
// Clear unconditionally so an unavailable file cannot retain stale focus.
|
||||
untrack(() => {
|
||||
const token = focusToken
|
||||
requestAnimationFrame(() => {
|
||||
if (token !== focusToken) return
|
||||
props.onFocusedCommentChange?.(null)
|
||||
})
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
untrack(() => {
|
||||
const comment = (props.comments ?? []).find((item) => item.file === focus.file && item.id === focus.id)
|
||||
return anchor instanceof HTMLElement ? anchor : undefined
|
||||
},
|
||||
scrollTo: (target) => target.scrollIntoView({ block: "center" }),
|
||||
onOpenComment: (_, commentId) => {
|
||||
const comment = comments().find((item) => item.id === commentId)
|
||||
|
||||
if (comment) commentsUi.focusComment(comment.id, comment.selection)
|
||||
|
||||
// The diff renders asynchronously, so poll for the comment anchor before
|
||||
// scrolling; clear the focus once handled so revisiting the file does not
|
||||
// re-open a stale comment.
|
||||
focusToken++
|
||||
const token = focusToken
|
||||
|
||||
const scrollTo = (attempt: number) => {
|
||||
if (token !== focusToken) return
|
||||
const anchor = scrollRef?.querySelector(`[data-comment-id="${focus.id}"]`)
|
||||
|
||||
if (anchor instanceof HTMLElement) {
|
||||
anchor.scrollIntoView({ block: "center" })
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (attempt >= 120) return
|
||||
requestAnimationFrame(() => scrollTo(attempt + 1))
|
||||
}
|
||||
|
||||
requestAnimationFrame(() => scrollTo(0))
|
||||
requestAnimationFrame(() => {
|
||||
if (token !== focusToken) return
|
||||
props.onFocusedCommentChange?.(null)
|
||||
})
|
||||
})
|
||||
},
|
||||
onAcknowledge: () => props.onFocusedCommentChange?.(null),
|
||||
})
|
||||
|
||||
const expandUnchanged = () => props.expandMode === "expand"
|
||||
@@ -225,6 +187,7 @@ export function SessionReviewFilePreviewV2(props: SessionReviewFilePreviewV2Prop
|
||||
deletionLines: view().fileDiff.deletionLines.length,
|
||||
})}
|
||||
hunkSeparators={view().fileDiff.isPartial ? "simple" : "line-info-basic"}
|
||||
onRendered={focus.poke}
|
||||
enableLineSelection={lineCommentsEnabled()}
|
||||
enableGutterUtility={lineCommentsEnabled()}
|
||||
onLineSelected={(range: SelectedLineRange | null) => {
|
||||
|
||||
Reference in New Issue
Block a user