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

PullRequestGhosts.tsx

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