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

PullRequestTimeline.tsx

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