Repository navigation
Expand file tree
/
Copy pathpullRequestPresentation.tsx
More file actions
566 lines (535 loc) · 17 KB
/
Copy pathpullRequestPresentation.tsx
File metadata and controls
566 lines (535 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import { Spinner } from "~/components/ui/spinner";
import type {
PullRequestActor,
PullRequestCheck,
PullRequestCheckStatus,
PullRequestChecksState,
PullRequestLabel,
PullRequestMergeability,
PullRequestReviewDecision,
PullRequestState,
} from "@t3tools/contracts";
import { formatDiffCount } from "@t3tools/client-runtime/diff-count";
import {
CircleCheckIcon,
CircleDashedIcon,
CircleDotIcon,
CircleSlashIcon,
CircleXIcon,
UserCheckIcon,
UserRoundIcon,
UserRoundXIcon,
} from "lucide-react";
import { Children, type CSSProperties, isValidElement, type ReactNode, useState } from "react";
import { cn } from "~/lib/utils";
import { Badge } from "../ui/badge";
import { InlineButton } from "../ui/button";
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
import type { PullRequestReviewOutcome } from "./pullRequestDetail.logic";
import { pullRequestLabelColor } from "./pullRequestList.logic";
import {
PULL_REQUEST_STATE_PRESENTATION,
PullRequestGlyph,
type PullRequestStatePresentation,
type PullRequestGlyphIcon,
} from "./pullRequestIcons";
export function PullRequestLabelChip({
label,
size = "sm",
className,
focusable = false,
children,
}: {
label: Pick<PullRequestLabel, "name" | "color" | "description">;
size?: "sm" | "default";
className?: string;
focusable?: boolean;
children?: ReactNode;
}) {
const color = pullRequestLabelColor(label.color);
const badge = (
<Badge
size={size}
variant={color ? "label" : "secondary"}
className={cn("min-w-0 max-w-40 shrink justify-start", className)}
{...(focusable && label.description ? { tabIndex: 0 } : {})}
{...(color ? { style: { "--label": color } as CSSProperties } : {})}
>
<span className="truncate">{label.name}</span>
{label.description ? <span className="sr-only">: {label.description}</span> : null}
{children}
</Badge>
);
if (!label.description) return badge;
return (
<Tooltip>
<TooltipTrigger render={badge} />
<TooltipPopup>{label.description}</TooltipPopup>
</Tooltip>
);
}
function reviewDecisionPresentation(decision: PullRequestReviewDecision) {
switch (decision) {
case "approved":
return {
Icon: UserCheckIcon,
label: "Approved",
toneClassName: CHECK_STATUS_PRESENTATION.success.toneClassName,
};
case "changes-requested":
return {
Icon: UserRoundXIcon,
label: "Changes requested",
toneClassName: "text-amber-600/90 dark:text-amber-400/80",
};
case "review-required":
return {
Icon: UserRoundIcon,
label: "Awaiting review",
toneClassName: "text-muted-foreground/60",
};
}
}
export function PullRequestReviewDecisionGlyph({
decision,
}: {
decision: PullRequestReviewDecision;
}) {
const presentation = reviewDecisionPresentation(decision);
return (
<Tooltip>
<TooltipTrigger render={<span className="inline-flex shrink-0" />}>
<presentation.Icon aria-hidden className={cn("size-3.5", presentation.toneClassName)} />
<span className="sr-only">{presentation.label}</span>
</TooltipTrigger>
<TooltipPopup>{presentation.label}</TooltipPopup>
</Tooltip>
);
}
export function resolvePullRequestState(input: {
readonly state: PullRequestState;
readonly isDraft: boolean;
}): PullRequestStatePresentation {
const key = input.state === "open" && input.isDraft ? "draft" : input.state;
return PULL_REQUEST_STATE_PRESENTATION[key];
}
export interface PullRequestConflictPresentation {
readonly label: string;
readonly toneClassName: string;
readonly Icon: PullRequestGlyphIcon;
}
function resolvePullRequestConflict(input: {
readonly state: PullRequestState;
readonly isDraft: boolean;
readonly mergeability?: PullRequestMergeability;
readonly baseBranch?: string;
}): PullRequestConflictPresentation | null {
if (input.state !== "open" || input.isDraft || input.mergeability !== "conflicting") {
return null;
}
return {
label: input.baseBranch ? `Conflicts with ${input.baseBranch}` : "Has conflicts",
toneClassName: "text-destructive",
Icon: PullRequestGlyph.conflicting,
};
}
export function PullRequestStateGlyph({
state,
isDraft,
className,
}: {
state: PullRequestState;
isDraft: boolean;
className?: string;
}) {
const presentation = resolvePullRequestState({ state, isDraft });
return (
<Tooltip>
<TooltipTrigger render={<span className="inline-flex shrink-0" />}>
<presentation.Icon
role="img"
aria-label={presentation.label}
className={cn("size-4 shrink-0", presentation.toneClassName, className)}
/>
</TooltipTrigger>
<TooltipPopup>{presentation.label}</TooltipPopup>
</Tooltip>
);
}
export function PullRequestConflictGlyph({
state,
isDraft,
mergeability,
baseBranch,
className,
}: {
state: PullRequestState;
isDraft: boolean;
mergeability?: PullRequestMergeability;
baseBranch?: string;
className?: string;
}) {
const presentation = resolvePullRequestConflict({
state,
isDraft,
...(mergeability === undefined ? {} : { mergeability }),
...(baseBranch === undefined ? {} : { baseBranch }),
});
if (presentation === null) return null;
return (
<Tooltip>
<TooltipTrigger render={<span className="inline-flex shrink-0" />}>
<presentation.Icon
role="img"
aria-label={presentation.label}
className={cn("size-4 shrink-0", presentation.toneClassName, className)}
/>
</TooltipTrigger>
<TooltipPopup>{presentation.label}</TooltipPopup>
</Tooltip>
);
}
const CHECK_STATUS_PRESENTATION = {
pending: { label: "Running", Icon: Spinner, toneClassName: "text-amber-500" },
"action-required": {
label: "Awaiting action",
Icon: CircleDotIcon,
toneClassName: "text-amber-600 dark:text-amber-400/90",
},
success: {
label: "Passed",
Icon: CircleCheckIcon,
toneClassName: "text-emerald-600 dark:text-emerald-300/90",
},
failure: { label: "Failed", Icon: CircleXIcon, toneClassName: "text-destructive" },
cancelled: { label: "Cancelled", Icon: CircleXIcon, toneClassName: "text-destructive" },
skipped: { label: "Skipped", Icon: CircleSlashIcon, toneClassName: "text-muted-foreground/70" },
neutral: { label: "Neutral", Icon: CircleDashedIcon, toneClassName: "text-muted-foreground/70" },
} as const satisfies Record<
PullRequestCheckStatus,
{ label: string; Icon: typeof CircleCheckIcon | typeof Spinner; toneClassName: string }
>;
function isWorkflowApprovalCheck(check: Pick<PullRequestCheck, "status" | "url">): boolean {
return (
check.status === "action-required" &&
check.url !== null &&
/\/actions\/runs\/\d+(?:\/|$)/u.test(check.url)
);
}
export function pullRequestCheckStatusLabel(
check: Pick<PullRequestCheck, "status" | "url">,
): string {
return isWorkflowApprovalCheck(check)
? "Awaiting approval"
: CHECK_STATUS_PRESENTATION[check.status].label;
}
export function PullRequestCheckStatusIcon({ status }: { status: PullRequestCheckStatus }) {
const presentation = CHECK_STATUS_PRESENTATION[status];
return (
<presentation.Icon
aria-hidden
className={cn("size-3.5 shrink-0", presentation.toneClassName)}
/>
);
}
const CHECKS_STATE_PRESENTATION = {
passing: {
label: "All checks have passed",
Icon: CircleCheckIcon,
toneClassName: CHECK_STATUS_PRESENTATION.success.toneClassName,
},
failing: {
label: "Some checks were not successful",
Icon: CircleXIcon,
toneClassName: "text-destructive",
},
pending: {
label: "Some checks haven't completed yet",
Icon: CircleDotIcon,
toneClassName: "text-amber-600 dark:text-amber-400/90",
},
} as const satisfies Record<
PullRequestChecksState,
{ label: string; Icon: typeof CircleCheckIcon; toneClassName: string }
>;
export function pullRequestChecksStatePresentation(state: PullRequestChecksState) {
return CHECKS_STATE_PRESENTATION[state];
}
export function pullRequestChecksState(
checks: ReadonlyArray<PullRequestCheck>,
): PullRequestChecksState | null {
if (checks.length === 0) return null;
const statuses = new Set(checks.map((check) => check.status));
if (statuses.has("failure") || statuses.has("cancelled")) return "failing";
if (statuses.has("pending") || statuses.has("action-required")) return "pending";
return statuses.has("success") ? "passing" : null;
}
const REVIEW_OUTCOME_PRESENTATION = {
approved: {
label: "Approved",
Icon: CircleCheckIcon,
toneClassName: "text-emerald-600 dark:text-emerald-300/90",
ringClassName: "ring-2 ring-emerald-500 dark:ring-emerald-400",
staleRingClassName:
"ring-2 ring-[color-mix(in_srgb,var(--color-emerald-500)_35%,var(--background))] dark:ring-[color-mix(in_srgb,var(--color-emerald-400)_35%,var(--background))]",
fillClassName: "fill-emerald-500 dark:fill-emerald-400",
badgeVariant: "success",
},
"changes-requested": {
label: "Changes requested",
Icon: CircleXIcon,
toneClassName: "text-destructive",
ringClassName: "ring-2 ring-destructive",
staleRingClassName: "ring-2 ring-[color-mix(in_srgb,var(--destructive)_35%,var(--background))]",
fillClassName: "fill-destructive",
badgeVariant: "error",
},
dismissed: {
label: "Review dismissed",
Icon: CircleDashedIcon,
toneClassName: "text-muted-foreground/70",
ringClassName: "ring-2 ring-muted-foreground/60",
staleRingClassName:
"ring-2 ring-[color-mix(in_srgb,var(--contrast-muted-foreground)_30%,var(--background))]",
fillClassName: "fill-muted-foreground",
badgeVariant: "outline",
},
} as const satisfies Record<
PullRequestReviewOutcome,
{
label: string;
Icon: typeof CircleCheckIcon;
toneClassName: string;
ringClassName: string;
staleRingClassName: string;
fillClassName: string;
badgeVariant: "success" | "error" | "outline";
}
>;
export function pullRequestReviewOutcomeToneClassName(outcome: PullRequestReviewOutcome): string {
return REVIEW_OUTCOME_PRESENTATION[outcome].toneClassName;
}
// Mix rings with the background to prevent avatars showing through their overlap.
export function pullRequestReviewOutcomeRingClassName(
outcome: PullRequestReviewOutcome,
stale = false,
): string {
const presentation = REVIEW_OUTCOME_PRESENTATION[outcome];
return stale ? presentation.staleRingClassName : presentation.ringClassName;
}
export function pullRequestReviewOutcomeStaleLabel(outcome: PullRequestReviewOutcome): string {
return `${REVIEW_OUTCOME_PRESENTATION[outcome].label} earlier changes`;
}
export function PullRequestReviewOutcomeIcon({
outcome,
className,
}: {
outcome: PullRequestReviewOutcome;
className?: string;
}) {
const presentation = REVIEW_OUTCOME_PRESENTATION[outcome];
return (
<presentation.Icon
aria-hidden
className={cn("size-3.5 shrink-0", presentation.toneClassName, className)}
/>
);
}
// Ring color alone blends into colorful bot avatars; the glyph gives each verdict a shape.
export function PullRequestReviewOutcomeAvatarBadge({
outcome,
stale = false,
}: {
outcome: PullRequestReviewOutcome;
stale?: boolean;
}) {
const presentation = REVIEW_OUTCOME_PRESENTATION[outcome];
return (
<presentation.Icon
aria-hidden
strokeWidth={3}
className={cn(
"absolute -right-1 -bottom-1 size-3 text-background",
presentation.fillClassName,
stale && "opacity-50",
)}
/>
);
}
export function pullRequestReviewOutcomeLabel(outcome: PullRequestReviewOutcome): string {
return REVIEW_OUTCOME_PRESENTATION[outcome].label;
}
export function PullRequestReviewOutcomeBadge({
outcome,
className,
}: {
outcome: PullRequestReviewOutcome;
className?: string;
}) {
const presentation = REVIEW_OUTCOME_PRESENTATION[outcome];
return (
<Badge size="sm" variant={presentation.badgeVariant} className={className}>
<span className="inline-flex items-baseline gap-1">
<presentation.Icon aria-hidden className="size-3 self-center" />
<span>{presentation.label}</span>
</span>
</Badge>
);
}
export function PullRequestActorAvatar({
actor,
className,
}: {
actor: PullRequestActor | null;
className?: string;
}) {
const login = actor?.login ?? "ghost";
const avatarUrl = actor?.avatarUrl ?? null;
const [failedAvatarUrl, setFailedAvatarUrl] = useState<string | null>(null);
return avatarUrl === null || failedAvatarUrl === avatarUrl ? (
<span
aria-hidden
className={cn(
"flex size-4 shrink-0 items-center justify-center rounded-full bg-muted text-3xs font-medium text-muted-foreground",
className,
)}
>
{login.slice(0, 1).toUpperCase()}
</span>
) : (
<img
aria-hidden
alt=""
src={avatarUrl}
loading="lazy"
className={cn("size-4 shrink-0 rounded-full bg-muted object-cover", className)}
onError={() => setFailedAvatarUrl(avatarUrl)}
/>
);
}
export function PullRequestActorLabel({
actor,
className,
variant = "label",
tooltip = true,
profileUrl,
}: {
actor: PullRequestActor | null;
className?: string;
variant?: "label" | "avatar";
tooltip?: boolean;
profileUrl?: string | null;
}) {
const login = actor?.login ?? "ghost";
const label = (
<span className={cn("flex min-w-0 items-baseline", variant === "label" && "gap-1.5")}>
<PullRequestActorAvatar actor={actor} className="self-center" />
<span className={variant === "label" ? "truncate font-medium text-foreground" : "sr-only"}>
{login}
</span>
</span>
);
const placement = cn("flex min-w-0 shrink", className);
if (!tooltip) return <span className={placement}>{label}</span>;
return (
<Tooltip>
<TooltipTrigger
render={
profileUrl ? (
<InlineButton
className={placement}
render={
<a
href={profileUrl}
target="_blank"
rel="noopener noreferrer"
aria-label={`Open ${login}'s profile`}
/>
}
/>
) : (
<span className={placement} />
)
}
>
{label}
</TooltipTrigger>
<TooltipPopup side="top">
{actor?.name && actor.name !== login ? `${actor.name} (@${login})` : login}
{profileUrl ? " · Open profile" : ""}
</TooltipPopup>
</Tooltip>
);
}
export function PullRequestDiffStat({
additions,
deletions,
className,
}: {
additions: number;
deletions: number;
className?: string;
}) {
if (additions === 0 && deletions === 0) {
return null;
}
return (
<span className={cn("inline-flex items-baseline gap-1 tabular-nums", className)}>
<span className="text-diff-addition-foreground">+{formatDiffCount(additions)}</span>
<span className="text-diff-deletion">-{formatDiffCount(deletions)}</span>
</span>
);
}
function separatorKey(segment: ReactNode): string {
return `separator:${isValidElement(segment) ? String(segment.key) : String(segment)}`;
}
export function PullRequestMetaLine({
children,
className,
}: {
children: ReactNode;
className?: string;
}) {
const segments = Children.toArray(children);
return (
<span className={cn("flex min-w-0 items-center gap-1.5", className)}>
{segments.flatMap((segment, index) =>
index === 0
? segment
: [
<span
aria-hidden
className="shrink-0 text-muted-foreground/50"
key={separatorKey(segment)}
>
·
</span>,
segment,
],
)}
</span>
);
}
export function summarizePullRequestChecks(checks: ReadonlyArray<PullRequestCheck>): string {
if (checks.length === 0) return "No checks reported";
const actionRequired = checks.filter((check) => check.status === "action-required");
const workflowApprovalRequired = actionRequired.filter(isWorkflowApprovalCheck).length;
const otherActionRequired = actionRequired.length - workflowApprovalRequired;
const failed = checks.filter(
(check) => check.status === "failure" || check.status === "cancelled",
).length;
const pending = checks.filter((check) => check.status === "pending").length;
const passed = checks.filter((check) => check.status === "success").length;
if (failed > 0) return `${failed} of ${checks.length} failing`;
if (workflowApprovalRequired > 0 && otherActionRequired > 0) {
return `${workflowApprovalRequired} ${workflowApprovalRequired === 1 ? "workflow" : "workflows"} and ${otherActionRequired} ${otherActionRequired === 1 ? "check" : "checks"} awaiting action`;
}
if (workflowApprovalRequired > 0) {
return `${workflowApprovalRequired} ${workflowApprovalRequired === 1 ? "workflow" : "workflows"} awaiting approval`;
}
if (otherActionRequired > 0) {
return `${otherActionRequired} ${otherActionRequired === 1 ? "check" : "checks"} awaiting action`;
}
if (pending > 0) return `${pending} of ${checks.length} running`;
return passed === checks.length ? "All checks passed" : `${passed} of ${checks.length} passing`;
}