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