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

ReviewGuideExcerpt.tsx

144 lines5.4 KB

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