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, useMemo } 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 = useMemo(() => {
106 const files = new Map<string, FileDiffMetadata>();
107 try {
108 for (const patch of parsePatchFiles(diff.patch)) {
109 for (const file of patch.files) {
110 files.set(file.name, file);
111 if (file.prevName) files.set(file.prevName, file);
112 }
113 }
114 } catch {}
115 return files;
116 }, [diff.patch]);
117 const wanted = useMemo(() => (stop ? stopFilePaths(stop) : []), [stop]);
118 const loadedPaths = new Set(
119 diff.parsed.kind === "files" ? diff.parsed.files.map((file) => file.path) : [],
120 );
121 const missing = wanted.some((path) => !loadedPaths.has(path));
122 const { error: diffError, loadMore } = diff;
123 useEffect(() => {
124 if (state.enabled && missing && !diffError) loadMore();
125 }, [state.enabled, missing, diffError, loadMore]);
126 const insets = useSafeAreaInsets();
127 const changedGuide =
128 guide !== null &&
129 (guide.headSha !== route.params.guideHeadSha ||
130 guide.generatedAt !== route.params.guideGeneratedAt);
131 const { appearance, setCodeWordBreak } = useAppearancePreferences();
132 const notes = stop ? letterStopNotes(stop).lettered : [];
133 return (
134 <View className="flex-1 bg-screen">
135 <NativeStackScreenOptions
136 options={{
137 title: `Stop ${route.params.position + 1}`,
138 headerTitle: `Stop ${route.params.position + 1} of ${guide?.stops.length ?? "…"}`,
139 }}
140 />
141 {Platform.OS === "android" ? (
142 <AndroidScreenHeader
143 title={`Stop ${route.params.position + 1}`}
144 onBack={() => navigation.goBack()}
145 trailing={
146 <Menu
147 actions={[
148 {
149 id: "word-wrap",
150 title: "Word wrap",
151 image: "text.alignleft",
152 state: appearance.codeWordBreak ? "on" : "off",
153 },
154 ]}
155 onPressAction={() => setCodeWordBreak(!appearance.codeWordBreak)}
156 >
157 <View pointerEvents="none">
158 <IconButton icon="ellipsis" accessibilityLabel="Guide options" />
159 </View>
160 </Menu>
161 }
162 />
163 ) : null}
164 <Toolbar placement="right">
165 <Toolbar.Menu title="Guide options" accessibilityLabel="Guide options" icon="ellipsis">
166 <Toolbar.MenuAction
167 icon="text.alignleft"
168 isOn={appearance.codeWordBreak}
169 onPress={() => setCodeWordBreak(!appearance.codeWordBreak)}
170 >
171 <Toolbar.Label>Word wrap</Toolbar.Label>
172 </Toolbar.MenuAction>
173 </Toolbar.Menu>
174 </Toolbar>
175 <PullRequestMarkdownRepository
176 params={route.params}
177 value={`https://${route.params.host ?? "github.com"}/${route.params.repository}`}
178 >
179 <ScreenScrollView
180 contentInsetAdjustmentBehavior="automatic"
181 contentContainerStyle={{ padding: 16, paddingBottom: insets.bottom + 24, gap: 20 }}
182 >
183 <View className="w-full max-w-[760px] self-center gap-5">
184 {!state.enabled ? (
185 <Text>Review guides are disabled for this server.</Text>
186 ) : changedGuide ? (
187 <Text>This guide was rewritten. Go back to start the new tour.</Text>
188 ) : !stop ? (
189 <Text>{state.error ?? "Loading stop…"}</Text>
190 ) : (
191 <>
192 <GuideNotice state={state} />
193 <Text className="text-2xl leading-8 font-t3-semibold">{stop.title}</Text>
194 <PullRequestMarkdown markdown={stop.summary} />
195 {stop.attention ? (
196 <View className="gap-3 rounded-xl border border-border bg-grouped-card p-4">
197 <View className="flex-row items-center gap-2">
198 <SymbolView name="eye" size={17} tintColorClassName="accent-icon-muted" />
199 <Text className="text-sm font-t3-semibold">
200 {attentionLabels[stop.attention.kind]}
201 </Text>
202 </View>
203 <PullRequestMarkdown markdown={stop.attention.note} />
204 </View>
205 ) : null}
206 {stale ? (
207 <Text className="text-sm text-foreground-muted">
208 Excerpts are hidden until the guide is rewritten for the current head.
209 </Text>
210 ) : (
211 stop.excerpts.map((excerpt) => (
212 <ReviewGuideExcerpt
213 key={`${guide?.headSha}:${JSON.stringify(excerpt)}`}
214 onOpen={() =>
215 navigation.dispatch(
216 StackActions.push("PullRequestFile", {
217 ...route.params,
218 path: excerpt.path,
219 focus: { side: excerpt.side, start: excerpt.start, end: excerpt.end },
220 fileSelectionId: `guide:${position}:${excerpt.path}:${excerpt.start}`,
221 }),
222 )
223 }
224 excerpt={excerpt}
225 file={excerptFiles.get(excerpt.path)}
226 loading={diff.isPending || (missing && diff.nextCursor !== null)}
227 />
228 ))
229 )}
230 {diff.error ? <Text className="text-danger-foreground">{diff.error}</Text> : null}
231 {notes.length ? (
232 <View className="gap-4">
233 <Text className="font-t3-semibold">Notes on this stop</Text>
234 {notes.map(({ marker, note }) => (
235 <View key={marker} className="flex-row gap-3">
236 <View className="h-7 w-7 items-center justify-center rounded-lg bg-grouped-card">
237 <Text className="text-xs font-t3-semibold">{marker}</Text>
238 </View>
239 <View className="min-w-0 flex-1 gap-2">
240 <Text className="font-t3-semibold">{note.title}</Text>
241 <Text className="text-xs text-foreground-muted">
242 {note.path.split("/").at(-1)}:{note.line}
243 </Text>
244 <PullRequestMarkdown markdown={note.body} />
245 </View>
246 </View>
247 ))}
248 </View>
249 ) : null}
250 {stop.files.length ? (
251 <PullRequestCardGroup>
252 {stop.files.map((file, index) => (
253 <PullRequestCardRow
254 key={file.path}
255 title={file.path}
256 detail={file.note}
257 icon="doc"
258 last={index === stop.files.length - 1}
259 onPress={() =>
260 navigation.navigate("PullRequestFile", {
261 ...route.params,
262 path: file.path,
263 })
264 }
265 />
266 ))}
267 </PullRequestCardGroup>
268 ) : null}
269 {stop.questions.length ? (
270 <View className="gap-3">
271 <Text className="font-t3-semibold">Questions to consider</Text>
272 {stop.questions.map((question) => (
273 <PullRequestMarkdown key={question} markdown={question} />
274 ))}
275 </View>
276 ) : null}
277 <Pressable
278 accessibilityRole="button"
279 accessibilityLabel={
280 guide && position < guide.stops.length - 1
281 ? "Next stop"
282 : canReview
283 ? "Finish review"
284 : "Finish tour"
285 }
286 className="mt-2 flex-row items-center gap-4 border-t border-border-subtle pt-5 pb-2 active:opacity-60"
287 onPress={() =>
288 guide && position < guide.stops.length - 1
289 ? navigation.dispatch(
290 StackActions.push("PullRequestGuideStop", {
291 ...route.params,
292 position: stepTourPosition(position, 1, guide.stops.length),
293 }),
294 )
295 : canReview
296 ? navigation.navigate("PullRequestReview", {
297 ...route.params,
298 mode: "review",
299 })
300 : navigation.dispatch(StackActions.popTo("PullRequest", route.params))
301 }
302 >
303 <View className="min-w-0 flex-1 gap-1">
304 <Text className="text-xs text-foreground-muted">
305 {guide && position < guide.stops.length - 1
306 ? `Next · ${position + 2} of ${guide.stops.length}`
307 : "End of guide"}
308 </Text>
309 <Text className="text-base font-t3-semibold">
310 {guide && position < guide.stops.length - 1
311 ? guide.stops[position + 1]?.title
312 : canReview
313 ? "Finish review"
314 : "Finish tour"}
315 </Text>
316 </View>
317 <View className="h-11 w-11 items-center justify-center rounded-full bg-foreground">
318 <SymbolView
319 name={
320 guide && position < guide.stops.length - 1 ? "arrow.right" : "checkmark"
321 }
322 size={20}
323 tintColorClassName="accent-screen"
324 />
325 </View>
326 </Pressable>
327 </>
328 )}
329 </View>
330 </ScreenScrollView>
331 </PullRequestMarkdownRepository>
332 </View>
333 );
334}