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
220 actor={seed.author ?? null}
221 className="font-medium"
222 tooltip={false}
223 />
224 <span>updated {formatRelativeTimeLabel(seed.updatedAt)}</span>
225 </PullRequestMetaLine>
226 ) : (
227 <PullRequestMetaLine className="min-w-0 whitespace-nowrap">
228 <span className="flex items-center gap-1.5">
229 <GhostBar className="size-4 rounded-full" />
230 <GhostBar className="h-3 w-14" />
231 </span>
232 <GhostBar className="h-3 w-16" />
233 </PullRequestMetaLine>
234 )}
235 {checkout ? (
236 <PullRequestCopyableCode
237 key={checkout}
238 value={checkout}
239 target="pull request checkout command"
240 copyLabel="Copy checkout command"
241 copiedLabel="Checkout command copied"
242 className="ml-auto font-mono"
243 tooltipSide="bottom"
244 {...(onCheckoutError ? { onError: onCheckoutError } : {})}
245 />
246 ) : null}
247 </div>
248
249 <div className="mt-4 flex min-h-5 min-w-0 items-center gap-2 text-xs text-muted-foreground">
250 <span className="flex min-w-0 flex-1 items-center gap-1.5 font-mono text-xs text-muted-foreground/70">
251 {seed ? (
252 <span className="inline-flex min-w-0 max-w-[40%] shrink-0 items-center gap-1">
253 <code className="flex min-w-0">
254 <MiddleTruncate value={seed.baseBranch} />
255 </code>
256 </span>
257 ) : (
258 <span className="inline-flex min-w-0 max-w-[40%] shrink-0 items-center gap-1">
259 <GhostBar className="h-3 w-12" />
260 </span>
261 )}
262 <ArrowLeftIcon
263 aria-label="receives changes from"
264 className="size-3.5 shrink-0 opacity-60"
265 />
266 {seed ? (
267 <PullRequestCopyableCode
268 key={seed.headBranch}
269 value={seed.headBranch}
270 target="branch name"
271 copyLabel="Copy pull request branch"
272 copiedLabel="Branch name copied"
273 className="min-w-0 font-mono"
274 />
275 ) : (
276 <GhostBar className="h-3 w-32 flex-1" />
277 )}
278 </span>
279 <span className="ml-auto inline-flex shrink-0 items-center justify-end gap-2">
280 <span className="inline-flex min-w-16 items-center justify-end gap-1.5 tabular-nums">
281 <FileDiffIcon aria-hidden className="size-3.5" />
282 {changedFiles === null ? (
283 <GhostBar className="h-3 w-10" />
284 ) : (
285 `${changedFiles.toLocaleString()} ${changedFiles === 1 ? "file" : "files"}`
286 )}
287 </span>
288 {seed ? (
289 <PullRequestDiffStat
290 additions={seed.additions ?? 0}
291 deletions={seed.deletions ?? 0}
292 className="shrink-0 font-mono text-xs"
293 />
294 ) : (
295 <GhostBar className="h-3 w-20" />
296 )}
297 </span>
298 </div>
299 </div>
300 </div>
301 </div>
302
303 <nav
304 className="col-span-2 flex min-w-0 flex-wrap items-center gap-2 border-t border-border/60 px-4 py-2"
305 aria-label="Pull request tabs"
306 inert
307 >
308 <ToggleGroup
309 className="shrink-0"
310 size="segmented"
311 variant="segmented"
312 value={[selectedTab]}
313 >
314 {tabs.map((tab) => (
315 <Toggle key={tab.value} value={tab.value} tabIndex={-1}>
316 {tab.label}
317 </Toggle>
318 ))}
319 </ToggleGroup>
320 {checksPresentation ? (
321 <span
322 className={cn(
323 "ml-auto inline-flex items-center gap-1.5 text-xs",
324 checksPresentation.toneClassName,
325 )}
326 >
327 <checksPresentation.Icon aria-hidden className="size-3.5" />
328 {checksPresentation.label}
329 </span>
330 ) : (
331 <GhostBar className="ml-auto h-3 w-32" />
332 )}
333 </nav>
334 </div>
335
336 <div className="min-h-0 flex-1 overflow-hidden">
337 <section className="px-4 pt-2.5 pb-1">
338 <div className="space-y-2">
339 <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">
340 <span className="flex items-center gap-1.5 text-muted-foreground">
341 <UsersIcon aria-hidden className="size-3.5" />
342 Reviewers
343 </span>
344 <span className="flex min-w-0 items-center gap-1.5 text-foreground">
345 <GhostBar className="h-3 w-10" />
346 <Button size="icon-xs" variant="ghost" disabled aria-label="Reviewers loading">
347 <UserPlusIcon aria-hidden className="size-3.5" />
348 </Button>
349 </span>
350 </div>
351 <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">
352 <span className="flex items-center gap-1.5 text-muted-foreground">
353 <TagIcon aria-hidden className="size-3.5" />
354 Labels
355 </span>
356 <span className="flex min-w-0 flex-wrap items-center gap-1 text-foreground">
357 {entry?.labels ? (
358 entry.labels.length > 0 ? (
359 entry.labels.map((label) => (
360 <PullRequestLabelChip
361 key={label.name}
362 label={label}
363 size="default"
364 className="max-w-48"
365 />
366 ))
367 ) : (
368 <span className="text-muted-foreground">None</span>
369 )
370 ) : (
371 <>
372 <GhostBar className="h-4.5 w-24" />
373 <GhostBar className="h-4.5 w-20" />
374 </>
375 )}
376 <Button size="icon-xs" variant="ghost" disabled aria-label="Labels loading">
377 <TagIcon aria-hidden className="size-3.5" />
378 </Button>
379 </span>
380 </div>
381 </div>
382 </section>
383
384 <section>
385 <div className="sticky top-0 z-10 flex w-full items-center bg-background pr-4">
386 <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">
387 <span>Description</span>
388 <ChevronRightIcon
389 aria-hidden
390 className="size-3.5 rotate-90 text-muted-foreground/60"
391 />
392 </div>
393 </div>
394 <div className="space-y-3 px-4 pb-4">
395 <GhostBar className="h-5 w-40" />
396 <div className="space-y-2">
397 <GhostBar className="h-4 w-full" />
398 <GhostBar className="h-4 w-11/12" />
399 <GhostBar className="h-4 w-4/5" />
400 <GhostBar className="h-4 w-2/3" />
401 </div>
402 </div>
403 </section>
404 </div>
405 </div>
406 );
407}
408
409/** People-shaped: an avatar and a name, in the reviewer picker's own row height. */
410export function PullRequestPeopleGhost({ rows = 4 }: { rows?: number }) {
411 return (
412 <div
413 role="status"
414 aria-label="Loading people"
415 className="motion-safe:animate-skeleton space-y-1 p-1"
416 >
417 {Array.from({ length: rows }, (_, index) => (
418 <div key={index} className="flex h-7 items-center gap-2 rounded-md px-2">
419 <GhostBar className="size-4 rounded-full" />
420 <GhostBar className={META_WIDTHS[index % META_WIDTHS.length]} />
421 </div>
422 ))}
423 </div>
424 );
425}
426
427/** The timeline's own shape: dots on the rail, a line and a date to each. */
428export function PullRequestTimelineGhost({ rows = 6 }: { rows?: number }) {
429 return (
430 <div
431 role="status"
432 aria-label="Loading timeline"
433 className="motion-safe:animate-skeleton px-4 py-5"
434 >
435 <div className="relative ml-2 border-l border-border/70 pl-5">
436 {Array.from({ length: rows }, (_, index) => (
437 <div key={index} className="relative pb-5">
438 <GhostBar className="absolute -left-[1.55rem] top-1 size-2 rounded-full" />
439 <GhostBar className={cn("h-3.5", TITLE_WIDTHS[index % TITLE_WIDTHS.length])} />
440 <GhostBar className="mt-1.5 w-16" />
441 </div>
442 ))}
443 </div>
444 </div>
445 );
446}
447
448/** A compact placeholder for the conversation while the core detail is already readable. */
449export function PullRequestConversationGhost({ rows = 3 }: { rows?: number }) {
450 return (
451 <div
452 role="status"
453 aria-label="Loading pull request conversation"
454 className="motion-safe:animate-skeleton space-y-4 py-2"
455 >
456 {Array.from({ length: rows }, (_, index) => (
457 <div key={index} className="flex items-start gap-2">
458 <GhostBar className="size-5 shrink-0 rounded-full" />
459 <div className="flex-1 space-y-1.5">
460 <GhostBar className={META_WIDTHS[index % META_WIDTHS.length]} />
461 <GhostBar className="w-full" />
462 <GhostBar className="w-3/4" />
463 </div>
464 </div>
465 ))}
466 </div>
467 );
468}