Open History to see the last commit
1import type { PullRequestUpdateMethod } from "@t3tools/contracts";
2import { TriangleAlertIcon } from "lucide-react";
3import type { ReactNode } from "react";
4
5import { cn } from "~/lib/utils";
6import { Button } from "../ui/button";
7import { MiddleTruncate } from "../ui/middle-truncate";
8import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
9import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
10import { PullRequestGlyph } from "./pullRequestIcons";
11
12interface BaseFreshness {
13 readonly behindBy: number | null;
14 readonly methods: ReadonlyArray<PullRequestUpdateMethod>;
15}
16
17function PullRequestBaseFreshnessWarning({
18 baseBranch,
19 freshness,
20 pending,
21 onUpdate,
22 iconClassName,
23 className,
24 children,
25}: {
26 readonly baseBranch: string;
27 readonly freshness: BaseFreshness;
28 readonly pending: boolean;
29 readonly onUpdate: (method: PullRequestUpdateMethod) => void;
30 readonly iconClassName?: string | undefined;
31 readonly className?: string;
32 readonly children?: ReactNode;
33}) {
34 const behind =
35 freshness.behindBy === null
36 ? ""
37 : ` by ${freshness.behindBy.toLocaleString()} ${
38 freshness.behindBy === 1 ? "commit" : "commits"
39 }`;
40 const summary = `This branch is out-of-date with ${baseBranch}${behind}.`;
41 return (
42 <Popover>
43 <PopoverTrigger
44 openOnHover
45 delay={0}
46 closeDelay={120}
47 render={
48 <button
49 type="button"
50 aria-label={summary}
51 className={cn(
52 "inline-flex min-w-0 shrink-0 cursor-help items-center gap-1 rounded-sm text-warning-foreground outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring",
53 className,
54 )}
55 />
56 }
57 >
58 {children}
59 <TriangleAlertIcon aria-hidden className={cn("size-3.5 shrink-0", iconClassName)} />
60 </PopoverTrigger>
61 <PopoverPopup align="start" side="bottom" className="max-w-80" padding="compact">
62 <p className="text-xs text-foreground">{summary}</p>
63 <p className="mt-0.5 text-xs text-muted-foreground">Changes can be cleanly merged.</p>
64 {freshness.methods.length > 0 ? (
65 <span className="mt-2 flex flex-wrap items-center gap-1.5">
66 {freshness.methods.map((method) => (
67 <Button
68 key={method}
69 size="xs"
70 variant="outline"
71 disabled={pending}
72 onClick={() => onUpdate(method)}
73 >
74 <PullRequestGlyph.merged aria-hidden className="size-3" />
75 {method === "rebase" ? "Update with rebase" : "Update branch"}
76 </Button>
77 ))}
78 </span>
79 ) : null}
80 </PopoverPopup>
81 </Popover>
82 );
83}
84
85/** The base branch, warning when the pull request has fallen behind it. */
86export function PullRequestBaseBranch({
87 baseBranch,
88 freshness,
89 stacked,
90 pending,
91 onUpdate,
92 iconClassName,
93}: {
94 readonly baseBranch: string;
95 readonly freshness: BaseFreshness | null;
96 readonly stacked: boolean;
97 readonly pending: boolean;
98 readonly onUpdate: (method: PullRequestUpdateMethod) => void;
99 readonly iconClassName?: string;
100}) {
101 const label = (
102 <>
103 {stacked ? (
104 <PullRequestGlyph.stack aria-label="Stacked pull request" className="size-3 shrink-0" />
105 ) : null}
106 <code className="flex min-w-0">
107 <MiddleTruncate value={baseBranch} showTitle={false} />
108 </code>
109 </>
110 );
111 if (freshness) {
112 return (
113 <PullRequestBaseFreshnessWarning
114 baseBranch={baseBranch}
115 freshness={freshness}
116 pending={pending}
117 onUpdate={onUpdate}
118 iconClassName={iconClassName}
119 className="max-w-[40%]"
120 >
121 {label}
122 </PullRequestBaseFreshnessWarning>
123 );
124 }
125 return (
126 <Tooltip>
127 <TooltipTrigger
128 render={
129 <span className="inline-flex min-w-0 max-w-[40%] shrink-0 items-center gap-1">
130 {label}
131 </span>
132 }
133 />
134 <TooltipPopup side="top">{stacked ? `Stacked on ${baseBranch}` : baseBranch}</TooltipPopup>
135 </Tooltip>
136 );
137}