The best way to control Claude Code, Codex and more from any computer or phone
perf(web): the file preview panel compiles with the React Compiler (#20692)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 16, 2026 at 21:49 UTC
06299ff56ad6ead52e94c9a8cc44e63c76a3b5b7
1 file changed
+266
-324
apps/web/src/components/files/FilePreviewPanel.tsx
+266
-324
@@ -40,7 +40,7 @@ import { mediaFileReference } from "@t3tools/client-runtime/media-reference";
40
import { FolderTree, Globe2, WrapTextIcon } from "lucide-react";
41
import { Code2, Eye, Table2 } from "lucide";
42
import * as Schema from "effect/Schema";
43
-import { useCallback, useEffect, useMemo, useRef, useState } from "react";
43
+import { useEffect, useRef, useState } from "react";
44
45
import { isBrowserPreviewFile, openFileInPreview } from "~/browser/openFileInPreview";
46
import { useAssetUrlRefresh, useAssetUrlState } from "~/assets/assetUrls";
@@ -158,17 +158,13 @@ function WorkspaceImagePreview(props: {
158
readonly alt: string;
159
readonly workspaceMutationId: string | null;
160
}) {
161
- const resource = useMemo(
162
- () =>
163
- workspaceAssetResource({
164
- kind: "workspace-file",
165
- threadRef: props.threadRef,
166
- draft: props.draft,
167
- workspaceRoot: props.workspaceRoot,
168
- absolutePath: props.absolutePath,
169
- }),
170
- [props.threadRef, props.draft, props.workspaceRoot, props.absolutePath],
171
- );
161
+ const resource = workspaceAssetResource({
162
+ kind: "workspace-file",
163
+ threadRef: props.threadRef,
164
+ draft: props.draft,
165
+ workspaceRoot: props.workspaceRoot,
166
+ absolutePath: props.absolutePath,
167
+ });
168
const assetUrl = useAssetUrlState(props.environmentId, resource);
169
const [failedUrl, setFailedUrl] = useState<string | null>(null);
170
const revisionSuffix =
@@ -228,17 +224,13 @@ function WorkspaceBrowserPreview(props: {
224
}) {
225
const insideWorkspace =
226
mediaFileReference(props.absolutePath, props.workspaceRoot).relativePath !== undefined;
231
- const resource = useMemo(
232
- () =>
233
- workspaceAssetResource({
234
- kind: insideWorkspace ? "workspace-file" : "media-file",
235
- threadRef: props.threadRef,
236
- draft: props.draft,
237
- workspaceRoot: props.workspaceRoot,
238
- absolutePath: props.absolutePath,
239
- }),
240
- [insideWorkspace, props.threadRef, props.draft, props.workspaceRoot, props.absolutePath],
241
- );
227
+ const resource = workspaceAssetResource({
228
+ kind: insideWorkspace ? "workspace-file" : "media-file",
229
+ threadRef: props.threadRef,
230
+ draft: props.draft,
231
+ workspaceRoot: props.workspaceRoot,
232
+ absolutePath: props.absolutePath,
233
+ });
234
const assetUrl = useAssetUrlState(props.environmentId, resource);
235
const revisionSuffix =
236
props.workspaceMutationId === null
@@ -277,17 +269,13 @@ function WorkspaceVideoPreview(props: {
269
readonly name: string;
270
readonly workspaceMutationId: string | null;
271
}) {
280
- const resource = useMemo(
281
- () =>
282
- workspaceAssetResource({
283
- kind: "media-file",
284
- threadRef: props.threadRef,
285
- draft: props.draft,
286
- workspaceRoot: props.workspaceRoot,
287
- absolutePath: props.absolutePath,
288
- }),
289
- [props.threadRef, props.draft, props.workspaceRoot, props.absolutePath],
290
- );
272
+ const resource = workspaceAssetResource({
273
+ kind: "media-file",
274
+ threadRef: props.threadRef,
275
+ draft: props.draft,
276
+ workspaceRoot: props.workspaceRoot,
277
+ absolutePath: props.absolutePath,
278
+ });
279
const assetUrl = useAssetUrlState(props.environmentId, resource);
280
const refreshAssetUrl = useAssetUrlRefresh(props.environmentId, resource);
281
useWorkspaceMutationRefresh({
@@ -334,17 +322,13 @@ function WorkspaceAudioPreview(props: {
322
readonly name: string;
323
readonly workspaceMutationId: string | null;
324
}) {
337
- const resource = useMemo(
338
- () =>
339
- workspaceAssetResource({
340
- kind: "media-file",
341
- threadRef: props.threadRef,
342
- draft: props.draft,
343
- workspaceRoot: props.workspaceRoot,
344
- absolutePath: props.absolutePath,
345
- }),
346
- [props.threadRef, props.draft, props.workspaceRoot, props.absolutePath],
347
- );
325
+ const resource = workspaceAssetResource({
326
+ kind: "media-file",
327
+ threadRef: props.threadRef,
328
+ draft: props.draft,
329
+ workspaceRoot: props.workspaceRoot,
330
+ absolutePath: props.absolutePath,
331
+ });
332
const assetUrl = useAssetUrlState(props.environmentId, resource);
333
const refreshAssetUrl = useAssetUrlRefresh(props.environmentId, resource);
334
const [failedUrl, setFailedUrl] = useState<string | null>(null);
@@ -427,164 +411,161 @@ function useFileLineReveal(
411
): FilePostRender {
412
const [revealStatesByPath] = useState(() => new Map<string, FileRevealState>());
413
430
- return useCallback<FilePostRender>(
431
- (fileContainer, instance, phase) => {
432
- if (relativePath === null) return;
433
-
434
- const existingState = revealStatesByPath.get(relativePath);
435
- const state: FileRevealState = existingState ?? {
436
- frameId: null,
437
- cancelGuard: null,
438
- handledRequestId: null,
439
- latestRequestId: null,
440
- };
441
- if (!existingState) revealStatesByPath.set(relativePath, state);
414
+ return (fileContainer, instance, phase) => {
415
+ if (relativePath === null) return;
416
443
- const cancelPendingReveal = () => {
444
- if (state.frameId !== null) {
445
- cancelAnimationFrame(state.frameId);
446
- state.frameId = null;
447
- }
448
- state.cancelGuard?.();
449
- };
417
+ const existingState = revealStatesByPath.get(relativePath);
418
+ const state: FileRevealState = existingState ?? {
419
+ frameId: null,
420
+ cancelGuard: null,
421
+ handledRequestId: null,
422
+ latestRequestId: null,
423
+ };
424
+ if (!existingState) revealStatesByPath.set(relativePath, state);
425
451
- if (phase === "unmount") {
452
- cancelPendingReveal();
453
- return;
426
+ const cancelPendingReveal = () => {
427
+ if (state.frameId !== null) {
428
+ cancelAnimationFrame(state.frameId);
429
+ state.frameId = null;
430
}
431
+ state.cancelGuard?.();
432
+ };
433
456
- const contents = instance.file?.contents;
457
- const targetLine =
458
- revealLine === null || contents === undefined ? null : clampFileLine(contents, revealLine);
459
- updateFileLinkReveal(fileContainer, targetLine);
434
+ if (phase === "unmount") {
435
+ cancelPendingReveal();
436
+ return;
437
+ }
438
461
- if (!(instance instanceof VirtualizedFile)) return;
439
+ const contents = instance.file?.contents;
440
+ const targetLine =
441
+ revealLine === null || contents === undefined ? null : clampFileLine(contents, revealLine);
442
+ updateFileLinkReveal(fileContainer, targetLine);
443
463
- if (state.latestRequestId !== revealRequestId) {
464
- cancelPendingReveal();
465
- state.latestRequestId = revealRequestId;
466
- state.handledRequestId = null;
467
- }
444
+ if (!(instance instanceof VirtualizedFile)) return;
445
469
- if (revealLine === null) {
470
- fileContainer.style.minHeight = "";
471
- return;
472
- }
446
+ if (state.latestRequestId !== revealRequestId) {
447
+ cancelPendingReveal();
448
+ state.latestRequestId = revealRequestId;
449
+ state.handledRequestId = null;
450
+ }
451
474
- const scrollContainer = fileContainer.closest<HTMLElement>(".file-preview-virtualizer");
475
- if (!scrollContainer) return;
476
- fileContainer.style.minHeight = `${Math.ceil(
477
- Math.max(instance.height, scrollContainer.clientHeight),
478
- )}px`;
452
+ if (revealLine === null) {
453
+ fileContainer.style.minHeight = "";
454
+ return;
455
+ }
456
480
- if (state.handledRequestId === revealRequestId || state.frameId !== null) {
481
- return;
482
- }
457
+ const scrollContainer = fileContainer.closest<HTMLElement>(".file-preview-virtualizer");
458
+ if (!scrollContainer) return;
459
+ fileContainer.style.minHeight = `${Math.ceil(
460
+ Math.max(instance.height, scrollContainer.clientHeight),
461
+ )}px`;
462
484
- const resolveScrollTarget = (line: number): number | null => {
485
- const linePosition = instance.getLinePosition(line);
486
- if (!linePosition) return null;
463
+ if (state.handledRequestId === revealRequestId || state.frameId !== null) {
464
+ return;
465
+ }
466
488
- const scrollContainerRect = scrollContainer.getBoundingClientRect();
489
- const fileTop =
490
- scrollContainer.scrollTop +
491
- fileContainer.getBoundingClientRect().top -
492
- scrollContainerRect.top;
493
- const root = fileContainer.shadowRoot ?? fileContainer;
494
- const renderedLineElement = root.querySelector<HTMLElement>(`[data-line="${line}"]`);
495
- const renderedLineRect = renderedLineElement?.getBoundingClientRect();
467
+ const resolveScrollTarget = (line: number): number | null => {
468
+ const linePosition = instance.getLinePosition(line);
469
+ if (!linePosition) return null;
470
497
- return resolveCenteredFileLineScrollTop({
498
- scrollTop: scrollContainer.scrollTop,
499
- scrollHeight: scrollContainer.scrollHeight,
500
- viewportTop: scrollContainerRect.top,
501
- viewportHeight: scrollContainer.clientHeight,
502
- fileTop,
503
- estimatedLine: linePosition,
504
- ...(renderedLineRect && renderedLineRect.height > 0
505
- ? {
506
- renderedLine: {
507
- top: renderedLineRect.top,
508
- height: renderedLineRect.height,
509
- },
510
- }
511
- : {}),
512
- });
513
- };
471
+ const scrollContainerRect = scrollContainer.getBoundingClientRect();
472
+ const fileTop =
473
+ scrollContainer.scrollTop +
474
+ fileContainer.getBoundingClientRect().top -
475
+ scrollContainerRect.top;
476
+ const root = fileContainer.shadowRoot ?? fileContainer;
477
+ const renderedLineElement = root.querySelector<HTMLElement>(`[data-line="${line}"]`);
478
+ const renderedLineRect = renderedLineElement?.getBoundingClientRect();
479
+
480
+ return resolveCenteredFileLineScrollTop({
481
+ scrollTop: scrollContainer.scrollTop,
482
+ scrollHeight: scrollContainer.scrollHeight,
483
+ viewportTop: scrollContainerRect.top,
484
+ viewportHeight: scrollContainer.clientHeight,
485
+ fileTop,
486
+ estimatedLine: linePosition,
487
+ ...(renderedLineRect && renderedLineRect.height > 0
488
+ ? {
489
+ renderedLine: {
490
+ top: renderedLineRect.top,
491
+ height: renderedLineRect.height,
492
+ },
493
+ }
494
+ : {}),
495
+ });
496
+ };
497
515
- const guardScrollTarget = (line: number) => {
516
- let framesLeft = REVEAL_GUARD_FRAMES;
517
- let guardFrameId: number | null = null;
518
- const cancelGuard = () => {
519
- if (guardFrameId !== null) {
520
- cancelAnimationFrame(guardFrameId);
521
- guardFrameId = null;
522
- }
523
- scrollContainer.removeEventListener("wheel", cancelGuard);
524
- scrollContainer.removeEventListener("touchstart", cancelGuard);
525
- scrollContainer.removeEventListener("pointerdown", cancelGuard, true);
526
- window.removeEventListener("keydown", cancelGuard, true);
527
- if (state.cancelGuard === cancelGuard) state.cancelGuard = null;
528
- };
529
- scrollContainer.addEventListener("wheel", cancelGuard, { passive: true });
530
- scrollContainer.addEventListener("touchstart", cancelGuard, { passive: true });
531
- // Pierre stops gutter pointer events from bubbling. Listen in capture
532
- // so starting a comment cancels the reveal guard before the row expands.
533
- scrollContainer.addEventListener("pointerdown", cancelGuard, {
534
- passive: true,
535
- capture: true,
536
- });
537
- window.addEventListener("keydown", cancelGuard, true);
538
- const holdTarget = () => {
498
+ const guardScrollTarget = (line: number) => {
499
+ let framesLeft = REVEAL_GUARD_FRAMES;
500
+ let guardFrameId: number | null = null;
501
+ const cancelGuard = () => {
502
+ if (guardFrameId !== null) {
503
+ cancelAnimationFrame(guardFrameId);
504
guardFrameId = null;
540
- framesLeft -= 1;
541
- if (framesLeft <= 0 || !scrollContainer.isConnected) {
542
- cancelGuard();
543
- return;
544
- }
545
- const targetTop = resolveScrollTarget(line);
546
- if (
547
- targetTop !== null &&
548
- Math.abs(scrollContainer.scrollTop - targetTop) > REVEAL_GUARD_TOLERANCE_PX
549
- ) {
550
- scrollContainer.scrollTop = targetTop;
551
- }
552
- guardFrameId = requestAnimationFrame(holdTarget);
553
- };
505
+ }
506
+ scrollContainer.removeEventListener("wheel", cancelGuard);
507
+ scrollContainer.removeEventListener("touchstart", cancelGuard);
508
+ scrollContainer.removeEventListener("pointerdown", cancelGuard, true);
509
+ window.removeEventListener("keydown", cancelGuard, true);
510
+ if (state.cancelGuard === cancelGuard) state.cancelGuard = null;
511
+ };
512
+ scrollContainer.addEventListener("wheel", cancelGuard, { passive: true });
513
+ scrollContainer.addEventListener("touchstart", cancelGuard, { passive: true });
514
+ // Pierre stops gutter pointer events from bubbling. Listen in capture
515
+ // so starting a comment cancels the reveal guard before the row expands.
516
+ scrollContainer.addEventListener("pointerdown", cancelGuard, {
517
+ passive: true,
518
+ capture: true,
519
+ });
520
+ window.addEventListener("keydown", cancelGuard, true);
521
+ const holdTarget = () => {
522
+ guardFrameId = null;
523
+ framesLeft -= 1;
524
+ if (framesLeft <= 0 || !scrollContainer.isConnected) {
525
+ cancelGuard();
526
+ return;
527
+ }
528
+ const targetTop = resolveScrollTarget(line);
529
+ if (
530
+ targetTop !== null &&
531
+ Math.abs(scrollContainer.scrollTop - targetTop) > REVEAL_GUARD_TOLERANCE_PX
532
+ ) {
533
+ scrollContainer.scrollTop = targetTop;
534
+ }
535
guardFrameId = requestAnimationFrame(holdTarget);
555
- state.cancelGuard = cancelGuard;
536
};
537
+ guardFrameId = requestAnimationFrame(holdTarget);
538
+ state.cancelGuard = cancelGuard;
539
+ };
540
558
- const scheduleReveal = (attempt: number) => {
559
- state.frameId = requestAnimationFrame(() => {
560
- state.frameId = null;
561
- if (state.latestRequestId !== revealRequestId || !fileContainer.isConnected) {
562
- return;
563
- }
541
+ const scheduleReveal = (attempt: number) => {
542
+ state.frameId = requestAnimationFrame(() => {
543
+ state.frameId = null;
544
+ if (state.latestRequestId !== revealRequestId || !fileContainer.isConnected) {
545
+ return;
546
+ }
547
565
- // Contents and line metrics can lag the first post-render on fresh
566
- // mounts; clamping against missing contents would scroll to line 1
567
- // and wrongly mark the request handled.
568
- const currentContents = instance.file?.contents;
569
- const line =
570
- currentContents === undefined ? null : clampFileLine(currentContents, revealLine);
571
- const targetTop = line === null ? null : resolveScrollTarget(line);
572
- if (line === null || targetTop === null) {
573
- if (attempt < REVEAL_MAX_ATTEMPTS) scheduleReveal(attempt + 1);
574
- return;
575
- }
576
- updateFileLinkReveal(fileContainer, line);
548
+ // Contents and line metrics can lag the first post-render on fresh
549
+ // mounts; clamping against missing contents would scroll to line 1
550
+ // and wrongly mark the request handled.
551
+ const currentContents = instance.file?.contents;
552
+ const line =
553
+ currentContents === undefined ? null : clampFileLine(currentContents, revealLine);
554
+ const targetTop = line === null ? null : resolveScrollTarget(line);
555
+ if (line === null || targetTop === null) {
556
+ if (attempt < REVEAL_MAX_ATTEMPTS) scheduleReveal(attempt + 1);
557
+ return;
558
+ }
559
+ updateFileLinkReveal(fileContainer, line);
560
578
- scrollContainer.scrollTop = targetTop;
579
- state.handledRequestId = revealRequestId;
580
- guardScrollTarget(line);
581
- });
582
- };
561
+ scrollContainer.scrollTop = targetTop;
562
+ state.handledRequestId = revealRequestId;
563
+ guardScrollTarget(line);
564
+ });
565
+ };
566
584
- scheduleReveal(0);
585
- },
586
- [revealStatesByPath, relativePath, revealLine, revealRequestId],
587
- );
567
+ scheduleReveal(0);
568
+ };
569
}
570
571
const createFileEditor: EditorFactory<FileCommentAnnotationGroup, undefined> = (
@@ -628,7 +609,7 @@ function needsWorkerHighlight(workerPool: WorkerPoolManager | undefined, file: F
609
function useEditableAfterHighlight(file: FileContents) {
610
const workerPool = useWorkerPool();
611
const [highlightedFile, setHighlightedFile] = useState<FileContents | null>(null);
631
- const needsHighlight = useMemo(() => needsWorkerHighlight(workerPool, file), [file, workerPool]);
612
+ const needsHighlight = needsWorkerHighlight(workerPool, file);
613
const ready = !needsHighlight || highlightedFile === file;
614
615
useEffect(() => {
@@ -636,15 +617,12 @@ function useEditableAfterHighlight(file: FileContents) {
617
workerPool.primeFileHighlightCache(file).catch(() => setHighlightedFile(file));
618
}, [file, ready, workerPool]);
619
639
- const onPostRender = useCallback(
640
- (renderedFile: FileContents | undefined, phase: PostRenderPhase) => {
641
- if (ready || phase === "unmount" || renderedFile?.cacheKey !== file.cacheKey) return;
642
- // The pool caches a result just before the instance renders it, so a
643
- // render that sees the cache has painted highlighted rows.
644
- if (workerPool?.getFileResultCache(file) !== undefined) setHighlightedFile(file);
645
- },
646
- [file, ready, workerPool],
647
- );
620
+ const onPostRender = (renderedFile: FileContents | undefined, phase: PostRenderPhase) => {
621
+ if (ready || phase === "unmount" || renderedFile?.cacheKey !== file.cacheKey) return;
622
+ // The pool caches a result just before the instance renders it, so a
623
+ // render that sees the cache has painted highlighted rows.
624
+ if (workerPool?.getFileResultCache(file) !== undefined) setHighlightedFile(file);
625
+ };
626
return { ready, onPostRender };
627
}
628
@@ -684,12 +662,9 @@ function EditableFileSurface({
662
const [selectionOverride, setSelectionOverride] = useState<FileSelectionOverride | null>(null);
663
const selectedRange =
664
selectionOverride?.revealRequestId === revealRequestId ? selectionOverride.range : null;
687
- const setSelectedRange = useCallback(
688
- (range: SelectedLineRange | null) => {
689
- setSelectionOverride({ revealRequestId, range });
690
- },
691
- [revealRequestId],
692
- );
665
+ const setSelectedRange = (range: SelectedLineRange | null) => {
666
+ setSelectionOverride({ revealRequestId, range });
667
+ };
668
const surfaceRef = useRef<HTMLDivElement>(null);
669
const selectionFrameRef = useRef<number | null>(null);
670
const saveCoordinator = useFileSaveCoordinator({
@@ -711,72 +686,30 @@ function EditableFileSurface({
686
const { ready: editable, onPostRender: onEditablePostRender } =
687
useEditableAfterHighlight(externalFile);
688
const editorRef = useRef<Editor<"file", FileCommentAnnotationGroup, undefined> | null>(null);
714
- const editorOptions = useMemo<EditorOptions<"file", FileCommentAnnotationGroup, undefined>>(
715
- () => ({
716
- onAttach: (editor) => {
717
- editorRef.current = editor;
718
- },
719
- onComplete: () => {
720
- editorRef.current = null;
721
- },
722
- }),
723
- [],
724
- );
725
-
726
- const handleEditChange = useCallback(
727
- ({
728
- file,
729
- lineAnnotations: nextLineAnnotations,
730
- }: EditorChangeEvent<"file", FileCommentAnnotationGroup, undefined>) => {
731
- if (file.contents === getProjectFileContents(environmentId, cwd, relativePath)) return;
732
- setEditedContents(file.contents);
733
- setProjectFileQueryData(environmentId, cwd, relativePath, file.contents);
734
- saveCoordinator.change(file.contents);
735
- if (!nextLineAnnotations) return;
736
- const remapped = remapFileCommentAnnotations(nextLineAnnotations);
737
- // The editor hands back the array it was given until an edit moves an annotation.
738
- setLineAnnotations((current) => (current === nextLineAnnotations ? current : remapped));
739
- for (const annotation of remapped) {
740
- for (const entry of annotation.metadata.entries) {
741
- if (entry.kind !== "comment") continue;
742
- addReviewComment(
743
- composerDraftTarget,
744
- buildFileReviewComment({
745
- id: entry.id,
746
- filePath: relativePath,
747
- startLine: entry.startLine,
748
- endLine: entry.endLine,
749
- text: entry.text,
750
- contents: file.contents,
751
- }),
752
- );
753
- }
754
- }
689
+ const editorOptions: EditorOptions<"file", FileCommentAnnotationGroup, undefined> = {
690
+ onAttach: (editor) => {
691
+ editorRef.current = editor;
692
},
756
- [addReviewComment, composerDraftTarget, cwd, environmentId, relativePath, saveCoordinator],
757
- );
758
-
759
- const removeAnnotationEntry = useCallback(
760
- (entryId: string) => {
761
- setSelectedRange(null);
762
- removeReviewComment(composerDraftTarget, entryId);
763
- setLineAnnotations((current) => {
764
- return current.flatMap((annotation) => {
765
- const entries = annotation.metadata.entries.filter((entry) => entry.id !== entryId);
766
- return entries.length > 0 ? [{ ...annotation, metadata: { entries } }] : [];
767
- });
768
- });
693
+ onComplete: () => {
694
+ editorRef.current = null;
695
},
770
- [composerDraftTarget, removeReviewComment, setSelectedRange],
771
- );
696
+ };
697
773
- const submitAnnotationEntry = useCallback(
774
- (entryId: string, text: string) => {
775
- setSelectedRange(null);
776
- const entry = lineAnnotations
777
- .flatMap((annotation) => annotation.metadata.entries)
778
- .find((candidate) => candidate.id === entryId);
779
- if (entry) {
698
+ const handleEditChange = ({
699
+ file,
700
+ lineAnnotations: nextLineAnnotations,
701
+ }: EditorChangeEvent<"file", FileCommentAnnotationGroup, undefined>) => {
702
+ if (file.contents === getProjectFileContents(environmentId, cwd, relativePath)) return;
703
+ setEditedContents(file.contents);
704
+ setProjectFileQueryData(environmentId, cwd, relativePath, file.contents);
705
+ saveCoordinator.change(file.contents);
706
+ if (!nextLineAnnotations) return;
707
+ const remapped = remapFileCommentAnnotations(nextLineAnnotations);
708
+ // The editor hands back the array it was given until an edit moves an annotation.
709
+ setLineAnnotations((current) => (current === nextLineAnnotations ? current : remapped));
710
+ for (const annotation of remapped) {
711
+ for (const entry of annotation.metadata.entries) {
712
+ if (entry.kind !== "comment") continue;
713
addReviewComment(
714
composerDraftTarget,
715
buildFileReviewComment({
@@ -784,35 +717,58 @@ function EditableFileSurface({
717
filePath: relativePath,
718
startLine: entry.startLine,
719
endLine: entry.endLine,
787
- text,
788
- contents,
720
+ text: entry.text,
721
+ contents: file.contents,
722
}),
723
);
724
}
792
- setLineAnnotations((current) =>
793
- current.map((annotation) => ({
794
- ...annotation,
795
- metadata: {
796
- entries: annotation.metadata.entries.map((annotationEntry) =>
797
- annotationEntry.id === entryId
798
- ? { ...annotationEntry, kind: "comment", text }
799
- : annotationEntry,
800
- ),
801
- },
802
- })),
725
+ }
726
+ };
727
+
728
+ const removeAnnotationEntry = (entryId: string) => {
729
+ setSelectedRange(null);
730
+ removeReviewComment(composerDraftTarget, entryId);
731
+ setLineAnnotations((current) => {
732
+ return current.flatMap((annotation) => {
733
+ const entries = annotation.metadata.entries.filter((entry) => entry.id !== entryId);
734
+ return entries.length > 0 ? [{ ...annotation, metadata: { entries } }] : [];
735
+ });
736
+ });
737
+ };
738
+
739
+ const submitAnnotationEntry = (entryId: string, text: string) => {
740
+ setSelectedRange(null);
741
+ const entry = lineAnnotations
742
+ .flatMap((annotation) => annotation.metadata.entries)
743
+ .find((candidate) => candidate.id === entryId);
744
+ if (entry) {
745
+ addReviewComment(
746
+ composerDraftTarget,
747
+ buildFileReviewComment({
748
+ id: entry.id,
749
+ filePath: relativePath,
750
+ startLine: entry.startLine,
751
+ endLine: entry.endLine,
752
+ text,
753
+ contents,
754
+ }),
755
);
804
- },
805
- [
806
- addReviewComment,
807
- composerDraftTarget,
808
- contents,
809
- lineAnnotations,
810
- relativePath,
811
- setSelectedRange,
812
- ],
813
- );
756
+ }
757
+ setLineAnnotations((current) =>
758
+ current.map((annotation) => ({
759
+ ...annotation,
760
+ metadata: {
761
+ entries: annotation.metadata.entries.map((annotationEntry) =>
762
+ annotationEntry.id === entryId
763
+ ? { ...annotationEntry, kind: "comment", text }
764
+ : annotationEntry,
765
+ ),
766
+ },
767
+ })),
768
+ );
769
+ };
770
815
- const beginComment = useCallback((range: SelectedLineRange) => {
771
+ const beginComment = (range: SelectedLineRange) => {
772
editorRef.current?.setSelections([]);
773
editorRef.current?.blur();
774
const { startLine, endLine } = normalizeFileCommentRange(range);
@@ -849,7 +805,7 @@ function EditableFileSurface({
805
: annotation,
806
);
807
});
852
- }, []);
808
+ };
809
const hasOpenCommentForm = lineAnnotations.some((annotation) =>
810
annotation.metadata.entries.some((entry) => entry.kind === "draft"),
811
);
@@ -863,35 +819,29 @@ function EditableFileSurface({
819
onDismiss: () => setSelectedRange(null),
820
});
821
}, [hasOpenCommentForm, setSelectedRange]);
866
- const handleLineSelectionEnd = useCallback(
867
- (range: SelectedLineRange | null) => {
868
- setSelectedRange(range);
869
- if (range) {
870
- beginComment(range);
871
- }
872
- },
873
- [beginComment, setSelectedRange],
874
- );
822
+ const handleLineSelectionEnd = (range: SelectedLineRange | null) => {
823
+ setSelectedRange(range);
824
+ if (range) {
825
+ beginComment(range);
826
+ }
827
+ };
828
876
- const handlePostRender = useCallback<FilePostRender>(
877
- (fileContainer, instance, phase) => {
878
- onPostRender(fileContainer, instance, phase);
879
- onEditablePostRender(instance.file, phase);
829
+ const handlePostRender: FilePostRender = (fileContainer, instance, phase) => {
830
+ onPostRender(fileContainer, instance, phase);
831
+ onEditablePostRender(instance.file, phase);
832
881
- if (selectionFrameRef.current !== null) {
882
- cancelAnimationFrame(selectionFrameRef.current);
883
- selectionFrameRef.current = null;
884
- }
885
- if (phase === "unmount") return;
833
+ if (selectionFrameRef.current !== null) {
834
+ cancelAnimationFrame(selectionFrameRef.current);
835
+ selectionFrameRef.current = null;
836
+ }
837
+ if (phase === "unmount") return;
838
887
- selectionFrameRef.current = requestAnimationFrame(() => {
888
- selectionFrameRef.current = null;
889
- if (!fileContainer.isConnected) return;
890
- instance.setSelectedLines(selectedRange, { notify: false });
891
- });
892
- },
893
- [onEditablePostRender, onPostRender, selectedRange],
894
- );
839
+ selectionFrameRef.current = requestAnimationFrame(() => {
840
+ selectionFrameRef.current = null;
841
+ if (!fileContainer.isConnected) return;
842
+ instance.setSelectedLines(selectedRange, { notify: false });
843
+ });
844
+ };
845
846
return (
847
<EditProvider createEditor={createFileEditor}>
@@ -1191,6 +1141,7 @@ export default function FilePreviewPanel({
1141
breadcrumbRef.current
1142
?.querySelector<HTMLElement>("[data-current-file-crumb='true']")
1143
?.scrollIntoView({ block: "nearest", inline: "nearest" });
1144
+ // oxlint-disable-next-line react/exhaustive-effect-dependencies -- A new path or trail re-renders the crumbs, so the current one scrolls back into view.
1145
}, [breadcrumbPath, breadcrumbTrail]);
1146
1147
const toggleExplorer = () => {
@@ -1205,7 +1156,7 @@ export default function FilePreviewPanel({
1156
});
1157
};
1158
1208
- const handleOpenInBrowser = useCallback(() => {
1159
+ const handleOpenInBrowser = () => {
1160
if (!canReadFiles || !canOperatePreview || !absolutePath || !environmentHttpBaseUrl) return;
1161
void (async () => {
1162
const result = await openFileInPreview({
@@ -1228,16 +1179,7 @@ export default function FilePreviewPanel({
1179
}),
1180
);
1181
})();
1231
- }, [
1232
- absolutePath,
1233
- canReadFiles,
1234
- canOperatePreview,
1235
- createAssetUrl,
1236
- cwd,
1237
- environmentHttpBaseUrl,
1238
- openPreview,
1239
- threadRef,
1240
- ]);
1182
+ };
1183
1184
if (attachment === undefined && !canReadFiles) {
1185
if (fileAccess.isPending) {