Open History to see the last commit
1import type { PullRequestListEntry, PullRequestSummary } from "@t3tools/contracts";
2import {
3 ArrowLeftIcon,
4 ChevronRightIcon,
5 EllipsisIcon,
6 ExternalLinkIcon,
7 PanelRightIcon,
8 TagIcon,
9 UserPlusIcon,
10 UsersIcon,
11} from "lucide-react";
12import type { ReactNode } from "react";
13
14import { readLocalApi } from "~/localApi";
15import { cn } from "~/lib/utils";
16import { RelativeTime } from "~/components/RelativeTime";
17
18import { Button, InlineButton } from "../ui/button";
19import { MiddleTruncate } from "../ui/middle-truncate";
20import { PullRequestCopyableCode } from "./PullRequestCopyableCode";
21import {
22 PullRequestChecksStatusLine,
23 PullRequestDetailHeaderBody,
24 PullRequestDetailTabBar,
25 PullRequestDetailTitleRow,
26} from "./PullRequestDetailLayout";
27import {
28 PullRequestActorLabel,
29 PullRequestDiffStat,
30 PullRequestLabelChip,
31 pullRequestChecksStatePresentation,
32 resolvePullRequestState,
33} from "./pullRequestPresentation";
34
35function GhostBar({ className }: { className?: string | undefined }) {
36 return <span aria-hidden className={cn("block h-3 rounded bg-muted-foreground/15", className)} />;
37}
38
39const TITLE_WIDTHS = ["w-3/5", "w-2/5", "w-1/2", "w-2/3", "w-2/5", "w-3/5", "w-1/2"];
40const META_WIDTHS = ["w-2/5", "w-1/3", "w-2/5", "w-1/4", "w-1/3", "w-2/5", "w-1/3"];
41const DEFAULT_DETAIL_TABS = [
42 { value: "summary", label: "Summary" },
43 { value: "timeline", label: "Timeline" },
44 { value: "code", label: "Code" },
45] as const;
46
47export function PullRequestListGhost({ rows = 7, caption }: { rows?: number; caption?: string }) {
48 return (
49 <div
50 role="status"
51 aria-label={caption ?? "Loading pull requests"}
52 className="motion-safe:animate-skeleton space-y-0.5"
53 >
54 {caption ? (
55 <p className="px-3 pb-1 text-xs font-medium text-muted-foreground/70">{caption}</p>
56 ) : null}
57 {Array.from({ length: rows }, (_, index) => (
58 <div
59 key={index}
60 className="grid grid-cols-[auto_minmax(0,1fr)_auto] items-center gap-2 rounded-md px-3 py-2.5"
61 >
62 <GhostBar className="size-4 rounded-full" />
63 <div className="min-w-0 space-y-1.5">
64 <GhostBar className={cn("h-4", TITLE_WIDTHS[index % TITLE_WIDTHS.length])} />
65 <GhostBar className={cn("h-3.5", META_WIDTHS[index % META_WIDTHS.length])} />
66 </div>
67 <div className="flex flex-col items-end gap-1.5">
68 <GhostBar className="w-12" />
69 <GhostBar className="w-16" />
70 </div>
71 </div>
72 ))}
73 </div>
74 );
75}
76
77export function PullRequestDetailGhost({
78 seed: entry,
79 summary,
80 actions,
81 checkoutCommand,
82 tabs = DEFAULT_DETAIL_TABS,
83 activeTab,
84 number,
85 onBack,
86 onClose,
87 onCheckoutError,
88}: {
89 seed?: PullRequestListEntry | null;
90 summary?: PullRequestSummary | null;
91 actions?: ReactNode;
92 checkoutCommand?: string | null;
93 tabs?: ReadonlyArray<{ value: string; label: string }>;
94 activeTab?: string;
95 number?: number;
96 onBack?: (() => void) | undefined;
97 onClose?: (() => void) | undefined;
98 onCheckoutError?: ((error: Error) => void) | undefined;
99}) {
100 const seed = summary
101 ? {
102 ...entry,
103 ...summary,
104 isDraft: summary.isDraft ?? entry?.isDraft,
105 }
106 : entry;
107 const statePresentation = seed
108 ? resolvePullRequestState({
109 state: seed.state,
110 isDraft: seed.isDraft ?? false,
111 })
112 : null;
113 // Passing list rollups can omit workflows awaiting approval; wait for detail to claim success.
114 const checksPresentation =
115 seed?.checksState === "failing" || seed?.checksState === "pending"
116 ? pullRequestChecksStatePresentation(seed.checksState)
117 : null;
118 const checkout = checkoutCommand ?? null;
119 const changedFiles = summary?.changedFiles ?? null;
120 const selectedTab =
121 tabs.find((item) => item.value === activeTab)?.value ?? tabs[0]?.value ?? "summary";
122
123 return (
124 <div
125 role="status"
126 aria-label="Loading pull request"
127 className={cn(
128 "flex h-full min-h-0 flex-col overflow-hidden bg-background",
129 !seed && "motion-safe:animate-skeleton",
130 )}
131 >
132 <div className="@container/pr-header grid min-w-0 shrink-0 grid-cols-[minmax(0,1fr)_auto] items-start gap-x-2 border-b border-border/60">
133 <div className="pl-4 grid h-7 min-w-0 items-center overflow-hidden">
134 <div className="flex min-w-0 items-center gap-1 text-sm text-muted-foreground sm:text-xs">
135 {onBack ? (
136 <Button
137 size="icon-micro"
138 variant="ghost-muted"
139 className="-ml-1.5"
140 onClick={onBack}
141 aria-label="Back to this thread's pull requests"
142 >
143 <ArrowLeftIcon aria-hidden className="size-3.5" />
144 </Button>
145 ) : null}
146 {seed ? (
147 <>
148 <span className="min-w-0 truncate font-medium">{seed.repository}</span>
149 <InlineButton
150 onClick={() => void readLocalApi()?.shell.openExternal(seed.url)}
151 aria-label={`Open pull request #${seed.number} on host`}
152 >
153 <span className={statePresentation?.toneClassName}>#{seed.number}</span>
154 <ExternalLinkIcon aria-hidden className="size-2.5" />
155 </InlineButton>
156 </>
157 ) : (
158 <>
159 <GhostBar className="w-24" />
160 <span className="shrink-0 text-xs text-muted-foreground">#{number ?? "…"}</span>
161 </>
162 )}
163 </div>
164 </div>
165 <div className="mr-4 flex h-7 shrink-0 items-center justify-end gap-1">
166 {actions ?? <GhostBar className="h-6 w-16 rounded-md" />}
167 <Button size="icon-xs" variant="ghost" disabled aria-label="Pull request actions loading">
168 <EllipsisIcon aria-hidden className="size-4" />
169 </Button>
170 {onClose ? (
171 <Button
172 size="icon-xs"
173 variant="ghost"
174 aria-label="Collapse pull request panel"
175 onClick={onClose}
176 >
177 <PanelRightIcon aria-hidden className="size-3.5" />
178 </Button>
179 ) : null}
180 </div>
181
182 <div className="col-span-2 grid grid-rows-[1fr]">
183 <div className="min-h-0 overflow-hidden">
184 <PullRequestDetailHeaderBody
185 title={
186 <PullRequestDetailTitleRow>
187 {seed ? (
188 <h1 className="truncate text-base font-semibold leading-snug">{seed.title}</h1>
189 ) : (
190 <GhostBar className="h-5 w-4/5 max-w-md" />
191 )}
192 </PullRequestDetailTitleRow>
193 }
194 author={
195 seed ? (
196 <PullRequestActorLabel actor={seed.author ?? null} tooltip={false} />
197 ) : (
198 <span className="flex items-center gap-1.5">
199 <GhostBar className="size-4 rounded-full" />
200 <GhostBar className="h-3 w-14" />
201 </span>
202 )
203 }
204 updated={
205 seed ? (
206 <span>
207 updated <RelativeTime timestamp={seed.updatedAt} />
208 </span>
209 ) : (
210 <GhostBar className="h-3 w-16" />
211 )
212 }
213 checkout={
214 checkout ? (
215 <PullRequestCopyableCode
216 key={checkout}
217 value={checkout}
218 target="pull request checkout command"
219 copyLabel="Copy checkout command"
220 copiedLabel="Checkout command copied"
221 className="ml-auto font-mono"
222 tooltipSide="bottom"
223 {...(onCheckoutError ? { onError: onCheckoutError } : {})}
224 />
225 ) : null
226 }
227 base={
228 <span className="inline-flex min-w-0 max-w-[40%] shrink-0 items-center gap-1">
229 {seed ? (
230 <code className="flex min-w-0">
231 <MiddleTruncate value={seed.baseBranch} />
232 </code>
233 ) : (
234 <GhostBar className="h-3 w-12" />
235 )}
236 </span>
237 }
238 head={
239 seed ? (
240 <PullRequestCopyableCode
241 key={seed.headBranch}
242 value={seed.headBranch}
243 target="branch name"
244 copyLabel="Copy pull request branch"
245 copiedLabel="Branch name copied"
246 className="min-w-0 font-mono"
247 />
248 ) : (
249 <GhostBar className="h-3 w-32 flex-1" />
250 )
251 }
252 files={
253 changedFiles === null ? (
254 <GhostBar className="h-3 w-10" />
255 ) : (
256 `${changedFiles.toLocaleString()} ${changedFiles === 1 ? "file" : "files"}`
257 )
258 }
259 diffStat={
260 seed ? (
261 <PullRequestDiffStat
262 additions={seed.additions ?? 0}
263 deletions={seed.deletions ?? 0}
264 className="shrink-0 font-mono text-xs"
265 />
266 ) : (
267 <GhostBar className="h-3 w-20" />
268 )
269 }
270 />
271 </div>
272 </div>
273
274 <PullRequestDetailTabBar tabs={tabs} value={selectedTab} inert>
275 {checksPresentation ? (
276 <PullRequestChecksStatusLine
277 className={checksPresentation.toneClassName}
278 icon={<checksPresentation.Icon aria-hidden className="size-3.5" />}
279 label={checksPresentation.label}
280 />
281 ) : (
282 <PullRequestChecksStatusLine
283 icon={<GhostBar className="my-px size-3.5 rounded-full" />}
284 label={<GhostBar className="w-28" />}
285 />
286 )}
287 </PullRequestDetailTabBar>
288 </div>
289
290 <div className="min-h-0 flex-1 overflow-hidden">
291 <section className="px-4 pt-2.5 pb-1">
292 <div className="space-y-2">
293 <div className="grid min-h-7 min-w-0 grid-cols-[6rem_minmax(0,1fr)] items-center gap-2 text-xs sm:min-h-6">
294 <span className="flex items-center gap-1.5 text-muted-foreground">
295 <UsersIcon aria-hidden className="size-3.5" />
296 Reviewers
297 </span>
298 <span className="flex min-w-0 items-center gap-1.5 text-foreground">
299 <GhostBar className="h-3 w-10" />
300 <Button size="icon-xs" variant="ghost" disabled aria-label="Reviewers loading">
301 <UserPlusIcon aria-hidden className="size-3.5" />
302 </Button>
303 </span>
304 </div>
305 <div className="grid min-h-7 min-w-0 grid-cols-[6rem_minmax(0,1fr)] items-center gap-2 text-xs sm:min-h-6">
306 <span className="flex items-center gap-1.5 text-muted-foreground">
307 <TagIcon aria-hidden className="size-3.5" />
308 Labels
309 </span>
310 <span className="flex min-w-0 flex-wrap items-center gap-1 text-foreground">
311 {entry?.labels ? (
312 entry.labels.length > 0 ? (
313 entry.labels.map((label) => (
314 <PullRequestLabelChip
315 key={label.name}
316 label={label}
317 size="default"
318 className="max-w-48"
319 />
320 ))
321 ) : (
322 <span className="text-muted-foreground">None</span>
323 )
324 ) : (
325 <>
326 <GhostBar className="h-4.5 w-24" />
327 <GhostBar className="h-4.5 w-20" />
328 </>
329 )}
330 <Button size="icon-xs" variant="ghost" disabled aria-label="Labels loading">
331 <TagIcon aria-hidden className="size-3.5" />
332 </Button>
333 </span>
334 </div>
335 </div>
336 </section>
337
338 <section>
339 <div className="sticky top-0 z-10 flex w-full items-center bg-background pr-4">
340 <div className="flex min-w-0 flex-1 items-center gap-1.5 px-4 py-3 text-left text-xs font-medium text-muted-foreground">
341 <span>Description</span>
342 <ChevronRightIcon
343 aria-hidden
344 className="size-3.5 rotate-90 text-muted-foreground/60"
345 />
346 </div>
347 </div>
348 <div className="space-y-3 px-4 pb-4">
349 <GhostBar className="h-5 w-40" />
350 <div className="space-y-2">
351 <GhostBar className="h-4 w-full" />
352 <GhostBar className="h-4 w-11/12" />
353 <GhostBar className="h-4 w-4/5" />
354 <GhostBar className="h-4 w-2/3" />
355 </div>
356 </div>
357 </section>
358 </div>
359 </div>
360 );
361}
362
363export function PullRequestPeopleGhost({ rows = 4 }: { rows?: number }) {
364 return (
365 <div
366 role="status"
367 aria-label="Loading people"
368 className="motion-safe:animate-skeleton space-y-1 p-1"
369 >
370 {Array.from({ length: rows }, (_, index) => (
371 <div key={index} className="flex h-7 items-center gap-2 rounded-md px-2">
372 <GhostBar className="size-4 rounded-full" />
373 <GhostBar className={META_WIDTHS[index % META_WIDTHS.length]} />
374 </div>
375 ))}
376 </div>
377 );
378}
379
380export function PullRequestTimelineGhost({ rows = 6 }: { rows?: number }) {
381 return (
382 <div
383 role="status"
384 aria-label="Loading timeline"
385 className="motion-safe:animate-skeleton px-4 py-5"
386 >
387 <div className="relative ml-2 border-l border-border/70 pl-5">
388 {Array.from({ length: rows }, (_, index) => (
389 <div key={index} className="relative pb-5">
390 <GhostBar className="absolute -left-[1.55rem] top-1 size-2 rounded-full" />
391 <GhostBar className={cn("h-3.5", TITLE_WIDTHS[index % TITLE_WIDTHS.length])} />
392 <GhostBar className="mt-1.5 w-16" />
393 </div>
394 ))}
395 </div>
396 </div>
397 );
398}
399
400export function PullRequestConversationGhost({ rows = 3 }: { rows?: number }) {
401 return (
402 <div
403 role="status"
404 aria-label="Loading pull request conversation"
405 className="motion-safe:animate-skeleton space-y-4 py-2"
406 >
407 {Array.from({ length: rows }, (_, index) => (
408 <div key={index} className="flex items-start gap-2">
409 <GhostBar className="size-5 shrink-0 rounded-full" />
410 <div className="flex-1 space-y-1.5">
411 <GhostBar className={META_WIDTHS[index % META_WIDTHS.length]} />
412 <GhostBar className="w-full" />
413 <GhostBar className="w-3/4" />
414 </div>
415 </div>
416 ))}
417 </div>
418 );
419}