Open History to see the last commit
1import { memo, useMemo, useState } from "react";
2import { getFiletypeFromFileName } from "@pierre/diffs/utils/getFiletypeFromFileName";
3import { ScrollView, StyleSheet, Text as NativeText, View, type ColorValue } from "react-native";
4import { SelectableMarkdownText } from "../../native/SelectableMarkdownText";
5import { useMarkdownPreviewStyles } from "../files/FileMarkdownPreview";
6import { AppText as Text } from "../../components/ui/AppText";
7import { SymbolView } from "../../components/ui/AppSymbol";
8import { useUniwindTheme } from "../../lib/useUniwindTheme";
9import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
10import { useAppearanceCodeSurface } from "../settings/appearance/useAppearanceCodeSurface";
11import { resolveNativeReviewDiffView } from "../diffs/nativeReviewDiffSurface";
12import {
13 buildNativeReviewDiffData,
14 buildNativeReviewSnippetRows,
15 createNativeReviewDiffTheme,
16 NATIVE_REVIEW_DIFF_CONTENT_WIDTH,
17} from "./nativeReviewDiffAdapter";
18import { buildReviewParsedDiff } from "./reviewModel";
19import { REVIEW_MONO_FONT_FAMILY } from "./reviewDiffRendering";
20import type { ReviewInlineComment } from "./reviewCommentSelection";
21import { useMarkdownCodeHighlight } from "../threads/markdownCodeHighlightState";
22
23export interface ReviewCommentColors {
24 readonly background: ColorValue;
25 readonly border: ColorValue;
26 readonly mutedBackground: ColorValue;
27 readonly text: ColorValue;
28 readonly mutedText: ColorValue;
29 readonly codeBackground: ColorValue;
30}
31
32export function useReviewCommentColors(): ReviewCommentColors {
33 const theme = useUniwindTheme();
34
35 return useMemo(
36 () => ({
37 background: theme["--color-card"],
38 border: theme["--color-border"],
39 mutedBackground: theme["--color-subtle"],
40 text: theme["--color-foreground"],
41 mutedText: theme["--color-foreground-muted"],
42 codeBackground: theme["--color-md-code-bg"],
43 }),
44 [theme],
45 );
46}
47
48export const ReviewCommentCard = memo(function ReviewCommentCard(props: {
49 readonly comment: ReviewInlineComment;
50 readonly colors: ReviewCommentColors;
51}) {
52 const { codeSurface, nativeReviewDiffStyle } = useAppearanceCodeSurface();
53 const markdownStyles = useMarkdownPreviewStyles();
54 const { themeAppearance: appearanceScheme, themeId } = useAppearancePreferences();
55 const appTheme = useUniwindTheme();
56 const [NativeReviewDiffView] = useState(() => resolveNativeReviewDiffView());
57 const patch = useMemo(() => buildReviewCommentPatch(props.comment), [props.comment]);
58 const parsedDiff = useMemo(
59 () => buildReviewParsedDiff(patch, `thread-review-comment:${props.comment.id}`),
60 [patch, props.comment.id],
61 );
62 const nativeReviewDiffData = useMemo(() => buildNativeReviewDiffData(parsedDiff), [parsedDiff]);
63 const compactNativeRows = useMemo(() => {
64 const rows = nativeReviewDiffData.rows.filter((row) => row.kind !== "file");
65 return rows.length > 0 ? rows : buildNativeReviewSnippetRows(props.comment);
66 }, [nativeReviewDiffData.rows, props.comment]);
67 const language =
68 props.comment.fenceLanguage && props.comment.fenceLanguage !== "diff"
69 ? props.comment.fenceLanguage
70 : getFiletypeFromFileName(props.comment.filePath);
71 const addedRows = useMemo(
72 () => compactNativeRows.filter((row) => row.kind === "line" && row.change !== "delete"),
73 [compactNativeRows],
74 );
75 const deletedRows = useMemo(
76 () => compactNativeRows.filter((row) => row.kind === "line" && row.change === "delete"),
77 [compactNativeRows],
78 );
79 const addedTokens = useMarkdownCodeHighlight({
80 code: addedRows.map((row) => row.content ?? "").join("\n"),
81 language,
82 enabled: addedRows.length > 0,
83 theme: appearanceScheme,
84 });
85 const deletedTokens = useMarkdownCodeHighlight({
86 code: deletedRows.map((row) => row.content ?? "").join("\n"),
87 language,
88 enabled: deletedRows.length > 0,
89 theme: appearanceScheme,
90 });
91 const snippetTokens = useMarkdownCodeHighlight({
92 code: props.comment.diff.trim(),
93 language: props.comment.fenceLanguage ?? language,
94 enabled: compactNativeRows.length === 0,
95 theme: appearanceScheme,
96 });
97 const nativeTokensJson = useMemo(
98 () =>
99 JSON.stringify(
100 Object.fromEntries([
101 ...addedRows.map((row, index) => [row.id, addedTokens?.[index] ?? []]),
102 ...deletedRows.map((row, index) => [row.id, deletedTokens?.[index] ?? []]),
103 ]),
104 ),
105 [addedRows, deletedRows, addedTokens, deletedTokens],
106 );
107 const nativeReviewDiffTheme = useMemo(
108 () => createNativeReviewDiffTheme(appearanceScheme, themeId, appTheme),
109 [appearanceScheme, appTheme, themeId],
110 );
111 const nativeRowsJson = useMemo(() => JSON.stringify(compactNativeRows), [compactNativeRows]);
112 const nativeThemeJson = useMemo(
113 () => JSON.stringify(nativeReviewDiffTheme),
114 [nativeReviewDiffTheme],
115 );
116 const nativeStyleJson = useMemo(
117 () => JSON.stringify({ ...nativeReviewDiffStyle, wordWrap: false }),
118 [nativeReviewDiffStyle],
119 );
120 const nativeDiffHeight = useMemo(
121 () =>
122 Math.min(
123 360,
124 Math.max(
125 48,
126 compactNativeRows.length * nativeReviewDiffStyle.rowHeight +
127 nativeReviewDiffStyle.fileHeaderVerticalMargin,
128 ),
129 ),
130 [compactNativeRows.length, nativeReviewDiffStyle],
131 );
132 const shouldRenderNativeDiff = NativeReviewDiffView != null && compactNativeRows.length > 0;
133
134 return (
135 <View
136 className="w-full overflow-hidden rounded-[16px] border border-continuous"
137 style={{
138 backgroundColor: props.colors.background,
139 borderColor: props.colors.border,
140 }}
141 >
142 <View
143 className="flex-row items-center gap-2 border-b px-3 py-2"
144 style={{ borderColor: props.colors.border }}
145 >
146 <View
147 className="size-6 items-center justify-center rounded-[7px] border-continuous"
148 style={{ backgroundColor: props.colors.mutedBackground }}
149 >
150 <SymbolView
151 name="doc.text"
152 size={13}
153 tintColor={props.colors.mutedText}
154 type="monochrome"
155 />
156 </View>
157 <View className="min-w-0 flex-1">
158 <Text
159 className="font-mono text-xs"
160 numberOfLines={1}
161 style={{ color: props.colors.text }}
162 >
163 {props.comment.filePath}
164 </Text>
165 <Text className="text-xs" style={{ color: props.colors.mutedText }}>
166 {props.comment.sectionTitle} · {props.comment.rangeLabel}
167 </Text>
168 </View>
169 </View>
170 {props.comment.text.length > 0 ? (
171 <View className="px-3 py-3">
172 <SelectableMarkdownText
173 markdown={props.comment.text}
174 textStyle={markdownStyles.nativeTextStyle}
175 />
176 </View>
177 ) : null}
178 {shouldRenderNativeDiff ? (
179 <View
180 className="border-t"
181 collapsable={false}
182 style={{
183 backgroundColor: nativeReviewDiffTheme.background,
184 borderColor: props.colors.border,
185 height: nativeDiffHeight,
186 }}
187 >
188 <NativeReviewDiffView
189 collapsable={false}
190 style={StyleSheet.absoluteFill}
191 appearanceScheme={appearanceScheme}
192 contentWidth={NATIVE_REVIEW_DIFF_CONTENT_WIDTH}
193 rowHeight={nativeReviewDiffStyle.rowHeight}
194 rowsJson={nativeRowsJson}
195 tokensJson={nativeTokensJson}
196 styleJson={nativeStyleJson}
197 themeJson={nativeThemeJson}
198 />
199 </View>
200 ) : props.comment.diff.trim().length > 0 ? (
201 <ScrollView
202 horizontal
203 nestedScrollEnabled
204 directionalLockEnabled
205 showsHorizontalScrollIndicator={false}
206 bounces={false}
207 className="border-t"
208 style={{ backgroundColor: props.colors.codeBackground, borderColor: props.colors.border }}
209 contentContainerStyle={{ padding: 10 }}
210 >
211 <NativeText
212 selectable
213 className="font-mono"
214 style={{
215 color: props.colors.text,
216 fontFamily: REVIEW_MONO_FONT_FAMILY,
217 fontSize: codeSurface.fontSize,
218 lineHeight: codeSurface.rowHeight,
219 }}
220 >
221 {snippetTokens
222 ? snippetTokens.map(
223 (line, lineIndex) => (
224 /* oxlint-disable react/no-array-index-key -- Highlight tokens are stateless positions within this immutable snippet. */ <NativeText
225 key={lineIndex}
226 >
227 {lineIndex > 0 ? "\n" : ""}
228 {line.map((token, tokenIndex) => (
229 <NativeText
230 key={tokenIndex}
231 style={{
232 color: token.color ?? props.colors.text,
233 fontStyle: (token.fontStyle ?? 0) & 1 ? "italic" : "normal",
234 fontWeight: (token.fontStyle ?? 0) & 2 ? "700" : "400",
235 }}
236 >
237 {token.content}
238 </NativeText>
239 ))}
240 </NativeText>
241 ) /* oxlint-enable react/no-array-index-key */,
242 )
243 : props.comment.diff.trim()}
244 </NativeText>
245 </ScrollView>
246 ) : null}
247 </View>
248 );
249});
250
251function buildReviewCommentPatch(comment: ReviewInlineComment): string {
252 if ((comment.fenceLanguage ?? "diff") !== "diff") {
253 return "";
254 }
255 const diff = comment.diff.trim();
256 if (!diff) {
257 return "";
258 }
259
260 if (diff.startsWith("diff --git ")) {
261 return diff;
262 }
263
264 const normalizedPath = comment.filePath.replaceAll("\\", "/");
265 return [
266 `diff --git a/${normalizedPath} b/${normalizedPath}`,
267 `--- a/${normalizedPath}`,
268 `+++ b/${normalizedPath}`,
269 diff,
270 ].join("\n");
271}