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

PullRequestGhosts.tsx

437 lines15.5 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 type { ReactNode } from "react";
13import {
14 capitalizeTerm,
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";
39​
40function GhostBar({ className }: { className?: string | undefined }) {
41 return <span aria-hidden className={cn("block h-3 rounded bg-muted-foreground/15", className)} />;
43​
44const TITLE_WIDTHS = ["w-3/5", "w-2/5", "w-1/2", "w-2/3", "w-2/5", "w-3/5", "w-1/2"];
45const META_WIDTHS = ["w-2/5", "w-1/3", "w-2/5", "w-1/4", "w-1/3", "w-2/5", "w-1/3"];
46const DEFAULT_DETAIL_TABS = [
47 { value: "summary", label: "Summary" },
48 { value: "timeline", label: "Timeline" },
49 { value: "code", label: "Code" },
50] as const;
51​
52export function PullRequestListGhost({ rows = 7, caption }: { rows?: number; caption?: string }) {
53 return (
54 <div role="status" aria-label={caption ?? "Loading pull requests"} className="space-y-0.5">
55 {/* Outside the pulse so the explanation stays readable. */}
56 {caption ? (
57 <p className="px-3 pb-1 text-xs font-medium text-muted-foreground/70 redesign:text-muted-foreground">
58 {caption}
59 </p>
60 ) : null}
61 <div className="motion-safe:animate-skeleton space-y-0.5">
62 {Array.from({ length: rows }, (_, index) => (
63 <div
64 key={index}
65 className="grid grid-cols-[auto_minmax(0,1fr)_auto] items-center gap-2 rounded-md px-3 py-2.5"
66 >
67 <GhostBar className="size-4 rounded-full" />
68 <div className="min-w-0 space-y-1.5">
69 <GhostBar className={cn("h-4", TITLE_WIDTHS[index % TITLE_WIDTHS.length])} />
70 <GhostBar className={cn("h-3.5", META_WIDTHS[index % META_WIDTHS.length])} />
71 </div>
72 <div className="flex flex-col items-end gap-1.5">
73 <GhostBar className="w-12" />
74 <GhostBar className="w-16" />
75 </div>
76 </div>
77 ))}
78 </div>
79 </div>
80 );
82​
83export function PullRequestDetailGhost({
84 seed: entry,
85 summary,
86 actions,
87 checkoutCommand,
88 tabs = DEFAULT_DETAIL_TABS,
89 activeTab,
90 number,
91 onBack,
92 onClose,
93 onCheckoutError,
94}: {
95 seed?: PullRequestListEntry | null;
96 summary?: PullRequestSummary | null;
97 actions?: ReactNode;
98 checkoutCommand?: string | null;
99 tabs?: ReadonlyArray<{ value: string; label: string }>;
100 activeTab?: string;
101 number?: number;
102 onBack?: (() => void) | undefined;
103 onClose?: (() => void) | undefined;
104 onCheckoutError?: ((error: Error) => void) | undefined;
105}) {
106 const seed = summary
107 ? {
108 ...entry,
109 ...summary,
110 isDraft: summary.isDraft ?? entry?.isDraft,
111 }
112 : entry;
113 const terminology = sourceControlClients.get(seed?.provider).changeRequest;
114 const statePresentation = seed
115 ? resolvePullRequestState({
116 state: seed.state,
117 isDraft: seed.isDraft ?? false,
118 })
119 : null;
120 // Passing list rollups can omit workflows awaiting approval; wait for detail.
121 const checksPresentation =
122 seed?.checksState === "failing" || seed?.checksState === "pending"
123 ? pullRequestChecksStatePresentation(seed.checksState)
124 : null;
125 const checkout = checkoutCommand ?? null;
126 const changedFiles = summary?.changedFiles ?? null;
127 const selectedTab =
128 tabs.find((item) => item.value === activeTab)?.value ?? tabs[0]?.value ?? "summary";
129​
130 return (
131 <div
132 role="status"
133 aria-label={`Loading ${terminology.singular}`}
134 className={cn(
135 "flex h-full min-h-0 flex-col overflow-hidden bg-background",
136 !seed && "motion-safe:animate-skeleton",
137 )}
138 >
139 <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">
140 <div className="pl-4 grid h-7 min-w-0 items-center overflow-hidden">
141 <div className="flex min-w-0 items-center gap-1 text-sm text-muted-foreground sm:text-xs">
142 {onBack ? (
143 <Button
144 size="icon-micro"
145 variant="ghost-muted"
146 className="-ml-1.5"
147 onClick={onBack}
148 aria-label="Back to this thread's pull requests"
149 >
150 <ArrowLeftIcon aria-hidden className="size-3.5" />
151 </Button>
152 ) : null}
153 {seed ? (
154 <>
155 <span className="min-w-0 truncate font-medium">{seed.repository}</span>
156 <InlineButton
157 onClick={() => void readLocalApi()?.shell.openExternal(seed.url)}
158 aria-label={`Open ${terminology.singular} #${seed.number} on host`}
159 >
160 <span className={statePresentation?.toneClassName}>#{seed.number}</span>
161 <ExternalLinkIcon aria-hidden className="size-2.5" />
162 </InlineButton>
163 </>
164 ) : (
165 <>
166 <GhostBar className="w-24" />
167 <span className="shrink-0 text-xs text-muted-foreground">#{number ?? "…"}</span>
168 </>
169 )}
170 </div>
171 </div>
172 <div className="mr-4 flex h-7 shrink-0 items-center justify-end gap-1">
173 {actions ?? <GhostBar className="h-6 w-16 rounded-md" />}
174 <Button
175 size="icon-xs"
176 variant="ghost"
177 disabled
178 aria-label={`${capitalizeTerm(terminology.singular)} actions loading`}
179 >
180 <EllipsisIcon aria-hidden className="size-4" />
181 </Button>
182 {onClose ? (
183 <Button
184 size="icon-xs"
185 variant="ghost"
186 aria-label={`Collapse ${terminology.singular} panel`}
187 onClick={onClose}
188 >
189 <PanelRightIcon aria-hidden className="size-3.5" />
190 </Button>
191 ) : null}
192 </div>
193​
194 <div className="col-span-2 grid grid-rows-[1fr]">
195 <div className="min-h-0 overflow-hidden">
196 <PullRequestDetailHeaderBody
197 title={
198 <PullRequestDetailTitleRow>
199 {seed ? (
200 <h1 className="truncate text-base font-semibold leading-snug">{seed.title}</h1>
201 ) : (
202 <GhostBar className="h-5 w-4/5 max-w-md" />
203 )}
204 </PullRequestDetailTitleRow>
205 }
206 author={
207 seed ? (
208 <PullRequestActorLabel actor={seed.author ?? null} tooltip={false} />
209 ) : (
210 <span className="flex items-center gap-1.5">
211 <GhostBar className="size-4 rounded-full" />
212 <GhostBar className="h-3 w-14" />
213 </span>
214 )
215 }
216 updated={
217 seed ? (
218 <span>
219 updated <RelativeTime timestamp={seed.updatedAt} />
220 </span>
221 ) : (
222 <GhostBar className="h-3 w-16" />
223 )
224 }
225 checkout={
226 checkout ? (
227 <PullRequestCopyableCode
228 key={checkout}
229 value={checkout}
230 target={`${terminology.singular} checkout command`}
231 copyLabel="Copy checkout command"
232 copiedLabel="Checkout command copied"
233 className="ml-auto font-mono"
234 tooltipSide="bottom"
235 {...(onCheckoutError ? { onError: onCheckoutError } : {})}
236 />
237 ) : null
238 }
239 base={
240 <span className="inline-flex min-w-0 max-w-[40%] shrink-0 items-center gap-1">
241 {seed ? (
242 <code className="flex min-w-0">
243 <MiddleTruncate value={seed.baseBranch} />
244 </code>
245 ) : (
246 <GhostBar className="h-3 w-12" />
247 )}
248 </span>
249 }
250 head={
251 seed ? (
252 <PullRequestCopyableCode
253 key={seed.headBranch}
254 value={seed.headBranch}
255 target="branch name"
256 copyLabel={`Copy ${terminology.singular} branch`}
257 copiedLabel="Branch name copied"
258 className="min-w-0 font-mono"
259 />
260 ) : (
261 <GhostBar className="h-3 w-32 flex-1" />
262 )
263 }
264 files={
265 changedFiles === null ? (
266 <GhostBar className="h-3 w-10" />
267 ) : (
268 `${changedFiles.toLocaleString()} ${changedFiles === 1 ? "file" : "files"}`
269 )
270 }
271 diffStat={
272 seed ? (
273 <PullRequestDiffStat
274 additions={seed.additions ?? 0}
275 deletions={seed.deletions ?? 0}
276 className="shrink-0 font-mono text-xs"
277 />
278 ) : (
279 <GhostBar className="h-3 w-20" />
280 )
281 }
282 />
283 </div>
284 </div>
285​
286 <PullRequestDetailTabBar tabs={tabs} value={selectedTab} inert>
287 {checksPresentation ? (
288 <PullRequestChecksStatusLine
289 className={checksPresentation.toneClassName}
290 icon={<checksPresentation.Icon aria-hidden className="size-3.5" />}
291 label={checksPresentation.label}
292 />
293 ) : (
294 <PullRequestChecksStatusLine
295 icon={<GhostBar className="my-px size-3.5 rounded-full" />}
296 label={<GhostBar className="w-28" />}
297 />
298 )}
299 </PullRequestDetailTabBar>
300 </div>
301​
302 <div className="min-h-0 flex-1 overflow-hidden">
303 <section className="px-4 pt-2.5 pb-1">
304 <div className="space-y-2">
305 <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">
306 <span className="flex items-center gap-1.5 text-muted-foreground">
307 <UsersIcon aria-hidden className="size-3.5" />
308 Reviewers
309 </span>
310 <span className="flex min-w-0 items-center gap-1.5 text-foreground">
311 <GhostBar className="h-3 w-10" />
312 <Button size="icon-xs" variant="ghost" disabled aria-label="Reviewers loading">
313 <UserPlusIcon aria-hidden className="size-3.5" />
314 </Button>
315 </span>
316 </div>
317 <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">
318 <span className="flex items-center gap-1.5 text-muted-foreground">
319 <TagIcon aria-hidden className="size-3.5" />
320 Labels
321 </span>
322 <span className="flex min-w-0 flex-wrap items-center gap-1 text-foreground">
323 {entry?.labels ? (
324 entry.labels.length > 0 ? (
325 entry.labels.map((label) => (
326 <PullRequestLabelChip
327 key={label.name}
328 label={label}
329 size="default"
330 className="max-w-48"
331 />
332 ))
333 ) : (
334 <span className="text-muted-foreground">None</span>
335 )
336 ) : (
337 <>
338 <GhostBar className="h-4.5 w-24" />
339 <GhostBar className="h-4.5 w-20" />
340 </>
341 )}
342 <Button size="icon-xs" variant="ghost" disabled aria-label="Labels loading">
343 <TagIcon aria-hidden className="size-3.5" />
344 </Button>
345 </span>
346 </div>
347 </div>
348 </section>
349​
350 <section>
351 <div className="sticky top-0 z-10 flex w-full items-center bg-background pr-4">
352 <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">
353 <span>Description</span>
354 <ChevronRightIcon
355 aria-hidden
356 className="size-3.5 rotate-90 text-muted-foreground/60"
357 />
358 </div>
359 </div>
360 <div className="space-y-3 px-4 pb-4">
361 <GhostBar className="h-5 w-40" />
362 <div className="space-y-2">
363 <GhostBar className="h-4 w-full" />
364 <GhostBar className="h-4 w-11/12" />
365 <GhostBar className="h-4 w-4/5" />
366 <GhostBar className="h-4 w-2/3" />
367 </div>
368 </div>
369 </section>
370 </div>
371 </div>
372 );
374​
375export function PullRequestPeopleGhost({ rows = 4 }: { rows?: number }) {
376 return (
377 <div
378 role="status"
379 aria-label="Loading people"
380 className="motion-safe:animate-skeleton space-y-1 p-1"
381 >
382 {Array.from({ length: rows }, (_, index) => (
383 <div key={index} className="flex h-7 items-center gap-2 rounded-md px-2">
384 <GhostBar className="size-4 rounded-full" />
385 <GhostBar className={META_WIDTHS[index % META_WIDTHS.length]} />
386 </div>
387 ))}
388 </div>
389 );
391​
392export function PullRequestTimelineGhost({ rows = 6 }: { rows?: number }) {
393 return (
394 <div
395 role="status"
396 aria-label="Loading timeline"
397 className="motion-safe:animate-skeleton px-4 py-5"
398 >
399 <div className="relative ml-2 border-l border-border/70 pl-5">
400 {Array.from({ length: rows }, (_, index) => (
401 <div key={index} className="relative pb-5">
402 <GhostBar className="absolute -left-[1.55rem] top-1 size-2 rounded-full" />
403 <GhostBar className={cn("h-3.5", TITLE_WIDTHS[index % TITLE_WIDTHS.length])} />
404 <GhostBar className="mt-1.5 w-16" />
405 </div>
406 ))}
407 </div>
408 </div>
409 );
411​
412export function PullRequestConversationGhost({
413 rows = 3,
414 terminology,
415}: {
416 rows?: number;
417 terminology: ChangeRequestTerminology;
418}) {
419 return (
420 <div
421 role="status"
422 aria-label={`Loading ${terminology.singular} conversation`}
423 className="motion-safe:animate-skeleton space-y-4 py-2"
424 >
425 {Array.from({ length: rows }, (_, index) => (
426 <div key={index} className="flex items-start gap-2">
427 <GhostBar className="size-5 shrink-0 rounded-full" />
428 <div className="flex-1 space-y-1.5">
429 <GhostBar className={META_WIDTHS[index % META_WIDTHS.length]} />
430 <GhostBar className="w-full" />
431 <GhostBar className="w-3/4" />
432 </div>
433 </div>
434 ))}
435 </div>
436 );