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

ReviewGuideExcerpt.tsx

139 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() ? <PullRequestMarkdown markdown={excerpt.caption} /> : null}
59 <View className="overflow-hidden rounded-lg border border-border-subtle">
60 <Pressable
61 accessibilityRole="button"
62 accessibilityLabel={`Open ${excerpt.path} at line ${excerpt.start} in full diff`}
63 onPress={onOpen}
64 className="min-h-11 flex-row items-center gap-3 border-b border-border-subtle px-3 py-2 active:opacity-60"
65 >
66 <Text
67 className="min-w-0 flex-1 text-xs text-foreground-muted"
68 numberOfLines={1}
69 ellipsizeMode="middle"
70 >
71 {excerpt.path}
72 </Text>
73 <Text className="text-2xs text-foreground-muted">
74 {excerpt.start}–{excerpt.end}
75 </Text>
76 <SymbolView name="arrow.up.right" size={13} tintColorClassName="accent-icon-muted" />
77 </Pressable>
78 {NativeDiff && data.rows.length ? (
79 <View
80 style={{
81 height:
82 measuredHeight?.key === measurementKey
83 ? measuredHeight.height
84 : data.rows.length * nativeReviewDiffStyle.rowHeight,
85 }}
86 >
87 <NativeDiff
88 scrollEnabled={false}
89 bounces={false}
90 appearanceScheme={themeAppearance}
91 rowHeight={nativeReviewDiffStyle.rowHeight}
92 contentWidth={2800}
93 style={{ flex: 1 }}
94 rowsJson={bridge.rowsJson}
95 themeJson={bridge.themeJson}
96 styleJson={bridge.styleJson}
97 tokensPatchJson={bridge.tokensPatchJson}
98 tokensResetKey={bridge.tokensResetKey}
99 onDebug={(event) => {
100 bridge.onDebug(event);
101 const { message, contentHeight } = event.nativeEvent;
102 if (message === "metrics" && typeof contentHeight === "number") {
103 setMeasuredHeight((current) =>
104 current?.key === measurementKey && current.height === contentHeight
105 ? current
106 : { key: measurementKey, height: contentHeight },
107 );
108 }
109 }}
110 onPressLine={onOpen}
111 />
112 </View>
113 ) : (
114 <Text className="p-3 text-xs text-foreground-muted">
115 {loading
116 ? `Loading ${excerpt.path}…`
117 : `${excerpt.path}:${excerpt.start}–${excerpt.end} is not in the loaded diff.`}
118 </Text>
119 )}
120 </View>
121 </View>
122 );
124​
125function excerptPatch(
126 file: FileDiffMetadata | undefined,
127 excerpt: PullRequestReviewGuideExcerpt,
128): string {
129 if (!file) return "";
130 try {
131 return rowsToPatch(
132 excerpt.path,
133 file.prevName ?? excerpt.path,
134 excerptRows(fileDiffRows(file), excerpt),
135 );
136 } catch {
137 return "";
138 }