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

PullRequestTimeline.tsx

386 lines14.6 KB

1import {
2 parsePullRequestAnchor,
3 pullRequestAnchorMatchesUrl,
4} from "@t3tools/shared/pullRequestAnchor";
5import { PullRequestEntryRow } from "./PullRequestEntryRow";
6import Svg, { Defs, LinearGradient, Rect, Stop } from "react-native-svg";
7import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
8import IconGitCommit from "@tabler/icons-react-native/IconGitCommit";
9import {
10 pullRequestConversationThreads,
11 type PullRequestActivity,
12 type PullRequestComment,
13} from "@t3tools/contracts";
14import { StackActions, type NavigationProp } from "@react-navigation/native";
15import { msg, T, useGT, useMessages, Var } from "gt-react-native";
16import { useMemo, useState, type ComponentProps, type ReactNode } from "react";
17import { Pressable, StyleSheet, View } from "react-native";
18import { parseMarkdownDocument } from "../../lib/markdownDocument";
19import { AppText as Text } from "../../components/ui/AppText";
20import { SymbolView, type AppSymbolName } from "../../components/ui/AppSymbol";
21import { byTimestamp, relativeTime } from "../../lib/time";
22import { PullRequestAvatar } from "./PullRequestPresentation";
23import { PullRequestMarkdown } from "./PullRequestMarkdown";
24import { PullRequestCommentFooter } from "./PullRequestReactions";
25import { collapsedTimelineCommentMarkdown } from "./PullRequestTimeline.logic";
26import { pullRequestDescriptionContent } from "./pullRequestDescriptionContent";
27import { discussionCommentCountLabel } from "./pullRequestDiffComments";
28import { normalizedReviewState } from "./pullRequestStatus";
29import type { PullRequestParams } from "./usePullRequestDiff";
30import type { MessageResolver } from "../../i18n/messages";
31​
32function TimelineEvent(props: {
33 icon: AppSymbolName | "commit";
34 tone?: "success" | "danger";
35 children: ReactNode;
36}) {
37 const { themeVariables: colors } = useAppearancePreferences();
38 return (
39 <PullRequestEntryRow
40 leading={
41 <>
42 <View className="absolute top-0 bottom-0 w-px bg-border-subtle" />
43 <View className="size-8 items-center justify-center rounded-full border border-border-subtle bg-grouped-card">
44 {props.icon === "commit" ? (
45 <View style={{ transform: [{ rotate: "90deg" }] }}>
46 <IconGitCommit
47 size={17}
48 color={colors["--color-foreground-muted"] ?? "#838383"}
49 strokeWidth={1.8}
50 />
51 </View>
52 ) : (
53 <SymbolView
54 name={props.icon}
55 size={15}
56 tintColorClassName={
57 props.tone === "success"
58 ? "accent-adaptive-emerald-600-400"
59 : props.tone === "danger"
60 ? "accent-danger-foreground"
61 : "accent-icon-muted"
62 }
63 />
64 )}
65 </View>
66 </>
67 }
68 >
69 <View className="gap-2 pt-1 pb-6">{props.children}</View>
70 </PullRequestEntryRow>
71 );
73​
74type CommentActions = {
75 readonly permissions: ComponentProps<typeof PullRequestCommentFooter>["permissions"];
76 readonly onChanged: () => void;
77};
78​
79function TimelineComment({
80 comment,
81 params,
82 actions,
83}: {
84 comment: PullRequestComment;
85 params: PullRequestParams;
86 actions: CommentActions;
87}) {
88 const target = parsePullRequestAnchor(params.navigationUrl);
89 const linked =
90 target?.kind === "timeline" && pullRequestAnchorMatchesUrl(target.anchor, comment.url);
91 const [expanded, setExpanded] = useState(linked);
92 const gt = useGT();
93 const messages = useMessages();
94 const { themeVariables: colors } = useAppearancePreferences();
95 const long = comment.body.length > 450 || comment.body.split("\n").length > 8;
96 const content = useMemo(
97 () => pullRequestDescriptionContent(parseMarkdownDocument(comment.body), [], messages),
98 [comment.body, messages],
99 );
100 const preview = content.preview;
101 const state = normalizedReviewState(comment.reviewState);
102 const approved = state === "APPROVED";
103 const changes = state === "CHANGES_REQUESTED";
104 return (
105 <TimelineEvent
106 icon={approved ? "checkmark" : changes ? "doc.text" : "text.bubble"}
107 tone={approved ? "success" : changes ? "danger" : undefined}
108 >
109 <View accessible accessibilityRole="header" className="flex-row items-center gap-2">
110 <PullRequestAvatar actor={comment.author} size={22} />
111 <View className="min-w-0 flex-1">
112 <Text className="text-sm">
113 {approved ? (
114 <T>
115 <Text className="font-t3-medium">
116 <Var>{comment.author?.login ?? gt("Unknown author")}</Var>
117 </Text>
118 <Text className="text-foreground-muted"> approved</Text>
119 </T>
120 ) : changes ? (
121 <T>
122 <Text className="font-t3-medium">
123 <Var>{comment.author?.login ?? gt("Unknown author")}</Var>
124 </Text>
125 <Text className="text-foreground-muted"> requested changes</Text>
126 </T>
127 ) : (
128 <T>
129 <Text className="font-t3-medium">
130 <Var>{comment.author?.login ?? gt("Unknown author")}</Var>
131 </Text>
132 <Text className="text-foreground-muted"> commented</Text>
133 </T>
134 )}
135 </Text>
136 </View>
137 <Text className="text-2xs text-foreground-tertiary">
138 {relativeTime(comment.createdAt, undefined, messages)}
139 </Text>
140 </View>
141 {comment.path ? (
142 <Text className="font-mono text-2xs text-foreground-muted" numberOfLines={2}>
143 {comment.path}
144 </Text>
145 ) : null}
146 {preview.trim() || content.attachments.length ? (
147 <View
148 className={
149 linked
150 ? "gap-2 overflow-hidden rounded-2xl border border-primary bg-primary/10 p-3"
151 : "gap-2 overflow-hidden rounded-2xl bg-grouped-card p-3"
152 }
153 >
154 <View style={long && !expanded ? { maxHeight: 240, overflow: "hidden" } : undefined}>
155 <PullRequestMarkdown
156 markdown={
157 long && !expanded ? collapsedTimelineCommentMarkdown(comment.body) : comment.body
158 }
159 imageGroupMarkdown={comment.body}
160 />
161 </View>
162 {long && !expanded ? (
163 <View
164 pointerEvents="none"
165 style={[
166 StyleSheet.absoluteFill,
167 { top: undefined, bottom: 0, left: 0, right: 0, height: 80, zIndex: 1 },
168 ]}
169 >
170 <Svg width="100%" height={80}>
171 <Defs>
172 <LinearGradient
173 id="comment-fade"
174 x1="0"
175 y1="0"
176 x2="0"
177 y2="80"
178 gradientUnits="userSpaceOnUse"
179 >
180 <Stop offset="0" stopColor={colors["--color-grouped-card"]} stopOpacity={0} />
181 <Stop
182 offset="0.5"
183 stopColor={colors["--color-grouped-card"]}
184 stopOpacity={0.94}
185 />
186 <Stop
187 offset="0.72"
188 stopColor={colors["--color-grouped-card"]}
189 stopOpacity={1}
190 />
191 <Stop offset="1" stopColor={colors["--color-grouped-card"]} stopOpacity={1} />
192 </LinearGradient>
193 </Defs>
194 <Rect width="100%" height="100%" fill="url(#comment-fade)" />
195 </Svg>
196 <View
197 style={{
198 position: "absolute",
199 bottom: 0,
200 left: 0,
201 right: 0,
202 height: 22,
203 backgroundColor: colors["--color-grouped-card"],
204 }}
205 />
206 </View>
207 ) : null}
208 {long ? (
209 <Pressable
210 accessibilityRole="button"
211 accessibilityState={{ expanded }}
212 onPress={() => setExpanded(!expanded)}
213 hitSlop={8}
214 style={
215 !expanded
216 ? { position: "absolute", bottom: 2, left: 12, right: 12, height: 44, zIndex: 2 }
217 : undefined
218 }
219 className="flex-row items-center justify-center gap-1 py-2 active:opacity-70"
220 >
221 <Text className="text-xs font-t3-medium text-foreground">
222 {expanded ? gt("Show less") : gt("Show more")}
223 </Text>
224 <SymbolView
225 name={expanded ? "chevron.up" : "chevron.down"}
226 size={10}
227 tintColorClassName="accent-foreground"
228 />
229 </Pressable>
230 ) : null}
231 </View>
232 ) : null}
233 <PullRequestCommentFooter
234 params={params}
235 comment={comment}
236 permissions={actions.permissions}
237 onChanged={actions.onChanged}
238 />
239 </TimelineEvent>
240 );
242​
243export function pullRequestTimelineRows(
244 activity: PullRequestActivity,
245 params: PullRequestParams,
246 navigation: Pick<NavigationProp<ReactNavigation.RootParamList>, "navigate" | "dispatch">,
247 actions: CommentActions,
248 newestFirst: boolean,
249 resolve: MessageResolver,
250) {
251 const linkTarget = parsePullRequestAnchor(params.navigationUrl);
252 const threads = pullRequestConversationThreads(activity);
253 const discussionIds = new Set(
254 threads.flatMap((thread) => thread.comments.map((comment) => comment.id)),
255 );
256 const events = [
257 ...activity.comments
258 .filter((comment) => !discussionIds.has(comment.id))
259 .map((comment) => ({
260 key: `comment:${comment.id}`,
261 date: comment.createdAt,
262 content: (
263 <TimelineComment
264 key={
265 linkTarget?.kind === "timeline" &&
266 pullRequestAnchorMatchesUrl(linkTarget.anchor, comment.url)
267 ? `${comment.id}:${params.navigationId ?? params.navigationUrl}`
268 : comment.id
269 }
270 comment={comment}
271 params={params}
272 actions={actions}
273 />
274 ),
275 })),
276 ...threads.map((thread) => ({
277 key: `discussion:${thread.id}`,
278 date: thread.comments[0]?.createdAt ?? "",
279 content: (
280 <TimelineEvent icon="text.bubble" tone={thread.isResolved ? "success" : undefined}>
281 <View accessible accessibilityRole="header" className="flex-row items-center gap-2">
282 <PullRequestAvatar actor={thread.comments[0]?.author ?? null} size={22} />
283 <Text className="flex-1 text-sm">
284 <T>
285 <Text className="font-t3-medium">
286 <Var>{thread.comments[0]?.author?.login ?? resolve(msg("Unknown author"))}</Var>
287 </Text>
288 <Text className="text-foreground-muted"> started a discussion</Text>
289 </T>
290 </Text>
291 {thread.comments[0] ? (
292 <Text className="text-2xs text-foreground-tertiary">
293 {relativeTime(thread.comments[0].createdAt, undefined, resolve)}
294 </Text>
295 ) : null}
296 </View>
297 <Pressable
298 accessibilityRole="button"
299 accessibilityLabel={
300 thread.path === null
301 ? thread.isResolved
302 ? resolve(msg("Open resolved discussion in the overview"))
303 : resolve(msg("Open unresolved discussion in the overview"))
304 : thread.isResolved
305 ? resolve(msg("Open resolved discussion in {path}", { path: thread.path }))
306 : resolve(msg("Open unresolved discussion in {path}", { path: thread.path }))
307 }
308 onPress={() =>
309 navigation.navigate("PullRequestDiscussion", { ...params, discussionId: thread.id })
310 }
311 className="gap-2 rounded-2xl bg-grouped-card p-3 active:opacity-70"
312 >
313 <View className="flex-row items-center gap-2">
314 {thread.path === null ? (
315 <Text className="flex-1 text-xs">
316 <T>Overview</T>
317 </Text>
318 ) : (
319 <>
320 <SymbolView name="doc.text" size={14} tintColorClassName="accent-icon-muted" />
321 <Text className="flex-1 font-mono text-xs" numberOfLines={2}>
322 {thread.path}
323 {thread.line ? `:${thread.line}` : ""}
324 </Text>
325 </>
326 )}
327 <SymbolView name="chevron.right" size={12} tintColorClassName="accent-icon-muted" />
328 </View>
329 <Text className="text-xs text-foreground-muted">
330 {thread.isResolved
331 ? resolve(msg("Resolved", { $context: "review thread state" }))
332 : resolve(msg("Unresolved", { $context: "review thread state" }))}
333 {thread.isOutdated
334 ? ` · ${resolve(msg("Outdated", { $context: "review thread state" }))}`
335 : ""}{" "}
336 ·{" "}
337 {discussionCommentCountLabel(thread.commentCount ?? thread.comments.length, resolve)}
338 </Text>
339 </Pressable>
340 </TimelineEvent>
341 ),
342 })),
343 ...activity.commits.map((commit) => ({
344 key: `commit:${commit.oid}`,
345 date: commit.committedDate,
346 content: (
347 <TimelineEvent icon="commit">
348 <Pressable
349 accessibilityRole="button"
350 accessibilityLabel={resolve(
351 msg("View commit {sha}, {headline}", {
352 sha: commit.oid.slice(0, 7),
353 headline: commit.messageHeadline,
354 }),
355 )}
356 onPress={() =>
357 navigation.dispatch(
358 StackActions.push("PullRequestFile", { ...params, commit: commit.oid, path: "" }),
359 )
360 }
361 className="flex-row items-center gap-2 active:opacity-70"
362 >
363 <PullRequestAvatar actor={commit.authors?.[0] ?? null} size={22} />
364 <Text className="min-w-0 flex-1 text-sm" numberOfLines={1}>
365 {commit.messageHeadline}
366 </Text>
367 <Text className="font-mono text-2xs text-foreground-muted">
368 {commit.oid.slice(0, 7)}
369 </Text>
370 </Pressable>
371 </TimelineEvent>
372 ),
373 })),
374 ].sort(byTimestamp(newestFirst));
375 if (activity.commentsTruncated || activity.reviewThreadsTruncated)
376 events.push({
377 key: "truncated",
378 date: "",
379 content: (
380 <Text className="py-3 text-xs text-foreground-muted">
381 <T>More activity is available on the provider.</T>
382 </Text>
383 ),
384 });
385 return events;