Open History to see the last commit
1import { useMessages } from "gt-react-native";
2import { memo, useMemo, useState } from "react";
3import { getFiletypeFromFileName } from "@pierre/diffs/utils/getFiletypeFromFileName";
4import { ScrollView, StyleSheet, Text as NativeText, View, type ColorValue } from "react-native";
5import { SelectableMarkdownText } from "../../native/SelectableMarkdownText";
6import { useMarkdownPreviewStyles } from "../files/FileMarkdownPreview";
7import { AppText as Text } from "../../components/ui/AppText";
8import { SymbolView } from "../../components/ui/AppSymbol";
9import { useUniwindTheme } from "../../lib/useUniwindTheme";
10import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
11import { useAppearanceCodeSurface } from "../settings/appearance/useAppearanceCodeSurface";
12import { resolveNativeReviewDiffView } from "../diffs/nativeReviewDiffSurface";
13import {
14 buildNativeReviewDiffData,
15 buildNativeReviewSnippetRows,
16 buildReviewCommentPatch,
17 createNativeReviewDiffTheme,
18 NATIVE_REVIEW_DIFF_CONTENT_WIDTH,
19 reviewCommentDiffText,
20} from "./nativeReviewDiffAdapter";
21import { buildReviewParsedDiff } from "./reviewModel";
22import type { ReviewInlineComment } from "./reviewCommentSelection";
23import { useMarkdownCodeHighlight } from "../threads/markdownCodeHighlightState";
24
25export interface ReviewCommentColors {
26 readonly background: ColorValue;
27 readonly border: ColorValue;
28 readonly mutedBackground: ColorValue;
29 readonly text: ColorValue;
30 readonly mutedText: ColorValue;
31 readonly codeBackground: ColorValue;
32}
33
34export function useReviewCommentColors(): ReviewCommentColors {
35 const theme = useUniwindTheme();
36
37 return {
38 background: theme["--color-card"],
39 border: theme["--color-border"],
40 mutedBackground: theme["--color-subtle"],
41 text: theme["--color-foreground"],
42 mutedText: theme["--color-foreground-muted"],
43 codeBackground: theme["--color-md-code-bg"],
44 };
45}
46
47export const ReviewCommentCard = memo(function ReviewCommentCard(props: {
48 readonly comment: ReviewInlineComment;
49 readonly colors: ReviewCommentColors;
50}) {
51 const { codeSurface, nativeReviewDiffStyle } = useAppearanceCodeSurface();
52 const messages = useMessages();
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}`, messages),
60 [patch, props.comment.id, messages],
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: reviewCommentDiffText(props.comment.diff),
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 = JSON.stringify({ ...nativeReviewDiffStyle, wordWrap: false });
117 const nativeDiffHeight = useMemo(
118 () =>
119 Math.min(
120 360,
121 Math.max(
122 48,
123 compactNativeRows.length * nativeReviewDiffStyle.rowHeight +
124 nativeReviewDiffStyle.fileHeaderVerticalMargin,
125 ),
126 ),
127 [compactNativeRows.length, nativeReviewDiffStyle],
128 );
129 const shouldRenderNativeDiff = NativeReviewDiffView != null && compactNativeRows.length > 0;
130
131 return (
132 <View
133 className="w-full overflow-hidden rounded-[16px] border border-continuous"
134 style={{
135 backgroundColor: props.colors.background,
136 borderColor: props.colors.border,
137 }}
138 >
139 <View
140 className="flex-row items-center gap-2 border-b px-3 py-2"
141 style={{ borderColor: props.colors.border }}
142 >
143 <View
144 className="size-6 items-center justify-center rounded-[7px] border-continuous"
145 style={{ backgroundColor: props.colors.mutedBackground }}
146 >
147 <SymbolView
148 name="doc.text"
149 size={13}
150 tintColor={props.colors.mutedText}
151 type="monochrome"
152 />
153 </View>
154 <View className="min-w-0 flex-1">
155 <Text
156 className="font-mono text-xs"
157 numberOfLines={1}
158 style={{ color: props.colors.text }}
159 >
160 {props.comment.filePath}
161 </Text>
162 <Text className="text-xs" style={{ color: props.colors.mutedText }}>
163 {props.comment.sectionTitle} · {props.comment.rangeLabel}
164 </Text>
165 </View>
166 </View>
167 {props.comment.text.length > 0 ? (
168 <View className="px-3 py-3">
169 <SelectableMarkdownText
170 markdown={props.comment.text}
171 textStyle={markdownStyles.nativeTextStyle}
172 />
173 </View>
174 ) : null}
175 {shouldRenderNativeDiff ? (
176 <View
177 className="border-t"
178 collapsable={false}
179 style={{
180 backgroundColor: nativeReviewDiffTheme.background,
181 borderColor: props.colors.border,
182 height: nativeDiffHeight,
183 }}
184 >
185 <NativeReviewDiffView
186 collapsable={false}
187 style={StyleSheet.absoluteFill}
188 appearanceScheme={appearanceScheme}
189 contentWidth={NATIVE_REVIEW_DIFF_CONTENT_WIDTH}
190 rowHeight={nativeReviewDiffStyle.rowHeight}
191 rowsJson={nativeRowsJson}
192 tokensJson={nativeTokensJson}
193 styleJson={nativeStyleJson}
194 themeJson={nativeThemeJson}
195 />
196 </View>
197 ) : props.comment.diff.trim().length > 0 ? (
198 <ScrollView
199 horizontal
200 nestedScrollEnabled
201 directionalLockEnabled
202 showsHorizontalScrollIndicator={false}
203 bounces={false}
204 className="border-t"
205 style={{ backgroundColor: props.colors.codeBackground, borderColor: props.colors.border }}
206 contentContainerStyle={{ padding: 10 }}
207 >
208 <NativeText
209 selectable
210 className="font-mono"
211 style={{
212 color: props.colors.text,
213 fontSize: codeSurface.fontSize,
214 lineHeight: codeSurface.rowHeight,
215 }}
216 >
217 {snippetTokens
218 ? snippetTokens.map(
219 (line, lineIndex) => (
220 /* oxlint-disable react/no-array-index-key -- Highlight tokens are stateless positions within this immutable snippet. */ <NativeText
221 key={lineIndex}
222 >
223 {lineIndex > 0 ? "\n" : ""}
224 {line.map((token, tokenIndex) => (
225 <NativeText
226 key={tokenIndex}
227 style={{
228 color: token.color ?? props.colors.text,
229 fontStyle: (token.fontStyle ?? 0) & 1 ? "italic" : "normal",
230 fontWeight: (token.fontStyle ?? 0) & 2 ? "700" : "400",
231 }}
232 >
233 {token.content}
234 </NativeText>
235 ))}
236 </NativeText>
237 ) /* oxlint-enable react/no-array-index-key */,
238 )
239 : reviewCommentDiffText(props.comment.diff)}
240 </NativeText>
241 </ScrollView>
242 ) : null}
243 </View>
244 );
245});