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