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