Open History to see the last commit
1import { Toolbar } from "../../../components/ui/Toolbar";
2import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles";
3import type { FileDiffMetadata } from "@pierre/diffs";
4import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native";
5import { useEffect } from "react";
6import { Platform, Pressable, View } from "react-native";
7import { AndroidScreenHeader } from "../../../components/ui/AndroidScreenHeader";
8import { useSafeAreaInsets } from "react-native-safe-area-context";
9import {
10 letterStopNotes,
11 stepTourPosition,
12 stopFilePaths,
13} from "@t3tools/client-runtime/review-guide-tour";
14import { SymbolView } from "../../../components/ui/AppSymbol";
15import { GuideOutline } from "./GuideOutline";
16import { AppText as Text } from "../../../components/ui/AppText";
17import { ScreenScrollView } from "../../../components/ui/ScreenScrollView";
18import { Menu } from "../../../components/ui/Menu";
19import { IconButton } from "../../../components/ui/IconButton";
20import { useAppearancePreferences } from "../../settings/appearance/AppearancePreferencesProvider";
21import { NativeStackScreenOptions } from "../../../native/StackHeader";
22import { PullRequestCardGroup, PullRequestCardRow } from "../PullRequestControls";
23import { PullRequestMarkdown, PullRequestMarkdownRepository } from "../PullRequestMarkdown";
24import { usePullRequestDiff, type PullRequestParams } from "../usePullRequestDiff";
25import { ReviewGuideExcerpt } from "./ReviewGuideExcerpt";
26import { reviewGuideState } from "./reviewGuideState";
27import { GuideNotice } from "./GuideNotice";
28import Animated from "react-native-reanimated";
29import { PR_ROW_ENTER, PR_ROW_EXIT, PR_ROW_LAYOUT } from "../pullRequestAnimations";
30import { useEnvironmentQuery } from "../../../state/query";
31import { composerPullRequests } from "../../../state/pull-requests";
32import { usePullRequestPermissions } from "../usePullRequestPermissions";
33import { pullRequestTarget } from "../usePullRequestDiff";
34import { useReviewGuide } from "./useReviewGuide";
35
36export type GuideStopParams = PullRequestParams & {
37 readonly position: number;
38 readonly guideHeadSha: string;
39 readonly guideGeneratedAt: string;
40};
41
42const attentionLabels = {
43 tradeoff: "A tradeoff to weigh",
44 contract: "A contract to check",
45 product: "A product call",
46 risk: "A risk to judge",
47 accessibility: "An accessibility call",
48};
49
50export function PullRequestGuideContent({ params }: { readonly params: PullRequestParams }) {
51 const navigation = useNavigation();
52 const state = useReviewGuide(params);
53 const { guide, stale } = reviewGuideState(state.data);
54 return (
55 <Animated.View layout={PR_ROW_LAYOUT} className="w-full max-w-[760px] self-center">
56 {!state.enabled ? (
57 <Text>Review guides are disabled for this server.</Text>
58 ) : (
59 <>
60 <GuideNotice state={state} />
61 {guide ? (
62 <Animated.View
63 key={`outline:${guide.headSha}:${guide.generatedAt}`}
64 layout={PR_ROW_LAYOUT}
65 entering={PR_ROW_ENTER}
66 exiting={PR_ROW_EXIT}
67 >
68 <GuideOutline
69 guide={guide}
70 onOpen={(position) =>
71 navigation.dispatch(
72 StackActions.push("PullRequestGuideStop", {
73 ...params,
74 position,
75 guideHeadSha: guide.headSha,
76 guideGeneratedAt: guide.generatedAt,
77 }),
78 )
79 }
80 onRewrite={
81 state.canWrite && !state.running && !stale && !state.startError && !state.error
82 ? () => void state.write()
83 : undefined
84 }
85 />
86 </Animated.View>
87 ) : null}
88 </>
89 )}
90 </Animated.View>
91 );
92}
93
94export function PullRequestGuideStopScreen({ route }: StaticScreenProps<GuideStopParams>) {
95 const navigation = useNavigation();
96 const state = useReviewGuide(route.params);
97 const detail = useEnvironmentQuery(composerPullRequests.detail(pullRequestTarget(route.params)));
98 const permissions = usePullRequestPermissions(route.params, detail.data);
99 const canReview =
100 permissions.verdicts.length > 0 && detail.data?.headSha === route.params.guideHeadSha;
101 const { guide, stale } = reviewGuideState(state.data);
102 const position = Math.max(0, Math.min(route.params.position, (guide?.stops.length ?? 1) - 1));
103 const stop = guide?.stops[Math.max(0, position)];
104 const diff = usePullRequestDiff(route.params);
105 const excerptFiles = diffFilesByPath(diff.patch);
106 const wanted = stop ? stopFilePaths(stop) : [];
107 const loadedPaths = new Set(
108 diff.parsed.kind === "files" ? diff.parsed.files.map((file) => file.path) : [],
109 );
110 const missing = wanted.some((path) => !loadedPaths.has(path));
111 const { error: diffError, loadMore } = diff;
112 useEffect(() => {
113 if (state.enabled && missing && !diffError) loadMore();
114 }, [state.enabled, missing, diffError, loadMore]);
115 const insets = useSafeAreaInsets();
116 const changedGuide =
117 guide !== null &&
118 (guide.headSha !== route.params.guideHeadSha ||
119 guide.generatedAt !== route.params.guideGeneratedAt);
120 const { appearance, setCodeWordBreak } = useAppearancePreferences();
121 const notes = stop ? letterStopNotes(stop).lettered : [];
122 return (
123 <View className="flex-1 bg-screen">
124 <NativeStackScreenOptions
125 options={{
126 title: `Stop ${route.params.position + 1}`,
127 headerTitle: `Stop ${route.params.position + 1} of ${guide?.stops.length ?? "…"}`,
128 }}
129 />
130 {Platform.OS === "android" ? (
131 <AndroidScreenHeader
132 title={`Stop ${route.params.position + 1}`}
133 onBack={() => navigation.goBack()}
134 trailing={
135 <Menu
136 actions={[
137 {
138 id: "word-wrap",
139 title: "Word wrap",
140 image: "text.alignleft",
141 state: appearance.codeWordBreak ? "on" : "off",
142 },
143 ]}
144 onPressAction={() => setCodeWordBreak(!appearance.codeWordBreak)}
145 >
146 <View pointerEvents="none">
147 <IconButton icon="ellipsis" accessibilityLabel="Guide options" />
148 </View>
149 </Menu>
150 }
151 />
152 ) : null}
153 <Toolbar placement="right">
154 <Toolbar.Menu title="Guide options" accessibilityLabel="Guide options" icon="ellipsis">
155 <Toolbar.MenuAction
156 icon="text.alignleft"
157 isOn={appearance.codeWordBreak}
158 onPress={() => setCodeWordBreak(!appearance.codeWordBreak)}
159 >
160 <Toolbar.Label>Word wrap</Toolbar.Label>
161 </Toolbar.MenuAction>
162 </Toolbar.Menu>
163 </Toolbar>
164 <PullRequestMarkdownRepository
165 params={route.params}
166 value={`https://${route.params.host ?? "github.com"}/${route.params.repository}`}
167 >
168 <ScreenScrollView
169 contentInsetAdjustmentBehavior="automatic"
170 contentContainerStyle={{ padding: 16, paddingBottom: insets.bottom + 24, gap: 20 }}
171 >
172 <View className="w-full max-w-[760px] self-center gap-5">
173 {!state.enabled ? (
174 <Text>Review guides are disabled for this server.</Text>
175 ) : changedGuide ? (
176 <Text>This guide was rewritten. Go back to start the new tour.</Text>
177 ) : !stop ? (
178 <Text>{state.error ?? "Loading stop…"}</Text>
179 ) : (
180 <>
181 <GuideNotice state={state} />
182 <Text className="text-2xl leading-8 font-t3-semibold">{stop.title}</Text>
183 <PullRequestMarkdown markdown={stop.summary} />
184 {stop.attention ? (
185 <View className="gap-3 rounded-xl border border-border bg-grouped-card p-4">
186 <View className="flex-row items-center gap-2">
187 <SymbolView name="eye" size={17} tintColorClassName="accent-icon-muted" />
188 <Text className="text-sm font-t3-semibold">
189 {attentionLabels[stop.attention.kind]}
190 </Text>
191 </View>
192 <PullRequestMarkdown markdown={stop.attention.note} />
193 </View>
194 ) : null}
195 {stale ? (
196 <Text className="text-sm text-foreground-muted">
197 Excerpts are hidden until the guide is rewritten for the current head.
198 </Text>
199 ) : (
200 stop.excerpts.map((excerpt) => (
201 <ReviewGuideExcerpt
202 key={`${guide?.headSha}:${JSON.stringify(excerpt)}`}
203 onOpen={() =>
204 navigation.dispatch(
205 StackActions.push("PullRequestFile", {
206 ...route.params,
207 path: excerpt.path,
208 focus: { side: excerpt.side, start: excerpt.start, end: excerpt.end },
209 fileSelectionId: `guide:${position}:${excerpt.path}:${excerpt.start}`,
210 }),
211 )
212 }
213 excerpt={excerpt}
214 file={excerptFiles.get(excerpt.path)}
215 loading={diff.isPending || (missing && diff.nextCursor !== null)}
216 />
217 ))
218 )}
219 {diff.error ? <Text className="text-danger-foreground">{diff.error}</Text> : null}
220 {notes.length ? (
221 <View className="gap-4">
222 <Text className="font-t3-semibold">Notes on this stop</Text>
223 {notes.map(({ marker, note }) => (
224 <View key={marker} className="flex-row gap-3">
225 <View className="h-7 w-7 items-center justify-center rounded-lg bg-grouped-card">
226 <Text className="text-xs font-t3-semibold">{marker}</Text>
227 </View>
228 <View className="min-w-0 flex-1 gap-2">
229 <Text className="font-t3-semibold">{note.title}</Text>
230 <Text className="text-xs text-foreground-muted">
231 {note.path.split("/").at(-1)}:{note.line}
232 </Text>
233 <PullRequestMarkdown markdown={note.body} />
234 </View>
235 </View>
236 ))}
237 </View>
238 ) : null}
239 {stop.files.length ? (
240 <PullRequestCardGroup>
241 {stop.files.map((file, index) => (
242 <PullRequestCardRow
243 key={file.path}
244 title={file.path}
245 detail={file.note}
246 icon="doc"
247 last={index === stop.files.length - 1}
248 onPress={() =>
249 navigation.navigate("PullRequestFile", {
250 ...route.params,
251 path: file.path,
252 })
253 }
254 />
255 ))}
256 </PullRequestCardGroup>
257 ) : null}
258 {stop.questions.length ? (
259 <View className="gap-3">
260 <Text className="font-t3-semibold">Questions to consider</Text>
261 {stop.questions.map((question) => (
262 <PullRequestMarkdown key={question} markdown={question} />
263 ))}
264 </View>
265 ) : null}
266 <Pressable
267 accessibilityRole="button"
268 accessibilityLabel={
269 guide && position < guide.stops.length - 1
270 ? "Next stop"
271 : canReview
272 ? "Finish review"
273 : "Finish tour"
274 }
275 className="mt-2 flex-row items-center gap-4 border-t border-border-subtle pt-5 pb-2 active:opacity-60"
276 onPress={() =>
277 guide && position < guide.stops.length - 1
278 ? navigation.dispatch(
279 StackActions.push("PullRequestGuideStop", {
280 ...route.params,
281 position: stepTourPosition(position, 1, guide.stops.length),
282 }),
283 )
284 : canReview
285 ? navigation.navigate("PullRequestReview", {
286 ...route.params,
287 mode: "review",
288 })
289 : navigation.dispatch(StackActions.popTo("PullRequest", route.params))
290 }
291 >
292 <View className="min-w-0 flex-1 gap-1">
293 <Text className="text-xs text-foreground-muted">
294 {guide && position < guide.stops.length - 1
295 ? `Next · ${position + 2} of ${guide.stops.length}`
296 : "End of guide"}
297 </Text>
298 <Text className="text-base font-t3-semibold">
299 {guide && position < guide.stops.length - 1
300 ? guide.stops[position + 1]?.title
301 : canReview
302 ? "Finish review"
303 : "Finish tour"}
304 </Text>
305 </View>
306 <View className="h-11 w-11 items-center justify-center rounded-full bg-foreground">
307 <SymbolView
308 name={
309 guide && position < guide.stops.length - 1 ? "arrow.right" : "checkmark"
310 }
311 size={20}
312 tintColorClassName="accent-screen"
313 />
314 </View>
315 </Pressable>
316 </>
317 )}
318 </View>
319 </ScreenScrollView>
320 </PullRequestMarkdownRepository>
321 </View>
322 );
323}
324
325function diffFilesByPath(patchText: string): Map<string, FileDiffMetadata> {
326 const files = new Map<string, FileDiffMetadata>();
327 try {
328 for (const patch of parsePatchFiles(patchText)) {
329 for (const file of patch.files) {
330 files.set(file.name, file);
331 if (file.prevName) files.set(file.prevName, file);
332 }
333 }
334 } catch {}
335 return files;
336}