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

PullRequestTimeline.tsx

298 lines11.6 KB

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