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

PullRequestTimelineTab.tsx

365 lines11.4 KB

1import type { PullRequestActor, PullRequestDetailView } from "@t3tools/contracts";
2import {
3 ChevronDownIcon,
4 ExternalLinkIcon,
5 FileCode2Icon,
6 GitCommitHorizontalIcon,
7 GitMergeIcon,
8 GitPullRequestClosedIcon,
9 GitPullRequestIcon,
10 MessageSquareIcon,
11} from "lucide-react";
12import { useState, type ReactNode } from "react";
13​
14import { cn } from "~/lib/utils";
15import { readLocalApi } from "~/localApi";
16import { formatRelativeTimeLabel } from "~/timestampFormat";
17​
18import { Button } from "../ui/button";
19import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible";
20import {
21 buildPullRequestTimeline,
22 groupPullRequestTimelineConversations,
23 type PullRequestTimelineEvent,
24} from "./pullRequestDetail.logic";
25import { PullRequestMarkdown } from "./PullRequestMarkdown";
26import {
27 PullRequestActorAvatar,
28 PullRequestDiffStat,
29 PullRequestMetaLine,
30} from "./pullRequestPresentation";
31​
32function TimelineBody({ body, markdown, cwd }: { body: string; markdown: boolean; cwd: string }) {
33 return (
34 <div className="mt-3">
35 {markdown ? (
36 <PullRequestMarkdown text={body} cwd={cwd} />
37 ) : (
38 <p className="whitespace-pre-wrap text-xs text-muted-foreground">{body}</p>
39 )}
40 </div>
41 );
43​
44function ActorName({ actor }: { actor: PullRequestActor | null }) {
45 return <span className="font-semibold text-foreground">{actor?.login ?? "ghost"}</span>;
47​
48function TimelineMarker({
49 children,
50 className,
51}: {
52 children: ReactNode;
53 className?: string | undefined;
54}) {
55 return (
56 <span
57 className={cn(
58 "absolute left-0 top-1/2 z-10 flex size-8 -translate-y-1/2 items-center justify-center bg-background",
59 className,
60 )}
61 >
62 {children}
63 </span>
64 );
66​
67function IconMarker({ icon, className }: { icon: ReactNode; className?: string | undefined }) {
68 return (
69 <TimelineMarker className={className}>
70 <span className="flex size-7 items-center justify-center bg-background text-muted-foreground">
71 {icon}
72 </span>
73 </TimelineMarker>
74 );
76​
77function ActorTimelineMarker({
78 actors,
79 className,
80 fallback,
81 muted = false,
82}: {
83 actors: ReadonlyArray<PullRequestActor>;
84 className?: string | undefined;
85 fallback: ReactNode;
86 muted?: boolean;
87}) {
88 const actor = actors[0];
89 return actor === undefined ? (
90 <IconMarker className={className} icon={fallback} />
91 ) : (
92 <TimelineMarker className={className}>
93 <PullRequestActorAvatar
94 actor={actor}
95 className={cn(
96 "size-7 bg-muted text-[9px] transition-opacity",
97 muted && "opacity-45 grayscale",
98 )}
99 />
100 </TimelineMarker>
101 );
103​
104function friendlyReviewState(value: string): string {
105 const words = value.toLowerCase().replaceAll("_", " ").replaceAll("-", " ");
106 return words.replace(/^\w/u, (letter) => letter.toUpperCase());
108​
109function ReviewStateBadge({ state }: { state: string }) {
110 return (
111 <span className="text-[10px] font-medium text-muted-foreground">
112 {friendlyReviewState(state)}
113 </span>
114 );
116​
117function OpenOnHostButton({ url, onOpen }: { url: string | null; onOpen: (url: string) => void }) {
118 return url === null ? null : (
119 <Button
120 size="icon-xs"
121 variant="ghost"
122 className="-mr-1 -mt-1 shrink-0 text-muted-foreground"
123 aria-label="Open activity on host"
124 onClick={() => onOpen(url)}
125 >
126 <ExternalLinkIcon className="size-3" />
127 </Button>
128 );
130​
131function ConversationCard({
132 event,
133 cwd,
134 onOpen,
135}: {
136 event: PullRequestTimelineEvent;
137 cwd: string;
138 onOpen: (url: string) => void;
139}) {
140 return (
141 <article className="py-2">
142 <div className="px-2">
143 <div className="flex min-w-0 items-start gap-2">
144 <div className="min-w-0 flex-1">
145 <div className="flex min-w-0 flex-wrap items-center gap-x-1.5 gap-y-1 text-xs">
146 <ActorName actor={event.actor} />
147 <span className="text-muted-foreground">{event.title}</span>
148 {event.reviewState ? <ReviewStateBadge state={event.reviewState} /> : null}
149 </div>
150 <PullRequestMetaLine className="mt-1 flex-wrap text-[11px] text-muted-foreground">
151 <span>{formatRelativeTimeLabel(event.at)}</span>
152 {event.path ? (
153 <span className="inline-flex min-w-0 items-center gap-1">
154 <FileCode2Icon aria-hidden className="size-3 shrink-0" />
155 <span className="truncate">{event.path}</span>
156 </span>
157 ) : null}
158 </PullRequestMetaLine>
159 </div>
160 <OpenOnHostButton url={event.url} onOpen={onOpen} />
161 </div>
162 </div>
163 {event.body ? (
164 <div className="px-2 pb-2">
165 <TimelineBody body={event.body} markdown={event.markdown} cwd={cwd} />
166 </div>
167 ) : null}
168 </article>
169 );
171​
172function uniqueConversationActors(events: ReadonlyArray<PullRequestTimelineEvent>) {
173 const actors = new Map<string, PullRequestActor>();
174 for (const event of events) {
175 const actor = event.actor;
176 if (actor !== null && !actors.has(actor.login)) actors.set(actor.login, actor);
177 }
178 return [...actors.values()];
180​
181function ConversationGroup({
182 events,
183 cwd,
184 onOpen,
185}: {
186 events: ReadonlyArray<PullRequestTimelineEvent>;
187 cwd: string;
188 onOpen: (url: string) => void;
189}) {
190 const [open, setOpen] = useState(false);
191 const actors = uniqueConversationActors(events);
192 const first = events[0];
193 if (first === undefined) return null;
194​
195 return (
196 <div className="relative mb-5 pl-12 [contain-intrinsic-block-size:48px] [content-visibility:auto]">
197 <ActorTimelineMarker
198 actors={actors}
199 className="top-6"
200 fallback={<MessageSquareIcon className="size-3.5" />}
201 muted={!open}
202 />
203 <Collapsible open={open} onOpenChange={setOpen}>
204 <div>
205 <CollapsibleTrigger
206 className={cn(
207 "flex w-full min-w-0 items-center gap-3 py-2 text-left transition-opacity hover:opacity-100",
208 open ? "text-foreground opacity-100" : "text-muted-foreground opacity-55",
209 )}
210 >
211 <span className="min-w-0 flex-1">
212 <span className="block text-xs font-semibold">
213 {events.length.toLocaleString()} {events.length === 1 ? "comment" : "comments"}
214 </span>
215 <span className="block truncate text-[10px] text-muted-foreground">
216 {actors.length.toLocaleString()} {actors.length === 1 ? "author" : "authors"} ·{" "}
217 {formatRelativeTimeLabel(first.at)}
218 </span>
219 </span>
220 <ChevronDownIcon
221 aria-hidden
222 className={cn(
223 "size-3.5 shrink-0 text-muted-foreground transition-transform",
224 open && "rotate-180",
225 )}
226 />
227 </CollapsibleTrigger>
228 <CollapsiblePanel>
229 {open ? (
230 <div className="mt-1 space-y-1">
231 {events.map((event) => (
232 <ConversationCard key={event.id} event={event} cwd={cwd} onOpen={onOpen} />
233 ))}
234 </div>
235 ) : null}
236 </CollapsiblePanel>
237 </div>
238 </Collapsible>
239 </div>
240 );
242​
243function CommitEvent({
244 event,
245 onOpen,
246}: {
247 event: PullRequestTimelineEvent;
248 onOpen: (oid: string) => void;
249}) {
250 return (
251 <button
252 type="button"
253 className="group relative mb-5 block w-full rounded-sm pl-12 text-left outline-none [contain-intrinsic-block-size:48px] [content-visibility:auto] focus-visible:ring-2 focus-visible:ring-ring"
254 aria-label={`View commit ${event.id}`}
255 onClick={() => onOpen(event.id)}
256 >
257 <ActorTimelineMarker
258 actors={event.commitAuthors}
259 fallback={<GitCommitHorizontalIcon className="size-3.5" />}
260 />
261 <div className="flex min-w-0 items-center gap-2.5 py-1.5">
262 <div className="min-w-0 flex-1">
263 <div className="truncate text-xs font-semibold text-foreground transition-colors group-hover:text-primary">
264 {event.body ?? "Untitled commit"}
265 </div>
266 <div className="mt-1 flex flex-wrap items-center gap-x-2 gap-y-0.5 text-[10px] text-muted-foreground">
267 <code className="font-mono">{event.id.slice(0, 7)}</code>
268 <span>{formatRelativeTimeLabel(event.at)}</span>
269 </div>
270 </div>
271 {event.additions !== null && event.deletions !== null ? (
272 <PullRequestDiffStat
273 additions={event.additions}
274 deletions={event.deletions}
275 className="ml-auto shrink-0 font-mono text-[10px]"
276 />
277 ) : null}
278 </div>
279 </button>
280 );
282​
283function LifecycleEvent({ event }: { event: PullRequestTimelineEvent }) {
284 const presentation =
285 event.kind === "opened"
286 ? {
287 icon: <GitPullRequestIcon className="size-3.5" />,
288 label: "Pull request opened",
289 }
290 : event.kind === "merged"
291 ? {
292 icon: <GitMergeIcon className="size-3.5" />,
293 label: "Pull request merged",
294 }
295 : {
296 icon: <GitPullRequestClosedIcon className="size-3.5" />,
297 label: "Pull request closed",
298 };
299​
300 return (
301 <div className="relative mb-5 pl-12 [contain-intrinsic-block-size:48px] [content-visibility:auto]">
302 <IconMarker icon={presentation.icon} />
303 <div className="py-1.5 text-xs">
304 <div className="flex flex-wrap items-center gap-1.5">
305 {event.actor ? <ActorName actor={event.actor} /> : null}
306 <span className="font-semibold text-foreground">{presentation.label}</span>
307 </div>
308 <div className="mt-0.5 text-[11px] text-muted-foreground">
309 {formatRelativeTimeLabel(event.at)}
310 </div>
311 </div>
312 </div>
313 );
315​
316export function PullRequestTimelineTab({
317 detail,
318 order,
319 onOpenCommit,
320}: {
321 detail: PullRequestDetailView;
322 order: "newest" | "oldest";
323 onOpenCommit: (oid: string) => void;
324}) {
325 const events = buildPullRequestTimeline(detail);
326 const orderedEvents = order === "newest" ? events : events.toReversed();
327 const rows = groupPullRequestTimelineConversations(orderedEvents);
328 const openOnHost = (url: string) => {
329 void readLocalApi()?.shell.openExternal(url);
330 };
331​
332 return (
333 <div className="h-full overflow-y-auto px-4 py-5">
334 <div className="mx-auto max-w-3xl">
335 <div className="relative">
336 <span aria-hidden className="absolute bottom-5 left-[15px] top-1 w-px bg-border/45" />
337 {rows.map((row) => {
338 if (row.kind === "comments") {
339 return (
340 <ConversationGroup
341 key={`comments:${row.events[0]?.id ?? "empty"}`}
342 events={row.events}
343 cwd={detail.workspaceRoot}
344 onOpen={openOnHost}
345 />
346 );
347 }
348 const event = row.event;
349 if (event.kind === "commit") {
350 return <CommitEvent key={event.id} event={event} onOpen={onOpenCommit} />;
351 }
352 return <LifecycleEvent key={event.id} event={event} />;
353 })}
354 </div>
355​
356 {events.length === 0 ? (
357 <div className="flex flex-col items-center justify-center py-16 text-center text-muted-foreground">
358 <GitPullRequestIcon className="mb-2 size-5" />
359 <p className="text-xs">No activity yet.</p>
360 </div>
361 ) : null}
362 </div>
363 </div>
364 );