|
3 | 3 | sourceControlClients, |
4 | 4 | } from "@t3tools/client-runtime/source-control-clients"; |
5 | 5 | import type { CodeViewItem, DiffLineAnnotation, SelectedLineRange } from "@pierre/diffs"; |
6 | | -import type { CodeViewAnnotationArgs, CodeViewDiffItem, CodeViewHandle } from "@pierre/diffs/react"; |
| 6 | +import type { CodeViewAnnotationArgs, CodeViewDiffItem } from "@pierre/diffs/react"; |
7 | 7 | import type { |
8 | 8 | EnvironmentId, |
9 | 9 | PullRequestDetailView, |
@@ -36,7 +36,6 @@ import { |
36 | 36 | use, |
37 | 37 | useEffect, |
38 | 38 | useEffectEvent, |
39 | | - useLayoutEffect, |
40 | 39 | useRef, |
41 | 40 | useState, |
42 | 41 | type ReactNode, |
@@ -313,9 +312,6 @@ function PullRequestCodeTab({ |
313 | 312 | readonly cursor: string | null; |
314 | 313 | readonly slices: ReadonlyArray<DiffSlice>; |
315 | 314 | }>({ key: "", cursor: null, slices: NO_SLICES }); |
316 | | - const [viewer, setViewer] = useState<CodeViewHandle<ReviewAnnotationGroup, undefined> | null>( |
317 | | - null, |
318 | | - ); |
319 | 315 |
|
320 | 316 | const referenceKey = pullRequestReviewKey(reference); |
321 | 317 | const commit = selectedCommitOid; |
@@ -454,7 +450,7 @@ function PullRequestCodeTab({ |
454 | 450 | refreshFilesViewed(); |
455 | 451 | }, [backgroundRefreshToken, refreshFirstDiffPage, refreshFilesViewed]); |
456 | 452 |
|
457 | | - const requestTreeReveal = useCodeViewFileReveal(viewer); |
| 453 | + const codeViewReveal = useCodeViewFileReveal(); |
458 | 454 | const answeredRevealNonce = useRef<number | null>(null); |
459 | 455 |
|
460 | 456 | const loadDiffFiles = createPullRequestDiffFileContentsLoader(getDiffFileContents, { |
@@ -701,14 +697,14 @@ function PullRequestCodeTab({ |
701 | 697 |
|
702 | 698 | const revealFile = (path: string) => { |
703 | 699 | const item = items.find((candidate) => resolveFileDiffPath(candidate.fileDiff) === path); |
704 | | - if (item === undefined) return false; |
705 | | - // Unfold only: a reveal retried before the unfold renders must not fold the file again. |
| 700 | + if (item === undefined) return; |
| 701 | + // Unfold only: a second reveal before the unfold renders must not fold the file again. |
706 | 702 | if (item.collapsed === true) { |
707 | 703 | setToggledFiles((current) => |
708 | 704 | toggleFileDiffFoldForViewed(item.id, false, effectiveFoldOverride, current), |
709 | 705 | ); |
710 | 706 | } |
711 | | - return requestTreeReveal(item.id); |
| 707 | + codeViewReveal.request(item.id); |
712 | 708 | }; |
713 | 709 |
|
714 | 710 | const toggleAllFiles = () => { |
@@ -848,6 +844,7 @@ function PullRequestCodeTab({ |
848 | 844 | enableLineSelection: canSelectLines && draft === null, |
849 | 845 | onGutterUtilityClick: beginComment, |
850 | 846 | onLineSelectionEnd: beginComment, |
| 847 | + onPostRender: codeViewReveal.flush, |
851 | 848 | }; |
852 | 849 |
|
853 | 850 | const runThreadCommand = async ( |
@@ -1284,22 +1281,22 @@ function PullRequestCodeTab({ |
1284 | 1281 | </div> |
1285 | 1282 | </div> |
1286 | 1283 | ); |
1287 | | - // The viewer takes new items in its own layout effect and attaches after the first commit, so |
1288 | | - // a reveal can only position after commit. It stays unanswered until it does, and a file not |
1289 | | - // loaded yet pages forward each time a slice arrives. |
| 1284 | + // A file not loaded yet pages forward each time a slice arrives; once loaded, the viewer |
| 1285 | + // positions it when it renders it. |
1290 | 1286 | const answerReveal = useEffectEvent(() => { |
1291 | 1287 | if (revealRequest === null || answeredRevealNonce.current === revealRequest.nonce) return; |
1292 | 1288 | if (!items.some((item) => resolveFileDiffPath(item.fileDiff) === revealRequest.path)) { |
1293 | 1289 | if (canLoadNextSlice) loadNextSlice(); |
1294 | 1290 | return; |
1295 | 1291 | } |
1296 | | - if (revealFile(revealRequest.path)) answeredRevealNonce.current = revealRequest.nonce; |
| 1292 | + answeredRevealNonce.current = revealRequest.nonce; |
| 1293 | + revealFile(revealRequest.path); |
1297 | 1294 | }); |
1298 | | - useLayoutEffect(() => { |
1299 | | - // oxlint-disable-next-line react/set-state-in-effect -- Positioning the viewer is the external sync; paging forward and unfolding set state only on the way there. |
| 1295 | + useEffect(() => { |
| 1296 | + // oxlint-disable-next-line react/set-state-in-effect -- A reveal for a file not loaded yet pages forward, which sets the slice cursor. |
1300 | 1297 | answerReveal(); |
1301 | | - // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Re-checked as pages load and the viewer attaches until the requested file is positioned. |
1302 | | - }, [canLoadNextSlice, items, revealRequest, viewer]); |
| 1298 | + // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Re-checked as pages load until the requested file arrives. |
| 1299 | + }, [canLoadNextSlice, items, revealRequest]); |
1303 | 1300 | useEffect(() => { |
1304 | 1301 | if (commit !== null && selectedCommit === undefined) { |
1305 | 1302 | onSelectedCommitChange(null); |
@@ -1435,7 +1432,7 @@ function PullRequestCodeTab({ |
1435 | 1432 | <StyledDiffCodeView<ReviewAnnotationGroup> |
1436 | 1433 | className="@container h-full overflow-auto [scrollbar-gutter:stable]" |
1437 | 1434 | containerRef={scrollerRef} |
1438 | | - viewerRef={setViewer} |
| 1435 | + viewerRef={codeViewReveal.attach} |
1439 | 1436 | items={items} |
1440 | 1437 | selectedLines={selectedLines} |
1441 | 1438 | onSelectedLinesChange={setSelectedLines} |
|
0 commit comments