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

ReviewGuideExcerpt.tsx

141 lines5.2 KB

1import { useState } from "react";
2import { Pressable, View } from "react-native";
3import type { FileDiffMetadata } from "@pierre/diffs";
4import type { PullRequestReviewGuideExcerpt } from "@t3tools/contracts";
5import { excerptRows, fileDiffRows, rowsToPatch } from "@t3tools/client-runtime/review-guide-tour";
6import { AppText as Text } from "../../../components/ui/AppText";
7import { SymbolView } from "../../../components/ui/AppSymbol";
8import { resolveNativeReviewDiffView } from "../../diffs/nativeReviewDiffSurface";
9import { buildNativeReviewDiffData } from "../../review/nativeReviewDiffAdapter";
10import { buildReviewParsedDiff } from "../../review/reviewModel";
11import { useNativeReviewDiffBridge } from "../../review/useNativeReviewDiffBridge";
12import { useAppearanceCodeSurface } from "../../settings/appearance/useAppearanceCodeSurface";
13import { useAppearancePreferences } from "../../settings/appearance/AppearancePreferencesProvider";
14import { PullRequestMarkdown } from "../PullRequestMarkdown";
15​
16const EMPTY_IDS: readonly string[] = [];
17​
18export function ReviewGuideExcerpt({
19 excerpt,
20 file,
21 loading,
22 onOpen,
23}: {
24 readonly excerpt: PullRequestReviewGuideExcerpt;
25 readonly file: FileDiffMetadata | undefined;
26 readonly loading: boolean;
27 readonly onOpen: () => void;
28}) {
29 const { nativeReviewDiffStyle, codeWordBreak } = useAppearanceCodeSurface();
30 const { themeAppearance } = useAppearancePreferences();
31 const slice = excerptPatch(file, excerpt);
32 const data = (() => {
33 const parsed = buildNativeReviewDiffData(
34 buildReviewParsedDiff(slice, `guide:${excerpt.path}:${excerpt.start}`),
35 );
36 return {
37 ...parsed,
38 rows: parsed.rows.filter((row) => row.kind !== "file" && row.kind !== "hunk"),
39 };
40 })();
41 const bridge = useNativeReviewDiffBridge({
42 threadKey: null,
43 sectionId: `guide:${excerpt.path}:${excerpt.start}`,
44 diff: slice,
45 data,
46 collapsedFileIds: EMPTY_IDS,
47 viewedFileIds: EMPTY_IDS,
48 selectedRowIds: EMPTY_IDS,
49 canHighlight: true,
50 });
51 const [measuredHeight, setMeasuredHeight] = useState<{ key: string; height: number } | null>(
52 null,
53 );
54 const measurementKey = `${slice}:${codeWordBreak}:${nativeReviewDiffStyle.rowHeight}`;
55 const [NativeDiff] = useState(() => resolveNativeReviewDiffView());
56 return (
57 <View className="gap-3">
58 {excerpt.caption.trim() ? (
59 <PullRequestMarkdown lineBreaks={false} markdown={excerpt.caption} />
60 ) : null}
61 <View className="overflow-hidden rounded-lg border border-border-subtle">
62 <Pressable
63 accessibilityRole="button"
64 accessibilityLabel={`Open ${excerpt.path} at line ${excerpt.start} in full diff`}
65 onPress={onOpen}
66 className="min-h-11 flex-row items-center gap-3 border-b border-border-subtle px-3 py-2 active:opacity-60"
67 >
68 <Text
69 className="min-w-0 flex-1 text-xs text-foreground-muted"
70 numberOfLines={1}
71 ellipsizeMode="middle"
72 >
73 {excerpt.path}
74 </Text>
75 <Text className="text-2xs text-foreground-muted">
76 {excerpt.start}–{excerpt.end}
77 </Text>
78 <SymbolView name="arrow.up.right" size={13} tintColorClassName="accent-icon-muted" />
79 </Pressable>
80 {NativeDiff && data.rows.length ? (
81 <View
82 style={{
83 height:
84 measuredHeight?.key === measurementKey
85 ? measuredHeight.height
86 : data.rows.length * nativeReviewDiffStyle.rowHeight,
87 }}
88 >
89 <NativeDiff
90 scrollEnabled={false}
91 bounces={false}
92 appearanceScheme={themeAppearance}
93 rowHeight={nativeReviewDiffStyle.rowHeight}
94 contentWidth={2800}
95 style={{ flex: 1 }}
96 rowsJson={bridge.rowsJson}
97 themeJson={bridge.themeJson}
98 styleJson={bridge.styleJson}
99 tokensPatchJson={bridge.tokensPatchJson}
100 tokensResetKey={bridge.tokensResetKey}
101 onDebug={(event) => {
102 bridge.onDebug(event);
103 const { message, contentHeight } = event.nativeEvent;
104 if (message === "metrics" && typeof contentHeight === "number") {
105 setMeasuredHeight((current) =>
106 current?.key === measurementKey && current.height === contentHeight
107 ? current
108 : { key: measurementKey, height: contentHeight },
109 );
110 }
111 }}
112 onPressLine={onOpen}
113 />
114 </View>
115 ) : (
116 <Text className="p-3 text-xs text-foreground-muted">
117 {loading
118 ? `Loading ${excerpt.path}…`
119 : `${excerpt.path}:${excerpt.start}–${excerpt.end} is not in the loaded diff.`}
120 </Text>
121 )}
122 </View>
123 </View>
124 );
126​
127function excerptPatch(
128 file: FileDiffMetadata | undefined,
129 excerpt: PullRequestReviewGuideExcerpt,
130): string {
131 if (!file) return "";
132 try {
133 return rowsToPatch(
134 excerpt.path,
135 file.prevName ?? excerpt.path,
136 excerptRows(fileDiffRows(file), excerpt),
137 );
138 } catch {
139 return "";
140 }