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