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