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

ReviewCommentCard.tsx

263 lines9.4 KB

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 { AppText as Text } from "../../components/ui/AppText";
5import { SymbolView } from "../../components/ui/AppSymbol";
6import { useUniwindTheme } from "../../lib/useUniwindTheme";
7import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
8import { useAppearanceCodeSurface } from "../settings/appearance/useAppearanceCodeSurface";
9import { resolveNativeReviewDiffView } from "../diffs/nativeReviewDiffSurface";
10import {
11 buildNativeReviewDiffData,
12 buildNativeReviewSnippetRows,
13 createNativeReviewDiffTheme,
14 NATIVE_REVIEW_DIFF_CONTENT_WIDTH,
15} from "./nativeReviewDiffAdapter";
16import { buildReviewParsedDiff } from "./reviewModel";
17import { REVIEW_MONO_FONT_FAMILY } from "./reviewDiffRendering";
18import type { ReviewInlineComment } from "./reviewCommentSelection";
19import { useMarkdownCodeHighlight } from "../threads/markdownCodeHighlightState";
20​
21export interface ReviewCommentColors {
22 readonly background: ColorValue;
23 readonly border: ColorValue;
24 readonly mutedBackground: ColorValue;
25 readonly text: ColorValue;
26 readonly mutedText: ColorValue;
27 readonly codeBackground: ColorValue;
29​
30export function useReviewCommentColors(): ReviewCommentColors {
31 const theme = useUniwindTheme();
32​
33 return useMemo(
34 () => ({
35 background: theme["--color-card"],
36 border: theme["--color-border"],
37 mutedBackground: theme["--color-subtle"],
38 text: theme["--color-foreground"],
39 mutedText: theme["--color-foreground-muted"],
40 codeBackground: theme["--color-md-code-bg"],
41 }),
42 [theme],
43 );
45​
46export const ReviewCommentCard = memo(function ReviewCommentCard(props: {
47 readonly comment: ReviewInlineComment;
48 readonly colors: ReviewCommentColors;
49}) {
50 const { codeSurface, nativeReviewDiffStyle } = useAppearanceCodeSurface();
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 = useMemo(
114 () => JSON.stringify({ ...nativeReviewDiffStyle, wordWrap: false }),
115 [nativeReviewDiffStyle],
116 );
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 <Text selectable className="text-base leading-snug" style={{ color: props.colors.text }}>
170 {props.comment.text}
171 </Text>
172 </View>
173 ) : null}
174 {shouldRenderNativeDiff ? (
175 <View
176 className="border-t"
177 collapsable={false}
178 style={{
179 backgroundColor: nativeReviewDiffTheme.background,
180 borderColor: props.colors.border,
181 height: nativeDiffHeight,
182 }}
183 >
184 <NativeReviewDiffView
185 collapsable={false}
186 style={StyleSheet.absoluteFill}
187 appearanceScheme={appearanceScheme}
188 contentWidth={NATIVE_REVIEW_DIFF_CONTENT_WIDTH}
189 rowHeight={nativeReviewDiffStyle.rowHeight}
190 rowsJson={nativeRowsJson}
191 tokensJson={nativeTokensJson}
192 styleJson={nativeStyleJson}
193 themeJson={nativeThemeJson}
194 />
195 </View>
196 ) : props.comment.diff.trim().length > 0 ? (
197 <ScrollView
198 horizontal
199 nestedScrollEnabled
200 directionalLockEnabled
201 showsHorizontalScrollIndicator={false}
202 bounces={false}
203 className="border-t"
204 style={{ backgroundColor: props.colors.codeBackground, borderColor: props.colors.border }}
205 contentContainerStyle={{ padding: 10 }}
206 >
207 <NativeText
208 selectable
209 className="font-mono"
210 style={{
211 color: props.colors.text,
212 fontFamily: REVIEW_MONO_FONT_FAMILY,
213 fontSize: codeSurface.fontSize,
214 lineHeight: codeSurface.rowHeight,
215 }}
216 >
217 {snippetTokens
218 ? snippetTokens.map((line, lineIndex) => (
219 <NativeText key={lineIndex}>
220 {lineIndex > 0 ? "\n" : ""}
221 {line.map((token, tokenIndex) => (
222 <NativeText
223 key={tokenIndex}
224 style={{
225 color: token.color ?? props.colors.text,
226 fontStyle: (token.fontStyle ?? 0) & 1 ? "italic" : "normal",
227 fontWeight: (token.fontStyle ?? 0) & 2 ? "700" : "400",
228 }}
229 >
230 {token.content}
231 </NativeText>
232 ))}
233 </NativeText>
234 ))
235 : props.comment.diff.trim()}
236 </NativeText>
237 </ScrollView>
238 ) : null}
239 </View>
240 );
241});
242​
243function buildReviewCommentPatch(comment: ReviewInlineComment): string {
244 if ((comment.fenceLanguage ?? "diff") !== "diff") {
245 return "";
246 }
247 const diff = comment.diff.trim();
248 if (!diff) {
249 return "";
250 }
251​
252 if (diff.startsWith("diff --git ")) {
253 return diff;
254 }
255​
256 const normalizedPath = comment.filePath.replaceAll("\\", "/");
257 return [
258 `diff --git a/${normalizedPath} b/${normalizedPath}`,
259 `--- a/${normalizedPath}`,
260 `+++ b/${normalizedPath}`,
261 diff,
262 ].join("\n");