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