The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 132 lines 4.11 KB
Raw
1 import {
2 cloneFileDiffMetadata,
3 type DiffLineAnnotation,
4 type FileDiffContentsLoader,
5 type FileDiffMetadata,
6 type SelectedLineRange,
7 } from "@pierre/diffs";
8 import { FileDiff } from "@pierre/diffs/react";
9 import { useState } from "react";
10
11 import { useRenderedAppearance } from "~/hooks/useTheme";
12 import { resolveDiffThemeName } from "~/lib/diffRendering";
13 import { PREFERRED_HIGHLIGHTER } from "~/lib/syntaxHighlighting";
14
15 import { DIFF_VIEW_UNSAFE_CSS } from "../../diffs/StyledDiffCodeView";
16 import {
17 getReviewPositionAnchor,
18 PendingReviewCommentCard,
19 toViewerSide,
20 } from "../PullRequestReviewAnnotation";
21 import type { PendingReviewComment } from "../pullRequestReviewStore";
22 import {
23 commentDraftFor,
24 GuideCommentDraft,
25 type CommentDraft,
26 type ExcerptCommenting,
27 } from "./GuideExcerpt";
28
29 interface FileAnnotation {
30 readonly pending: ReadonlyArray<PendingReviewComment>;
31 readonly draft: boolean;
32 }
33
34 export function GuideFileDiff({
35 path,
36 file,
37 commenting,
38 loadDiffFiles,
39 }: {
40 path: string;
41 file: FileDiffMetadata;
42 commenting: ExcerptCommenting | null;
43 loadDiffFiles: FileDiffContentsLoader;
44 }) {
45 const resolvedTheme = useRenderedAppearance();
46 const [draft, setDraft] = useState<CommentDraft | null>(null);
47 // Loading the unchanged lines rewrites the diff it is given; the excerpts read the same file
48 // as a patch, so this view expands a copy of its own.
49 const expandable = cloneFileDiffMetadata(file);
50
51 const annotations = (() => {
52 const groups = new Map<string, DiffLineAnnotation<FileAnnotation>>();
53 const update = (
54 side: "additions" | "deletions",
55 line: number,
56 change: (metadata: FileAnnotation) => FileAnnotation,
57 ) => {
58 const key = `${side}:${line}`;
59 const group = groups.get(key) ?? {
60 side,
61 lineNumber: line,
62 metadata: { pending: [], draft: false },
63 };
64 groups.set(key, { ...group, metadata: change(group.metadata) });
65 };
66 for (const comment of commenting?.pending ?? []) {
67 if (comment.path !== path) continue;
68 const anchor = getReviewPositionAnchor(comment.position);
69 update(toViewerSide(anchor.side), anchor.line, (metadata) => ({
70 ...metadata,
71 pending: [...metadata.pending, comment],
72 }));
73 }
74 if (draft !== null) {
75 const anchor = getReviewPositionAnchor(draft.position);
76 update(toViewerSide(anchor.side), anchor.line, (metadata) => ({ ...metadata, draft: true }));
77 }
78 return [...groups.values()];
79 })();
80
81 const beginComment = (range: SelectedLineRange | null) => {
82 const next = commentDraftFor(file, range, commenting);
83 if (next !== null) setDraft(next);
84 };
85 const interactive = commenting !== null && draft === null;
86
87 const options = {
88 diffStyle: "unified" as const,
89 lineDiffType: "none" as const,
90 overflow: "scroll" as const,
91 disableFileHeader: true,
92 theme: resolveDiffThemeName(resolvedTheme),
93 themeType: resolvedTheme,
94 preferredHighlighter: PREFERRED_HIGHLIGHTER,
95 unsafeCSS: DIFF_VIEW_UNSAFE_CSS,
96 enableGutterUtility: interactive,
97 enableLineSelection: interactive,
98 onGutterUtilityClick: beginComment,
99 onLineSelectionEnd: beginComment,
100 loadDiffFiles,
101 };
102 return (
103 <div className="border-t border-border/60">
104 <FileDiff<FileAnnotation>
105 fileDiff={expandable}
106 options={options}
107 lineAnnotations={annotations}
108 selectedLines={draft?.range ?? null}
109 renderAnnotation={(annotation) => (
110 <div className="font-sans">
111 {annotation.metadata.pending.map((comment) => (
112 <PendingReviewCommentCard
113 key={comment.id}
114 comment={comment}
115 onRemove={() => commenting?.onRemove(comment.id)}
116 />
117 ))}
118 {annotation.metadata.draft && draft !== null && commenting !== null ? (
119 <GuideCommentDraft
120 path={path}
121 file={file}
122 draft={draft}
123 commenting={commenting}
124 onClose={() => setDraft(null)}
125 />
126 ) : null}
127 </div>
128 )}
129 />
130 </div>
131 );
132 }