Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GuideFileDiff.tsx

133 lines4.1 KB

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