The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 149 lines 5.46 KB
Raw
1 import { useGT, useMessages } from "gt-react-native";
2 import { useState } from "react";
3 import { Pressable, View } from "react-native";
4 import type { FileDiffMetadata } from "@pierre/diffs";
5 import type { PullRequestReviewGuideExcerpt } from "@t3tools/contracts";
6 import { excerptRows, fileDiffRows, rowsToPatch } from "@t3tools/client-runtime/review-guide-tour";
7 import { AppText as Text } from "../../../components/ui/AppText";
8 import { SymbolView } from "../../../components/ui/AppSymbol";
9 import { resolveNativeReviewDiffView } from "../../diffs/nativeReviewDiffSurface";
10 import { buildNativeReviewDiffData } from "../../review/nativeReviewDiffAdapter";
11 import { buildReviewParsedDiff } from "../../review/reviewModel";
12 import { useNativeReviewDiffBridge } from "../../review/useNativeReviewDiffBridge";
13 import { useAppearanceCodeSurface } from "../../settings/appearance/useAppearanceCodeSurface";
14 import { useAppearancePreferences } from "../../settings/appearance/AppearancePreferencesProvider";
15 import { PullRequestMarkdown } from "../PullRequestMarkdown";
16
17 const EMPTY_IDS: readonly string[] = [];
18
19 export 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} at line {line} in full diff", {
68 path: excerpt.path,
69 line: excerpt.start,
70 })}
71 onPress={onOpen}
72 className="min-h-11 flex-row items-center gap-3 border-b border-border-subtle px-3 py-2 active:opacity-60"
73 >
74 <Text
75 className="min-w-0 flex-1 text-xs text-foreground-muted"
76 numberOfLines={1}
77 ellipsizeMode="middle"
78 >
79 {excerpt.path}
80 </Text>
81 <Text className="text-2xs text-foreground-muted">
82 {excerpt.start}–{excerpt.end}
83 </Text>
84 <SymbolView name="arrow.up.right" size={13} tintColorClassName="accent-icon-muted" />
85 </Pressable>
86 {NativeDiff && data.rows.length ? (
87 <View
88 style={{
89 height:
90 measuredHeight?.key === measurementKey
91 ? measuredHeight.height
92 : data.rows.length * nativeReviewDiffStyle.rowHeight,
93 }}
94 >
95 <NativeDiff
96 scrollEnabled={false}
97 bounces={false}
98 appearanceScheme={themeAppearance}
99 rowHeight={nativeReviewDiffStyle.rowHeight}
100 contentWidth={2800}
101 style={{ flex: 1 }}
102 rowsJson={bridge.rowsJson}
103 themeJson={bridge.themeJson}
104 styleJson={bridge.styleJson}
105 tokensPatchJson={bridge.tokensPatchJson}
106 tokensResetKey={bridge.tokensResetKey}
107 onDebug={(event) => {
108 bridge.onDebug(event);
109 const { message, contentHeight } = event.nativeEvent;
110 if (message === "metrics" && typeof contentHeight === "number") {
111 setMeasuredHeight((current) =>
112 current?.key === measurementKey && current.height === contentHeight
113 ? current
114 : { key: measurementKey, height: contentHeight },
115 );
116 }
117 }}
118 onPressLine={onOpen}
119 />
120 </View>
121 ) : (
122 <Text className="p-3 text-xs text-foreground-muted">
123 {loading
124 ? gt("Loading {path}…", { path: excerpt.path })
125 : gt("{location} is not in the loaded diff.", {
126 location: `${excerpt.path}:${excerpt.start}–${excerpt.end}`,
127 })}
128 </Text>
129 )}
130 </View>
131 </View>
132 );
133 }
134
135 function excerptPatch(
136 file: FileDiffMetadata | undefined,
137 excerpt: PullRequestReviewGuideExcerpt,
138 ): string {
139 if (!file) return "";
140 try {
141 return rowsToPatch(
142 excerpt.path,
143 file.prevName ?? excerpt.path,
144 excerptRows(fileDiffRows(file), excerpt),
145 );
146 } catch {
147 return "";
148 }
149 }