Open History to see the last commit
1import type { PullRequestListEntry, PullRequestSummary, ScopedThreadRef } 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 { msg, T, useGT, useMessages, Var } from "gt-react";
13import type { ReactNode } from "react";
14import {
15 sourceControlClients,
16 type ChangeRequestTerminology,
17} from "@t3tools/client-runtime/source-control-clients";
18
19import { useOpenLink } from "~/browser/useOpenLink";
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";
39import { resolveFilterOptions } from "./pullRequestListOptions";
40
41import { changeRequestTermKind } from "./changeRequestTerm";
42
43export function GhostBar({ className }: { className?: string | undefined }) {
44 return <span aria-hidden className={cn("block h-3 rounded bg-muted-foreground/15", className)} />;
45}
46
47const TITLE_WIDTHS = ["w-3/5", "w-2/5", "w-1/2", "w-2/3", "w-2/5", "w-3/5", "w-1/2"];
48const META_WIDTHS = ["w-2/5", "w-1/3", "w-2/5", "w-1/4", "w-1/3", "w-2/5", "w-1/3"];
49const DEFAULT_DETAIL_TABS = [
50 { value: "summary", label: msg("Summary", { $context: "pull request tab" }) },
51 { value: "timeline", label: msg("Timeline", { $context: "pull request tab" }) },
52 { value: "code", label: msg("Code", { $context: "pull request tab" }) },
53] as const;
54
55export function PullRequestListGhost({ rows = 7, caption }: { rows?: number; caption?: string }) {
56 const gt = useGT();
57 return (
58 <div role="status" aria-label={caption ?? gt("Loading pull requests")} className="space-y-0.5">
59 {/* Outside the pulse so the explanation stays readable. */}
60 {caption ? (
61 <p className="px-3 pb-1 text-xs font-medium text-muted-foreground">{caption}</p>
62 ) : null}
63 <div className="motion-safe:animate-skeleton space-y-0.5">
64 {Array.from({ length: rows }, (_, index) => (
65 <div
66 key={index}
67 className="grid grid-cols-[auto_minmax(0,1fr)_auto] items-center gap-2 rounded-md px-3 py-2.5"
68 >
69 <GhostBar className="size-4 rounded-full" />
70 <div className="min-w-0 space-y-1.5">
71 <GhostBar className={cn("h-4", TITLE_WIDTHS[index % TITLE_WIDTHS.length])} />
72 <GhostBar className={cn("h-3.5", META_WIDTHS[index % META_WIDTHS.length])} />
73 </div>
74 <div className="flex flex-col items-end gap-1.5">
75 <GhostBar className="w-12" />
76 <GhostBar className="w-16" />
77 </div>
78 </div>
79 ))}
80 </div>
81 </div>
82 );
83}
84
85export function PullRequestDetailGhost({
86 seed: entry,
87 summary,
88 actions,
89 checkoutCommand,
90 tabs: requestedTabs,
91 activeTab,
92 number,
93 onBack,
94 onClose,
95 onCheckoutError,
96 threadRef = null,
97}: {
98 seed?: PullRequestListEntry | null;
99 summary?: PullRequestSummary | null;
100 actions?: ReactNode;
101 checkoutCommand?: string | null;
102 tabs?: ReadonlyArray<{ value: string; label: string }>;
103 activeTab?: string;
104 number?: number;
105 onBack?: (() => void) | undefined;
106 onClose?: (() => void) | undefined;
107 onCheckoutError?: ((error: Error) => void) | undefined;
108 threadRef?: ScopedThreadRef | null | undefined;
109}) {
110 const gt = useGT();
111 const resolve = useMessages();
112 const openLink = useOpenLink(threadRef);
113 const tabs = requestedTabs ?? resolveFilterOptions(DEFAULT_DETAIL_TABS, resolve);
114 const seed = summary
115 ? {
116 ...entry,
117 ...summary,
118 isDraft: summary.isDraft ?? entry?.isDraft,
119 }
120 : entry;
121 const terminology = sourceControlClients.get(seed?.provider).changeRequest;
122 const statePresentation = seed
123 ? resolvePullRequestState(
124 {
125 state: seed.state,
126 isDraft: seed.isDraft ?? false,
127 },
128 resolve,
129 )
130 : null;
131 // Passing list rollups can omit workflows awaiting approval; wait for detail.
132 const checksPresentation =
133 seed?.checksState === "failing" || seed?.checksState === "pending"
134 ? pullRequestChecksStatePresentation(seed.checksState, resolve)
135 : null;
136 const checkout = checkoutCommand ?? null;
137 const changedFiles = summary?.changedFiles ?? null;
138 const selectedTab =
139 tabs.find((item) => item.value === activeTab)?.value ?? tabs[0]?.value ?? "summary";
140
141 return (
142 <div
143 role="status"
144 aria-label={gt(
145 "Loading {term, select, mr {merge request} cr {change request} other {pull request}}",
146 { term: changeRequestTermKind(terminology) },
147 )}
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={gt("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={(event) => void openLink(seed.url, { event }).catch(console.error)}
172 onAuxClick={(event) => {
173 if (event.button === 1) void openLink(seed.url, { event }).catch(console.error);
174 }}
175 aria-label={gt(
176 "Open {term, select, mr {merge request} cr {change request} other {pull request}} #{number} on host",
177 {
178 term: changeRequestTermKind(terminology),
179 number: seed.number,
180 },
181 )}
182 >
183 <span className={statePresentation?.toneClassName}>#{seed.number}</span>
184 <ExternalLinkIcon aria-hidden className="size-2.5" />
185 </InlineButton>
186 </>
187 ) : (
188 <>
189 <GhostBar className="w-24" />
190 <span className="shrink-0 text-xs text-muted-foreground">#{number ?? "…"}</span>
191 </>
192 )}
193 </div>
194 </div>
195 <div className="mr-4 flex h-7 shrink-0 items-center justify-end gap-1">
196 {actions ?? <GhostBar className="h-6 w-16 rounded-md" />}
197 <Button
198 size="icon-xs"
199 variant="ghost"
200 disabled
201 aria-label={gt(
202 "{term, select, mr {Merge request} cr {Change request} other {Pull request}} actions loading",
203 {
204 term: changeRequestTermKind(terminology),
205 },
206 )}
207 >
208 <EllipsisIcon aria-hidden className="size-4" />
209 </Button>
210 {onClose ? (
211 <Button
212 size="icon-xs"
213 variant="ghost"
214 aria-label={gt(
215 "Collapse {term, select, mr {merge request} cr {change request} other {pull request}} panel",
216 { term: changeRequestTermKind(terminology) },
217 )}
218 onClick={onClose}
219 >
220 <PanelRightIcon aria-hidden className="size-3.5" />
221 </Button>
222 ) : null}
223 </div>
224
225 <div className="col-span-2 grid grid-rows-[1fr]">
226 <div className="min-h-0 overflow-hidden">
227 <PullRequestDetailHeaderBody
228 title={
229 <PullRequestDetailTitleRow>
230 {seed ? (
231 <h1 className="truncate text-base font-semibold leading-snug">{seed.title}</h1>
232 ) : (
233 <GhostBar className="h-5 w-4/5 max-w-md" />
234 )}
235 </PullRequestDetailTitleRow>
236 }
237 author={
238 seed ? (
239 <PullRequestActorLabel actor={seed.author ?? null} tooltip={false} />
240 ) : (
241 <span className="flex items-center gap-1.5">
242 <GhostBar className="size-4 rounded-full" />
243 <GhostBar className="h-3 w-14" />
244 </span>
245 )
246 }
247 updated={
248 seed ? (
249 <span>
250 <T context="followed by a relative time such as 5m ago or just now">
251 updated{" "}
252 <Var>
253 <RelativeTime timestamp={seed.updatedAt} />
254 </Var>
255 </T>
256 </span>
257 ) : (
258 <GhostBar className="h-3 w-16" />
259 )
260 }
261 checkout={
262 checkout ? (
263 <PullRequestCopyableCode
264 key={checkout}
265 value={checkout}
266 target={{
267 target: `${terminology.singular} checkout command`,
268 unavailableMessage: msg(
269 "Clipboard API is unavailable while copying {term, select, mr {merge request} cr {change request} other {pull request}} checkout command.",
270 { term: changeRequestTermKind(terminology) },
271 ),
272 failureMessage: msg(
273 "Failed to copy {term, select, mr {merge request} cr {change request} other {pull request}} checkout command to the clipboard.",
274 { term: changeRequestTermKind(terminology) },
275 ),
276 }}
277 copyLabel={gt("Copy checkout command")}
278 copiedLabel={gt("Checkout command copied")}
279 className="ml-auto font-mono"
280 tooltipSide="bottom"
281 {...(onCheckoutError ? { onError: onCheckoutError } : {})}
282 />
283 ) : null
284 }
285 base={
286 <span className="inline-flex min-w-0 max-w-[40%] shrink-0 items-center gap-1">
287 {seed ? (
288 <code className="flex min-w-0">
289 <MiddleTruncate value={seed.baseBranch} />
290 </code>
291 ) : (
292 <GhostBar className="h-3 w-12" />
293 )}
294 </span>
295 }
296 head={
297 seed ? (
298 <PullRequestCopyableCode
299 key={seed.headBranch}
300 value={seed.headBranch}
301 target={{
302 target: "branch name",
303 unavailableMessage: msg(
304 "Clipboard API is unavailable while copying branch name.",
305 ),
306 failureMessage: msg("Failed to copy branch name to the clipboard."),
307 }}
308 copyLabel={gt(
309 "Copy {term, select, mr {merge request} cr {change request} other {pull request}} branch",
310 { term: changeRequestTermKind(terminology) },
311 )}
312 copiedLabel={gt("Branch name copied")}
313 className="min-w-0 font-mono"
314 />
315 ) : (
316 <GhostBar className="h-3 w-32 flex-1" />
317 )
318 }
319 files={
320 changedFiles === null ? (
321 <GhostBar className="h-3 w-10" />
322 ) : (
323 gt("{count, plural, one {{formatted} file} other {{formatted} files}}", {
324 count: changedFiles,
325 formatted: changedFiles.toLocaleString(),
326 })
327 )
328 }
329 diffStat={
330 seed ? (
331 <PullRequestDiffStat
332 additions={seed.additions ?? 0}
333 deletions={seed.deletions ?? 0}
334 className="shrink-0 font-mono text-xs"
335 />
336 ) : (
337 <GhostBar className="h-3 w-20" />
338 )
339 }
340 />
341 </div>
342 </div>
343
344 <PullRequestDetailTabBar tabs={tabs} value={selectedTab} inert>
345 {checksPresentation ? (
346 <PullRequestChecksStatusLine
347 className={checksPresentation.toneClassName}
348 icon={<checksPresentation.Icon aria-hidden className="size-3.5" />}
349 label={checksPresentation.label}
350 />
351 ) : (
352 <PullRequestChecksStatusLine
353 icon={<GhostBar className="my-px size-3.5 rounded-full" />}
354 label={<GhostBar className="w-28" />}
355 />
356 )}
357 </PullRequestDetailTabBar>
358 </div>
359
360 <div
361 tabIndex={-1}
362 data-workspace-focus-default
363 className="min-h-0 flex-1 overflow-hidden outline-none"
364 >
365 <section className="px-4 pt-2.5 pb-1">
366 <div className="space-y-2">
367 <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">
368 <span className="flex items-center gap-1.5 text-muted-foreground">
369 <UsersIcon aria-hidden className="size-3.5" />
370 <T>Reviewers</T>
371 </span>
372 <span className="flex min-w-0 items-center gap-1.5 text-foreground">
373 <GhostBar className="h-3 w-10" />
374 <Button
375 size="icon-xs"
376 variant="ghost"
377 disabled
378 aria-label={gt("Reviewers loading")}
379 >
380 <UserPlusIcon aria-hidden className="size-3.5" />
381 </Button>
382 </span>
383 </div>
384 <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">
385 <span className="flex items-center gap-1.5 text-muted-foreground">
386 <TagIcon aria-hidden className="size-3.5" />
387 <T>Labels</T>
388 </span>
389 <span className="flex min-w-0 flex-wrap items-center gap-1 text-foreground">
390 {entry?.labels ? (
391 entry.labels.length > 0 ? (
392 entry.labels.map((label) => (
393 <PullRequestLabelChip
394 key={label.name}
395 label={label}
396 size="default"
397 className="max-w-48"
398 />
399 ))
400 ) : (
401 <span className="text-muted-foreground">
402 <T context="no labels">None</T>
403 </span>
404 )
405 ) : (
406 <>
407 <GhostBar className="h-4.5 w-24" />
408 <GhostBar className="h-4.5 w-20" />
409 </>
410 )}
411 <Button size="icon-xs" variant="ghost" disabled aria-label={gt("Labels loading")}>
412 <TagIcon aria-hidden className="size-3.5" />
413 </Button>
414 </span>
415 </div>
416 </div>
417 </section>
418
419 <section>
420 <div className="sticky top-0 z-10 flex w-full items-center bg-background pr-4">
421 <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">
422 <span>
423 <T>Description</T>
424 </span>
425 <ChevronRightIcon
426 aria-hidden
427 className="size-3.5 rotate-90 text-muted-foreground/60"
428 />
429 </div>
430 </div>
431 <div className="space-y-3 px-4 pb-4">
432 <GhostBar className="h-5 w-40" />
433 <div className="space-y-2">
434 <GhostBar className="h-4 w-full" />
435 <GhostBar className="h-4 w-11/12" />
436 <GhostBar className="h-4 w-4/5" />
437 <GhostBar className="h-4 w-2/3" />
438 </div>
439 </div>
440 </section>
441 </div>
442 </div>
443 );
444}
445
446export function PullRequestPeopleGhost({ rows = 4 }: { rows?: number }) {
447 const gt = useGT();
448 return (
449 <div
450 role="status"
451 aria-label={gt("Loading people")}
452 className="motion-safe:animate-skeleton space-y-1 p-1"
453 >
454 {Array.from({ length: rows }, (_, index) => (
455 <div key={index} className="flex h-7 items-center gap-2 rounded-md px-2">
456 <GhostBar className="size-4 rounded-full" />
457 <GhostBar className={META_WIDTHS[index % META_WIDTHS.length]} />
458 </div>
459 ))}
460 </div>
461 );
462}
463
464export function PullRequestTimelineGhost({ rows = 6 }: { rows?: number }) {
465 const gt = useGT();
466 return (
467 <div
468 role="status"
469 aria-label={gt("Loading timeline")}
470 className="motion-safe:animate-skeleton px-4 py-5"
471 >
472 <div className="relative ml-2 border-l border-border/70 pl-5">
473 {Array.from({ length: rows }, (_, index) => (
474 <div key={index} className="relative pb-5">
475 <GhostBar className="absolute -left-[1.55rem] top-1 size-2 rounded-full" />
476 <GhostBar className={cn("h-3.5", TITLE_WIDTHS[index % TITLE_WIDTHS.length])} />
477 <GhostBar className="mt-1.5 w-16" />
478 </div>
479 ))}
480 </div>
481 </div>
482 );
483}
484
485export function PullRequestConversationGhost({
486 rows = 3,
487 terminology,
488}: {
489 rows?: number;
490 terminology: ChangeRequestTerminology;
491}) {
492 const gt = useGT();
493 return (
494 <div
495 role="status"
496 aria-label={gt(
497 "Loading {term, select, mr {merge request} cr {change request} other {pull request}} conversation",
498 { term: changeRequestTermKind(terminology) },
499 )}
500 className="motion-safe:animate-skeleton space-y-4 py-2"
501 >
502 {Array.from({ length: rows }, (_, index) => (
503 <div key={index} className="flex items-start gap-2">
504 <GhostBar className="size-5 shrink-0 rounded-full" />
505 <div className="flex-1 space-y-1.5">
506 <GhostBar className={META_WIDTHS[index % META_WIDTHS.length]} />
507 <GhostBar className="w-full" />
508 <GhostBar className="w-3/4" />
509 </div>
510 </div>
511 ))}
512 </div>
513 );
514}