The best way to control Claude Code, Codex and more from any computer or phone

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