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