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