Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PullRequestGhosts.tsx

504 lines18.4 KB

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