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, select, plural, part } 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({ context: "pull request tab" })`Summary` },
51 { value: "timeline", label: msg({ context: "pull request tab" })`Timeline` },
52 { value: "code", label: msg({ context: "pull request tab" })`Code` },
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`Loading ${select(
145 { term: changeRequestTermKind(terminology) },
146 {
147 mr: "merge request",
148 cr: "change request",
149 other: "pull request",
150 },
151 )}`}
152 className={cn(
153 "flex h-full min-h-0 flex-col overflow-hidden bg-background",
154 !seed && "motion-safe:animate-skeleton",
155 )}
156 >
157 <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">
158 <div className="pl-4 grid h-7 min-w-0 items-center overflow-hidden">
159 <div className="flex min-w-0 items-center gap-1 text-sm text-muted-foreground sm:text-xs">
160 {onBack ? (
161 <Button
162 size="icon-micro"
163 variant="ghost-muted"
164 className="-ml-1.5"
165 onClick={onBack}
166 aria-label={gt`Back to this thread's pull requests`}
167 >
168 <ArrowLeftIcon aria-hidden className="size-3.5" />
169 </Button>
170 ) : null}
171 {seed ? (
172 <>
173 <span className="min-w-0 truncate font-medium">{seed.repository}</span>
174 <InlineButton
175 onClick={(event) => void openLink(seed.url, { event }).catch(console.error)}
176 onAuxClick={(event) => {
177 if (event.button === 1) void openLink(seed.url, { event }).catch(console.error);
178 }}
179 aria-label={gt`Open ${select(
180 { term: changeRequestTermKind(terminology) },
181 {
182 mr: "merge request",
183 cr: "change request",
184 other: "pull request",
185 },
186 )} #${{ number: seed.number }} on host`}
187 >
188 <span className={statePresentation?.toneClassName}>#{seed.number}</span>
189 <ExternalLinkIcon aria-hidden className="size-2.5" />
190 </InlineButton>
191 </>
192 ) : (
193 <>
194 <GhostBar className="w-24" />
195 <span className="shrink-0 text-xs text-muted-foreground">#{number ?? "…"}</span>
196 </>
197 )}
198 </div>
199 </div>
200 <div className="mr-4 flex h-7 shrink-0 items-center justify-end gap-1">
201 {actions ?? <GhostBar className="h-6 w-16 rounded-md" />}
202 <Button
203 size="icon-xs"
204 variant="ghost"
205 disabled
206 aria-label={gt`${select(
207 { term: changeRequestTermKind(terminology) },
208 {
209 mr: "Merge request",
210 cr: "Change request",
211 other: "Pull request",
212 },
213 )} actions loading`}
214 >
215 <EllipsisIcon aria-hidden className="size-4" />
216 </Button>
217 {onClose ? (
218 <Button
219 size="icon-xs"
220 variant="ghost"
221 aria-label={gt`Collapse ${select(
222 { term: changeRequestTermKind(terminology) },
223 {
224 mr: "merge request",
225 cr: "change request",
226 other: "pull request",
227 },
228 )} panel`}
229 onClick={onClose}
230 >
231 <PanelRightIcon aria-hidden className="size-3.5" />
232 </Button>
233 ) : null}
234 </div>
235
236 <div className="col-span-2 grid grid-rows-[1fr]">
237 <div className="min-h-0 overflow-hidden">
238 <PullRequestDetailHeaderBody
239 title={
240 <PullRequestDetailTitleRow>
241 {seed ? (
242 <h1 className="truncate text-base font-semibold leading-snug">{seed.title}</h1>
243 ) : (
244 <GhostBar className="h-5 w-4/5 max-w-md" />
245 )}
246 </PullRequestDetailTitleRow>
247 }
248 author={
249 seed ? (
250 <PullRequestActorLabel actor={seed.author ?? null} tooltip={false} />
251 ) : (
252 <span className="flex items-center gap-1.5">
253 <GhostBar className="size-4 rounded-full" />
254 <GhostBar className="h-3 w-14" />
255 </span>
256 )
257 }
258 updated={
259 seed ? (
260 <span>
261 <T context="followed by a relative time such as 5m ago or just now">
262 updated{" "}
263 <Var>
264 <RelativeTime timestamp={seed.updatedAt} />
265 </Var>
266 </T>
267 </span>
268 ) : (
269 <GhostBar className="h-3 w-16" />
270 )
271 }
272 checkout={
273 checkout ? (
274 <PullRequestCopyableCode
275 key={checkout}
276 value={checkout}
277 target={{
278 target: `${terminology.singular} checkout command`,
279 unavailableMessage: msg`Clipboard API is unavailable while copying ${select(
280 { term: changeRequestTermKind(terminology) },
281 {
282 mr: "merge request",
283 cr: "change request",
284 other: "pull request",
285 },
286 )} checkout command.`,
287 failureMessage: msg`Failed to copy ${select(
288 { term: changeRequestTermKind(terminology) },
289 {
290 mr: "merge request",
291 cr: "change request",
292 other: "pull request",
293 },
294 )} checkout command to the clipboard.`,
295 }}
296 copyLabel={gt`Copy checkout command`}
297 copiedLabel={gt`Checkout command copied`}
298 className="ml-auto font-mono"
299 tooltipSide="bottom"
300 {...(onCheckoutError ? { onError: onCheckoutError } : {})}
301 />
302 ) : null
303 }
304 base={
305 <span className="inline-flex min-w-0 max-w-[40%] shrink-0 items-center gap-1">
306 {seed ? (
307 <code className="flex min-w-0">
308 <MiddleTruncate value={seed.baseBranch} />
309 </code>
310 ) : (
311 <GhostBar className="h-3 w-12" />
312 )}
313 </span>
314 }
315 head={
316 seed ? (
317 <PullRequestCopyableCode
318 key={seed.headBranch}
319 value={seed.headBranch}
320 target={{
321 target: "branch name",
322 unavailableMessage: msg(
323 "Clipboard API is unavailable while copying branch name.",
324 ),
325 failureMessage: msg("Failed to copy branch name to the clipboard."),
326 }}
327 copyLabel={gt`Copy ${select(
328 { term: changeRequestTermKind(terminology) },
329 {
330 mr: "merge request",
331 cr: "change request",
332 other: "pull request",
333 },
334 )} branch`}
335 copiedLabel={gt`Branch name copied`}
336 className="min-w-0 font-mono"
337 />
338 ) : (
339 <GhostBar className="h-3 w-32 flex-1" />
340 )
341 }
342 files={
343 changedFiles === null ? (
344 <GhostBar className="h-3 w-10" />
345 ) : (
346 gt`${plural(
347 { count: changedFiles },
348 {
349 one: part`${{ formatted: changedFiles.toLocaleString() }} file`,
350 other: part`${{ formatted: changedFiles.toLocaleString() }} files`,
351 },
352 )}`
353 )
354 }
355 diffStat={
356 seed ? (
357 <PullRequestDiffStat
358 additions={seed.additions ?? 0}
359 deletions={seed.deletions ?? 0}
360 className="shrink-0 font-mono text-xs"
361 />
362 ) : (
363 <GhostBar className="h-3 w-20" />
364 )
365 }
366 />
367 </div>
368 </div>
369
370 <PullRequestDetailTabBar tabs={tabs} value={selectedTab} inert>
371 {checksPresentation ? (
372 <PullRequestChecksStatusLine
373 className={checksPresentation.toneClassName}
374 icon={<checksPresentation.Icon aria-hidden className="size-3.5" />}
375 label={checksPresentation.label}
376 />
377 ) : (
378 <PullRequestChecksStatusLine
379 icon={<GhostBar className="my-px size-3.5 rounded-full" />}
380 label={<GhostBar className="w-28" />}
381 />
382 )}
383 </PullRequestDetailTabBar>
384 </div>
385
386 <div
387 tabIndex={-1}
388 data-workspace-focus-default
389 className="min-h-0 flex-1 overflow-hidden outline-none"
390 >
391 <section className="px-4 pt-2.5 pb-1">
392 <div className="space-y-2">
393 <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">
394 <span className="flex items-center gap-1.5 text-muted-foreground">
395 <UsersIcon aria-hidden className="size-3.5" />
396 <T>Reviewers</T>
397 </span>
398 <span className="flex min-w-0 items-center gap-1.5 text-foreground">
399 <GhostBar className="h-3 w-10" />
400 <Button size="icon-xs" variant="ghost" disabled aria-label={gt`Reviewers loading`}>
401 <UserPlusIcon aria-hidden className="size-3.5" />
402 </Button>
403 </span>
404 </div>
405 <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">
406 <span className="flex items-center gap-1.5 text-muted-foreground">
407 <TagIcon aria-hidden className="size-3.5" />
408 <T>Labels</T>
409 </span>
410 <span className="flex min-w-0 flex-wrap items-center gap-1 text-foreground">
411 {entry?.labels ? (
412 entry.labels.length > 0 ? (
413 entry.labels.map((label) => (
414 <PullRequestLabelChip
415 key={label.name}
416 label={label}
417 size="default"
418 className="max-w-48"
419 />
420 ))
421 ) : (
422 <span className="text-muted-foreground">
423 <T context="no labels">None</T>
424 </span>
425 )
426 ) : (
427 <>
428 <GhostBar className="h-4.5 w-24" />
429 <GhostBar className="h-4.5 w-20" />
430 </>
431 )}
432 <Button size="icon-xs" variant="ghost" disabled aria-label={gt`Labels loading`}>
433 <TagIcon aria-hidden className="size-3.5" />
434 </Button>
435 </span>
436 </div>
437 </div>
438 </section>
439
440 <section>
441 <div className="sticky top-0 z-10 flex w-full items-center bg-background pr-4">
442 <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">
443 <span>
444 <T>Description</T>
445 </span>
446 <ChevronRightIcon
447 aria-hidden
448 className="size-3.5 rotate-90 text-muted-foreground/60"
449 />
450 </div>
451 </div>
452 <div className="space-y-3 px-4 pb-4">
453 <GhostBar className="h-5 w-40" />
454 <div className="space-y-2">
455 <GhostBar className="h-4 w-full" />
456 <GhostBar className="h-4 w-11/12" />
457 <GhostBar className="h-4 w-4/5" />
458 <GhostBar className="h-4 w-2/3" />
459 </div>
460 </div>
461 </section>
462 </div>
463 </div>
464 );
465}
466
467export function PullRequestPeopleGhost({ rows = 4 }: { rows?: number }) {
468 const gt = useGT();
469 return (
470 <div
471 role="status"
472 aria-label={gt`Loading people`}
473 className="motion-safe:animate-skeleton space-y-1 p-1"
474 >
475 {Array.from({ length: rows }, (_, index) => (
476 <div key={index} className="flex h-7 items-center gap-2 rounded-md px-2">
477 <GhostBar className="size-4 rounded-full" />
478 <GhostBar className={META_WIDTHS[index % META_WIDTHS.length]} />
479 </div>
480 ))}
481 </div>
482 );
483}
484
485export function PullRequestTimelineGhost({ rows = 6 }: { rows?: number }) {
486 const gt = useGT();
487 return (
488 <div
489 role="status"
490 aria-label={gt`Loading timeline`}
491 className="motion-safe:animate-skeleton px-4 py-5"
492 >
493 <div className="relative ml-2 border-l border-border/70 pl-5">
494 {Array.from({ length: rows }, (_, index) => (
495 <div key={index} className="relative pb-5">
496 <GhostBar className="absolute -left-[1.55rem] top-1 size-2 rounded-full" />
497 <GhostBar className={cn("h-3.5", TITLE_WIDTHS[index % TITLE_WIDTHS.length])} />
498 <GhostBar className="mt-1.5 w-16" />
499 </div>
500 ))}
501 </div>
502 </div>
503 );
504}
505
506export function PullRequestConversationGhost({
507 rows = 3,
508 terminology,
509}: {
510 rows?: number;
511 terminology: ChangeRequestTerminology;
512}) {
513 const gt = useGT();
514 return (
515 <div
516 role="status"
517 aria-label={gt`Loading ${select(
518 { term: changeRequestTermKind(terminology) },
519 {
520 mr: "merge request",
521 cr: "change request",
522 other: "pull request",
523 },
524 )} conversation`}
525 className="motion-safe:animate-skeleton space-y-4 py-2"
526 >
527 {Array.from({ length: rows }, (_, index) => (
528 <div key={index} className="flex items-start gap-2">
529 <GhostBar className="size-5 shrink-0 rounded-full" />
530 <div className="flex-1 space-y-1.5">
531 <GhostBar className={META_WIDTHS[index % META_WIDTHS.length]} />
532 <GhostBar className="w-full" />
533 <GhostBar className="w-3/4" />
534 </div>
535 </div>
536 ))}
537 </div>
538 );
539}