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

PullRequestFiles.tsx

309 lines12.6 KB

1import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
2import { PullRequestSectionHeading } from "./PullRequestSectionHeading";
3import { AnimatedLegendList } from "@legendapp/list/reanimated";
4import Animated from "react-native-reanimated";
5import { PR_ROW_LAYOUT, PR_ROW_ENTER, PR_ROW_EXIT } from "./pullRequestAnimations";
6import { useNavigation } from "@react-navigation/native";
7import { useState } from "react";
8import { Pressable, View } from "react-native";
9import { AppText as Text } from "../../components/ui/AppText";
10import { EmptyState } from "../../components/ui/EmptyState";
11import { Menu } from "../../components/ui/Menu";
12import { Button } from "../../components/ui/Button";
13import { IconButton } from "../../components/ui/IconButton";
14import { SymbolView } from "../../components/ui/AppSymbol";
15import { PierreEntryIcon } from "../../components/PierreEntryIcon";
16import { composerPullRequests } from "../../state/pull-requests";
17import { useEnvironmentQuery } from "../../state/query";
18import { PullRequestDiffStats, PullRequestReadStatus } from "./PullRequestPresentation";
19import {
20 pullRequestTarget as requestTarget,
21 usePullRequestDiff,
22 type PullRequestDiffParams,
23} from "./usePullRequestDiff";
24import { usePullRequestFilesViewed } from "./usePullRequestFilesViewed";
25import { usePullRequestFileFilter } from "./usePullRequestFileFilter";
26​
27function fileDirectory(path: string) {
28 const separator = path.lastIndexOf("/");
29 return separator < 0 ? "" : path.slice(0, separator);
31​
32export function usePullRequestFilesContent(props: {
33 readonly params: PullRequestDiffParams;
34 readonly onSelectFile?: (path: string) => void;
35 readonly groupByDirectory?: boolean;
36}) {
37 const navigation = useNavigation();
38 const result = usePullRequestDiff(props.params);
39 const detail = useEnvironmentQuery(composerPullRequests.detail(requestTarget(props.params)));
40 const viewed = usePullRequestFilesViewed(props.params, props.params.commit ? null : detail.data);
41 const filter = usePullRequestFileFilter(result.parsed);
42 const parsed = filter.parsed;
43 const loading = result.isPending && parsed.kind === "empty" && !result.error;
44 const fileCount = props.params.commit ? undefined : detail.data?.changedFiles;
45 const summary = props.params.commit ? undefined : (detail.data ?? props.params.initialSummary);
46 const [collapsedDirectories, setCollapsedDirectories] = useState<ReadonlySet<string>>(
47 () => new Set(),
48 );
49 const files = parsed.kind === "files" ? parsed.files : [];
50 const orderedFiles = props.groupByDirectory
51 ? [...files].sort(
52 (a, b) =>
53 fileDirectory(a.path).localeCompare(fileDirectory(b.path)) ||
54 a.path.localeCompare(b.path),
55 )
56 : files;
57 const rows = orderedFiles.map((file, index) => {
58 const directory = fileDirectory(file.path);
59 const previousDirectory = fileDirectory(orderedFiles[index - 1]?.path ?? "");
60 const showDirectory =
61 props.groupByDirectory && (index === 0 || directory !== previousDirectory);
62 const collapsed = props.groupByDirectory && collapsedDirectories.has(directory);
63 if (collapsed && !showDirectory) return null;
64 return {
65 key: file.id,
66 content: (
67 <View>
68 {showDirectory ? (
69 <Pressable
70 accessibilityRole="button"
71 accessibilityLabel={directory || "Repository root"}
72 accessibilityState={{ expanded: !collapsed }}
73 onPress={() =>
74 setCollapsedDirectories((current) => {
75 const next = new Set(current);
76 if (next.has(directory)) next.delete(directory);
77 else next.add(directory);
78 return next;
79 })
80 }
81 className="min-h-11 flex-row items-center gap-2 pt-4 pb-2 active:opacity-70"
82 >
83 <SymbolView
84 name={collapsed ? "chevron.right" : "chevron.down"}
85 size={11}
86 tintColorClassName="accent-icon-muted"
87 />
88 <SymbolView name="folder" size={16} tintColorClassName="accent-icon-muted" />
89 <Text className="min-w-0 flex-1 text-xs text-foreground-muted" numberOfLines={2}>
90 {directory || "Repository root"}
91 </Text>
92 </Pressable>
93 ) : null}
94 {!collapsed ? (
95 <Pressable
96 accessibilityRole="button"
97 accessibilityLabel={`View diff for ${file.path}, ${file.additions} additions, ${file.deletions} deletions`}
98 onPress={() => {
99 if (props.onSelectFile) props.onSelectFile(file.path);
100 else
101 navigation.navigate("PullRequestFile", {
102 ...props.params,
103 path: file.path,
104 });
105 }}
106 className="min-h-14 flex-row items-center gap-3 border-b border-border-subtle py-3 active:opacity-70"
107 >
108 <PierreEntryIcon kind="file" path={file.path} size={20} />
109 <View className="min-w-0 flex-1">
110 <Text className="text-sm font-t3-medium text-foreground" numberOfLines={2}>
111 {file.path.split("/").at(-1)}
112 </Text>
113 {directory && !props.groupByDirectory ? (
114 <Text className="mt-0.5 text-2xs text-foreground-muted" numberOfLines={1}>
115 {directory}
116 </Text>
117 ) : null}
118 {file.previousPath ? (
119 <Text className="mt-0.5 text-2xs text-foreground-muted" numberOfLines={1}>
120 Renamed from {file.previousPath}
121 </Text>
122 ) : null}
123 </View>
124 <PullRequestDiffStats additions={file.additions} deletions={file.deletions} compact />
125 {viewed.isViewed(file.path) ? (
126 <SymbolView
127 name="checkmark.circle.fill"
128 size={18}
129 tintColorClassName="accent-adaptive-emerald-600-400"
130 />
131 ) : null}
132 <SymbolView name="chevron.right" size={12} tintColorClassName="accent-icon-muted" />
133 </Pressable>
134 ) : null}
135 </View>
136 ),
137 };
138 });
139 return {
140 refresh: result.refresh,
141 rows: loading
142 ? Array.from({ length: Math.min(fileCount ?? 4, 6) }, (_, index) => ({
143 key: `loading-file:${index}`,
144 content: (
145 <View
146 accessible={false}
147 importantForAccessibility="no-hide-descendants"
148 className="min-h-14 flex-row items-center gap-3 border-b border-border-subtle py-3"
149 >
150 <View className="size-5 rounded-md bg-subtle-strong" />
151 <View className="flex-1 gap-2">
152 <View
153 className="h-3.5 rounded bg-subtle-strong"
154 style={{ width: index % 2 ? "65%" : "80%" }}
155 />
156 <View className="h-2.5 w-2/5 rounded bg-subtle-strong" />
157 </View>
158 <View className="h-3 w-7 rounded bg-subtle-strong" />
159 </View>
160 ),
161 }))
162 : rows.filter((row) => row !== null),
163 header: (
164 <>
165 <PullRequestReadStatus pending={false} error={result.error} />
166 {loading ? (
167 <View
168 accessibilityLabel="Loading changed files"
169 accessibilityState={{ busy: true }}
170 className="min-h-17 flex-row items-center gap-2 py-3"
171 >
172 <View className="min-w-0 flex-1 gap-1">
173 <PullRequestSectionHeading>Changed files</PullRequestSectionHeading>
174 {fileCount !== undefined ? (
175 <Text className="text-2xs text-foreground-muted">
176 {fileCount} {fileCount === 1 ? "file" : "files"}
177 </Text>
178 ) : (
179 <View className="h-2.5 w-28 rounded bg-subtle-strong" />
180 )}
181 </View>
182 {summary?.additions !== undefined && summary.deletions !== undefined ? (
183 <PullRequestDiffStats
184 additions={summary.additions}
185 deletions={summary.deletions}
186 compact
187 />
188 ) : null}
189 <View className="size-11 items-center justify-center opacity-40">
190 <SymbolView
191 name="line.3.horizontal.decrease"
192 size={20}
193 tintColorClassName="accent-icon-muted"
194 />
195 </View>
196 </View>
197 ) : parsed.kind === "files" ? (
198 <>
199 <View className="min-h-17 flex-row items-center gap-2 py-3">
200 <View className="min-w-0 flex-1 gap-1">
201 <PullRequestSectionHeading>Changed files</PullRequestSectionHeading>
202 <Text className="text-2xs text-foreground-muted">
203 {parsed.fileCount} {filter.hiddenCount ? "visible" : "changed"}{" "}
204 {parsed.fileCount === 1 ? "file" : "files"}
205 {viewed.supported
206 ? ` · ${parsed.files.filter((file) => viewed.isViewed(file.path)).length} viewed`
207 : ""}
208 {" · "}
209 {filter.hideTestFiles
210 ? `${filter.hiddenCount} test ${filter.hiddenCount === 1 ? "file" : "files"} hidden`
211 : "Test files included"}
212 </Text>
213 </View>
214 <PullRequestDiffStats
215 additions={parsed.additions}
216 deletions={parsed.deletions}
217 compact
218 />
219 <Menu
220 title="Review files"
221 actions={[
222 {
223 id: "hide-tests",
224 title: "Hide test files",
225 image: "line.3.horizontal.decrease",
226 state: filter.hideTestFiles ? "on" : "off",
227 },
228 ]}
229 onPressAction={filter.toggle}
230 >
231 <View pointerEvents="none">
232 <IconButton
233 icon="line.3.horizontal.decrease"
234 accessibilityLabel="Filter review files"
235 selected={filter.hideTestFiles}
236 />
237 </View>
238 </Menu>
239 </View>
240 {parsed.files.length === 0 && filter.hiddenCount > 0 ? (
241 <EmptyState
242 title={
243 result.nextCursor ? "No non-test files loaded yet" : "Only test files changed"
244 }
245 detail="Test files are hidden from this review."
246 action={<Button label="Show test files" tone="contrast" onPress={filter.showAll} />}
247 variant="plain"
248 />
249 ) : null}
250 </>
251 ) : !result.isPending && !result.error ? (
252 <Text className="py-6 text-sm text-foreground-muted">No text changes available.</Text>
253 ) : null}
254 </>
255 ),
256 footer: (
257 <>
258 {result.nextCursor ? (
259 <Pressable
260 onPress={result.error ? result.refresh : result.loadMore}
261 accessibilityRole="button"
262 className="min-h-11 items-center justify-center rounded-xl bg-grouped-card px-4 py-3"
263 >
264 {result.isPending ? (
265 <AppActivityIndicator />
266 ) : (
267 <Text className="text-sm font-t3-medium">
268 {result.error ? "Retry loading files" : "Load more files"}
269 </Text>
270 )}
271 </Pressable>
272 ) : null}
273 {viewed.error ? (
274 <Text className="py-2 text-xs text-danger-foreground">{viewed.error}</Text>
275 ) : null}
276 {parsed.kind === "files" && parsed.notice ? (
277 <Text className="py-4 text-xs text-foreground-muted">{parsed.notice}</Text>
278 ) : null}
279 </>
280 ),
281 };
283​
284export function PullRequestFiles(props: Parameters<typeof usePullRequestFilesContent>[0]) {
285 const content = usePullRequestFilesContent(props);
286 const [animationsReady, setAnimationsReady] = useState(false);
287 return (
288 <AnimatedLegendList
289 data={content.rows}
290 keyExtractor={(row) => row.key}
291 itemLayoutAnimation={PR_ROW_LAYOUT}
292 onLoad={() => setAnimationsReady(true)}
293 renderItem={({ item }) => (
294 <Animated.View
295 key={item.key}
296 entering={animationsReady ? PR_ROW_ENTER : undefined}
297 exiting={animationsReady ? PR_ROW_EXIT : undefined}
298 >
299 {item.content}
300 </Animated.View>
301 )}
302 recycleItems={false}
303 estimatedItemSize={64}
304 ListHeaderComponent={content.header}
305 ListFooterComponent={content.footer}
306 contentContainerStyle={{ paddingHorizontal: 16, paddingBottom: 20 }}
307 />
308 );