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