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

PullRequestBaseBranch.tsx

147 lines4.5 KB

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