Open History to see the last commit
1/**
2 * Loading states specific to the pull request surface — the first list, a search under way,
3 * and a detail panel opening — use bars in the geometry of the content they stand for, pulsing
4 * on one composited layer. Diff loading uses the shared diff-panel skeleton instead.
5 *
6 * The bars share the app-wide `Skeleton` tone (`muted-foreground` at low alpha, which reads on
7 * both themes) and the single `animate-skeleton` pulse, applied once on the container so any
8 * number of bars costs one opacity animation.
9 */
10import type { PullRequestListEntry, PullRequestSummary } from "@t3tools/contracts";
11import {
12 ArrowLeftIcon,
13 ChevronRightIcon,
14 EllipsisIcon,
15 ExternalLinkIcon,
16 FileDiffIcon,
17 PanelRightIcon,
18 TagIcon,
19 UserPlusIcon,
20 UsersIcon,
21} from "lucide-react";
22import type { ReactNode } from "react";
23
24import { readLocalApi } from "~/localApi";
25import { cn } from "~/lib/utils";
26import { formatRelativeTimeLabel } from "~/timestampFormat";
27
28import { Button, InlineButton } from "../ui/button";
29import { Toggle, ToggleGroup } from "../ui/toggle-group";
30import { MiddleTruncate } from "../ui/middle-truncate";
31import { PullRequestCopyableCode } from "./PullRequestCopyableCode";
32import {
33 PullRequestActorLabel,
34 PullRequestDiffStat,
35 PullRequestLabelChip,
36 PullRequestMetaLine,
37 pullRequestChecksStatePresentation,
38 resolvePullRequestState,
39} from "./pullRequestPresentation";
40
41function GhostBar({ className }: { className?: string | undefined }) {
42 return <div aria-hidden className={cn("h-3 rounded bg-muted-foreground/15", className)} />;
43}
44
45/** Widths cycle rather than randomize, so the ghost renders the same on every pass. */
46const TITLE_WIDTHS = ["w-3/5", "w-2/5", "w-1/2", "w-2/3", "w-2/5", "w-3/5", "w-1/2"];
47const META_WIDTHS = ["w-2/5", "w-1/3", "w-2/5", "w-1/4", "w-1/3", "w-2/5", "w-1/3"];
48const DEFAULT_DETAIL_TABS = [
49 { value: "summary", label: "Summary" },
50 { value: "timeline", label: "Timeline" },
51 { value: "code", label: "Code" },
52] as const;
53
54/** Rows in the list's own grid — glyph, title over meta, time over diffstat. */
55export function PullRequestListGhost({
56 rows = 7,
57 caption,
58}: {
59 rows?: number;
60 /** Said where the group headers speak, for the states with something to say — a search. */
61 caption?: string;
62}) {
63 return (
64 <div
65 role="status"
66 aria-label={caption ?? "Loading pull requests"}
67 className="motion-safe:animate-skeleton space-y-0.5"
68 >
69 {caption ? (
70 <p className="px-3 pb-1 text-xs font-medium text-muted-foreground/70">{caption}</p>
71 ) : null}
72 {Array.from({ length: rows }, (_, index) => (
73 <div
74 key={index}
75 className="grid grid-cols-[auto_minmax(0,1fr)_auto] items-center gap-2 rounded-md px-3 py-2.5"
76 >
77 <GhostBar className="size-4 rounded-full" />
78 <div className="min-w-0 space-y-1.5">
79 <GhostBar className={cn("h-4", TITLE_WIDTHS[index % TITLE_WIDTHS.length])} />
80 <GhostBar className={cn("h-3.5", META_WIDTHS[index % META_WIDTHS.length])} />
81 </div>
82 <div className="flex flex-col items-end gap-1.5">
83 <GhostBar className="w-12" />
84 <GhostBar className="w-16" />
85 </div>
86 </div>
87 ))}
88 </div>
89 );
90}
91
92/**
93 * The detail panel's current expanded shape. Keeping the chrome, summary facts, and description
94 * boundaries in the ghost prevents the loaded pull request from replacing one layout with
95 * another a moment later.
96 */
97export function PullRequestDetailGhost({
98 seed: entry,
99 summary,
100 actions,
101 checkoutCommand,
102 tabs = DEFAULT_DETAIL_TABS,
103 activeTab,
104 number,
105 onBack,
106 onClose,
107 onCheckoutError,
108}: {
109 seed?: PullRequestListEntry | null;
110 summary?: PullRequestSummary | null;
111 actions?: ReactNode;
112 checkoutCommand?: string | null;
113 tabs?: ReadonlyArray<{ value: string; label: string }>;
114 /** The panel's current tab, so the highlight does not jump when the detail arrives. */
115 activeTab?: string;
116 number?: number;
117 onBack?: (() => void) | undefined;
118 onClose?: (() => void) | undefined;
119 onCheckoutError?: ((error: Error) => void) | undefined;
120}) {
121 const seed = summary
122 ? {
123 ...entry,
124 ...summary,
125 isDraft: summary.isDraft ?? entry?.isDraft,
126 }
127 : entry;
128 const statePresentation = seed
129 ? resolvePullRequestState({
130 state: seed.state,
131 isDraft: seed.isDraft ?? false,
132 })
133 : null;
134 // Passing list rollups can omit workflows awaiting approval; wait for detail to claim success.
135 const checksPresentation =
136 seed?.checksState === "failing" || seed?.checksState === "pending"
137 ? pullRequestChecksStatePresentation(seed.checksState)
138 : null;
139 const checkout = checkoutCommand ?? null;
140 const changedFiles = summary?.changedFiles ?? null;
141 const selectedTab =
142 tabs.find((item) => item.value === activeTab)?.value ?? tabs[0]?.value ?? "summary";
143
144 return (
145 <div
146 role="status"
147 aria-label="Loading pull request"
148 className={cn(
149 "flex h-full min-h-0 flex-col overflow-hidden bg-background",
150 !seed && "motion-safe:animate-skeleton",
151 )}
152 >
153 <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">
154 <div className="pl-4 grid h-7 min-w-0 items-center overflow-hidden">
155 <div className="flex min-w-0 items-center gap-1 text-sm text-muted-foreground sm:text-xs">
156 {onBack ? (
157 <Button
158 size="icon-micro"
159 variant="ghost-muted"
160 className="-ml-1.5"
161 onClick={onBack}
162 aria-label="Back to this thread's pull requests"
163 >
164 <ArrowLeftIcon aria-hidden className="size-3.5" />
165 </Button>
166 ) : null}
167 {seed ? (
168 <>
169 <span className="min-w-0 truncate font-medium">{seed.repository}</span>
170 <InlineButton
171 onClick={() => void readLocalApi()?.shell.openExternal(seed.url)}
172 className={statePresentation?.toneClassName}
173 aria-label={`Open pull request #${seed.number} on host`}
174 >
175 #{seed.number}
176 <ExternalLinkIcon aria-hidden className="size-2.5" />
177 </InlineButton>
178 </>
179 ) : (
180 <>
181 <GhostBar className="w-24" />
182 <span className="shrink-0 text-xs text-muted-foreground">#{number ?? "…"}</span>
183 </>
184 )}
185 </div>
186 </div>
187 <div className="mr-4 flex h-7 shrink-0 items-center justify-end gap-1">
188 {actions ?? <GhostBar className="h-6 w-16 rounded-md" />}
189 <Button size="icon-xs" variant="ghost" disabled aria-label="Pull request actions loading">
190 <EllipsisIcon aria-hidden className="size-4" />
191 </Button>
192 {onClose ? (
193 <Button
194 size="icon-xs"
195 variant="ghost"
196 aria-label="Collapse pull request panel"
197 onClick={onClose}
198 >
199 <PanelRightIcon aria-hidden className="size-3.5" />
200 </Button>
201 ) : null}
202 </div>
203
204 <div className="col-span-2 grid grid-rows-[1fr]">
205 <div className="min-h-0 overflow-hidden">
206 <div className="col-span-2 mt-1 min-w-0 px-4 pb-4">
207 {seed ? (
208 <div className="flex min-h-7 min-w-0 items-center sm:min-h-6">
209 <h1 className="truncate text-base font-semibold leading-snug">{seed.title}</h1>
210 </div>
211 ) : (
212 <div className="flex min-h-7 min-w-0 items-center sm:min-h-6">
213 <GhostBar className="h-5 w-4/5 max-w-md" />
214 </div>
215 )}
216 <div className="mt-2 flex min-h-5 min-w-0 items-center gap-2 text-xs text-muted-foreground">
217 {seed ? (
218 <PullRequestMetaLine className="min-w-0 whitespace-nowrap">
219 <PullRequestActorLabel actor={seed.author ?? null} tooltip={false} />
220 <span>updated {formatRelativeTimeLabel(seed.updatedAt)}</span>
221 </PullRequestMetaLine>
222 ) : (
223 <PullRequestMetaLine className="min-w-0 whitespace-nowrap">
224 <span className="flex items-center gap-1.5">
225 <GhostBar className="size-4 rounded-full" />
226 <GhostBar className="h-3 w-14" />
227 </span>
228 <GhostBar className="h-3 w-16" />
229 </PullRequestMetaLine>
230 )}
231 {checkout ? (
232 <PullRequestCopyableCode
233 key={checkout}
234 value={checkout}
235 target="pull request checkout command"
236 copyLabel="Copy checkout command"
237 copiedLabel="Checkout command copied"
238 className="ml-auto font-mono"
239 tooltipSide="bottom"
240 {...(onCheckoutError ? { onError: onCheckoutError } : {})}
241 />
242 ) : null}
243 </div>
244
245 <div className="mt-4 flex min-h-5 min-w-0 items-center gap-2 text-xs text-muted-foreground">
246 <span className="flex min-w-0 flex-1 items-center gap-1.5 font-mono text-xs text-muted-foreground/70">
247 {seed ? (
248 <span className="inline-flex min-w-0 max-w-[40%] shrink-0 items-center gap-1">
249 <code className="flex min-w-0">
250 <MiddleTruncate value={seed.baseBranch} />
251 </code>
252 </span>
253 ) : (
254 <span className="inline-flex min-w-0 max-w-[40%] shrink-0 items-center gap-1">
255 <GhostBar className="h-3 w-12" />
256 </span>
257 )}
258 <ArrowLeftIcon
259 aria-label="receives changes from"
260 className="size-3.5 shrink-0 opacity-60"
261 />
262 {seed ? (
263 <PullRequestCopyableCode
264 key={seed.headBranch}
265 value={seed.headBranch}
266 target="branch name"
267 copyLabel="Copy pull request branch"
268 copiedLabel="Branch name copied"
269 className="min-w-0 font-mono"
270 />
271 ) : (
272 <GhostBar className="h-3 w-32 flex-1" />
273 )}
274 </span>
275 <span className="ml-auto inline-flex shrink-0 items-center justify-end gap-2">
276 <span className="inline-flex min-w-16 items-center justify-end gap-1.5 tabular-nums">
277 <FileDiffIcon aria-hidden className="size-3.5" />
278 {changedFiles === null ? (
279 <GhostBar className="h-3 w-10" />
280 ) : (
281 `${changedFiles.toLocaleString()} ${changedFiles === 1 ? "file" : "files"}`
282 )}
283 </span>
284 {seed ? (
285 <PullRequestDiffStat
286 additions={seed.additions ?? 0}
287 deletions={seed.deletions ?? 0}
288 className="shrink-0 font-mono text-xs"
289 />
290 ) : (
291 <GhostBar className="h-3 w-20" />
292 )}
293 </span>
294 </div>
295 </div>
296 </div>
297 </div>
298
299 <nav
300 className="col-span-2 flex min-w-0 flex-wrap items-center gap-2 border-t border-border/60 px-4 py-2"
301 aria-label="Pull request tabs"
302 inert
303 >
304 <ToggleGroup
305 className="shrink-0"
306 size="segmented"
307 variant="segmented"
308 value={[selectedTab]}
309 >
310 {tabs.map((tab) => (
311 <Toggle key={tab.value} value={tab.value} tabIndex={-1}>
312 {tab.label}
313 </Toggle>
314 ))}
315 </ToggleGroup>
316 {checksPresentation ? (
317 <span
318 className={cn(
319 "ml-auto inline-flex items-center gap-1.5 text-xs",
320 checksPresentation.toneClassName,
321 )}
322 >
323 <checksPresentation.Icon aria-hidden className="size-3.5" />
324 {checksPresentation.label}
325 </span>
326 ) : (
327 <GhostBar className="ml-auto h-3 w-32" />
328 )}
329 </nav>
330 </div>
331
332 <div className="min-h-0 flex-1 overflow-hidden">
333 <section className="px-4 pt-2.5 pb-1">
334 <div className="space-y-2">
335 <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">
336 <span className="flex items-center gap-1.5 text-muted-foreground">
337 <UsersIcon aria-hidden className="size-3.5" />
338 Reviewers
339 </span>
340 <span className="flex min-w-0 items-center gap-1.5 text-foreground">
341 <GhostBar className="h-3 w-10" />
342 <Button size="icon-xs" variant="ghost" disabled aria-label="Reviewers loading">
343 <UserPlusIcon aria-hidden className="size-3.5" />
344 </Button>
345 </span>
346 </div>
347 <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">
348 <span className="flex items-center gap-1.5 text-muted-foreground">
349 <TagIcon aria-hidden className="size-3.5" />
350 Labels
351 </span>
352 <span className="flex min-w-0 flex-wrap items-center gap-1 text-foreground">
353 {entry?.labels ? (
354 entry.labels.length > 0 ? (
355 entry.labels.map((label) => (
356 <PullRequestLabelChip
357 key={label.name}
358 label={label}
359 size="default"
360 className="max-w-48"
361 />
362 ))
363 ) : (
364 <span className="text-muted-foreground">None</span>
365 )
366 ) : (
367 <>
368 <GhostBar className="h-4.5 w-24" />
369 <GhostBar className="h-4.5 w-20" />
370 </>
371 )}
372 <Button size="icon-xs" variant="ghost" disabled aria-label="Labels loading">
373 <TagIcon aria-hidden className="size-3.5" />
374 </Button>
375 </span>
376 </div>
377 </div>
378 </section>
379
380 <section>
381 <div className="sticky top-0 z-10 flex w-full items-center bg-background pr-4">
382 <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">
383 <span>Description</span>
384 <ChevronRightIcon
385 aria-hidden
386 className="size-3.5 rotate-90 text-muted-foreground/60"
387 />
388 </div>
389 </div>
390 <div className="space-y-3 px-4 pb-4">
391 <GhostBar className="h-5 w-40" />
392 <div className="space-y-2">
393 <GhostBar className="h-4 w-full" />
394 <GhostBar className="h-4 w-11/12" />
395 <GhostBar className="h-4 w-4/5" />
396 <GhostBar className="h-4 w-2/3" />
397 </div>
398 </div>
399 </section>
400 </div>
401 </div>
402 );
403}
404
405/** People-shaped: an avatar and a name, in the reviewer picker's own row height. */
406export function PullRequestPeopleGhost({ rows = 4 }: { rows?: number }) {
407 return (
408 <div
409 role="status"
410 aria-label="Loading people"
411 className="motion-safe:animate-skeleton space-y-1 p-1"
412 >
413 {Array.from({ length: rows }, (_, index) => (
414 <div key={index} className="flex h-7 items-center gap-2 rounded-md px-2">
415 <GhostBar className="size-4 rounded-full" />
416 <GhostBar className={META_WIDTHS[index % META_WIDTHS.length]} />
417 </div>
418 ))}
419 </div>
420 );
421}
422
423/** The timeline's own shape: dots on the rail, a line and a date to each. */
424export function PullRequestTimelineGhost({ rows = 6 }: { rows?: number }) {
425 return (
426 <div
427 role="status"
428 aria-label="Loading timeline"
429 className="motion-safe:animate-skeleton px-4 py-5"
430 >
431 <div className="relative ml-2 border-l border-border/70 pl-5">
432 {Array.from({ length: rows }, (_, index) => (
433 <div key={index} className="relative pb-5">
434 <GhostBar className="absolute -left-[1.55rem] top-1 size-2 rounded-full" />
435 <GhostBar className={cn("h-3.5", TITLE_WIDTHS[index % TITLE_WIDTHS.length])} />
436 <GhostBar className="mt-1.5 w-16" />
437 </div>
438 ))}
439 </div>
440 </div>
441 );
442}
443
444/** A compact placeholder for the conversation while the core detail is already readable. */
445export function PullRequestConversationGhost({ rows = 3 }: { rows?: number }) {
446 return (
447 <div
448 role="status"
449 aria-label="Loading pull request conversation"
450 className="motion-safe:animate-skeleton space-y-4 py-2"
451 >
452 {Array.from({ length: rows }, (_, index) => (
453 <div key={index} className="flex items-start gap-2">
454 <GhostBar className="size-5 shrink-0 rounded-full" />
455 <div className="flex-1 space-y-1.5">
456 <GhostBar className={META_WIDTHS[index % META_WIDTHS.length]} />
457 <GhostBar className="w-full" />
458 <GhostBar className="w-3/4" />
459 </div>
460 </div>
461 ))}
462 </div>
463 );
464}