Open History to see the last commit
1import type { ScopedThreadRef } from "@t3tools/contracts";
2import { contextMenuSourceOf } from "../../contextMenuSource";
3import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
4import { T, useGT } from "gt-react";
5import { ArrowLeftIcon, ExternalLinkIcon } from "lucide-react";
6import type { MouseEvent as ReactMouseEvent } from "react";
7
8import { useOpenLink } from "~/browser/useOpenLink";
9import { cn } from "~/lib/utils";
10import { Button } from "../ui/button";
11import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
12import { openOnHostLabel, showPullRequestLinkContextMenu } from "./pullRequestLinkContextMenu";
13
14import { changeRequestTermKind } from "./changeRequestTerm";
15
16interface CrumbsPullRequest {
17 readonly url: string;
18 readonly provider: string;
19 readonly number: number;
20 readonly repository: string;
21 readonly title: string;
22}
23
24const openNumberContextMenu = (event: ReactMouseEvent, detail: CrumbsPullRequest): void => {
25 event.preventDefault();
26 event.stopPropagation();
27 void showPullRequestLinkContextMenu({
28 url: detail.url,
29 openLabel: openOnHostLabel(sourceControlClients.get(detail.provider)),
30 position: { x: event.clientX, y: event.clientY },
31 source: contextMenuSourceOf(event),
32 });
33};
34
35const ROW_CLASS_NAME =
36 "col-start-1 row-start-1 flex min-w-0 items-center gap-1 text-sm text-muted-foreground transition-[opacity,transform] ease-out motion-reduce:transform-none motion-reduce:transition-none sm:text-xs";
37
38function BackButton({ onBack, focusable }: { onBack: () => void; focusable: boolean }) {
39 const gt = useGT();
40 return (
41 <Tooltip>
42 <TooltipTrigger
43 render={
44 <Button
45 size="icon-micro"
46 variant="ghost-muted"
47 {...(focusable ? {} : { tabIndex: -1 })}
48 onClick={onBack}
49 className="-ml-1.5"
50 aria-label={gt("Back to this thread's pull requests")}
51 >
52 <ArrowLeftIcon aria-hidden className="size-3.5" />
53 </Button>
54 }
55 />
56 <TooltipPopup side="top">
57 <T>Back to pull requests</T>
58 </TooltipPopup>
59 </Tooltip>
60 );
61}
62
63function NumberLink({
64 detail,
65 threadRef,
66 toneClassName,
67 openOnHostText,
68 focusable,
69}: {
70 detail: CrumbsPullRequest;
71 threadRef: ScopedThreadRef | null;
72 toneClassName: string;
73 openOnHostText: string;
74 focusable: boolean;
75}) {
76 const gt = useGT();
77 const openLink = useOpenLink(threadRef);
78 return (
79 <Tooltip>
80 <TooltipTrigger
81 render={
82 <button
83 type="button"
84 {...(focusable ? {} : { tabIndex: -1 })}
85 onClick={(event) => void openLink(detail.url, { event }).catch(console.error)}
86 onAuxClick={(event) => {
87 if (event.button === 1) void openLink(detail.url, { event }).catch(console.error);
88 }}
89 onContextMenu={(event) => openNumberContextMenu(event, detail)}
90 className={cn(
91 "inline-flex shrink-0 cursor-pointer items-center gap-0.5 font-medium underline-offset-2 hover:underline",
92 toneClassName,
93 )}
94 aria-label={gt(
95 "Open {term, select, mr {merge request} cr {change request} other {pull request}} #{number} on host",
96 {
97 term: changeRequestTermKind(
98 sourceControlClients.get(detail.provider).changeRequest,
99 ),
100 number: detail.number,
101 },
102 )}
103 >
104 #{detail.number}
105 <ExternalLinkIcon aria-hidden className="size-2.5" />
106 </button>
107 }
108 />
109 <TooltipPopup side="top">{openOnHostText}</TooltipPopup>
110 </Tooltip>
111 );
112}
113
114/**
115 * The repository and number above the title, swapped for the number and title once the reader has
116 * scrolled the title away.
117 */
118export function PullRequestDetailCrumbs({
119 detail,
120 threadRef,
121 toneClassName,
122 condensed,
123 repositoryUrl,
124 openOnHostText,
125 onBack,
126}: {
127 detail: CrumbsPullRequest | null;
128 threadRef: ScopedThreadRef | null;
129 toneClassName: string | null;
130 condensed: boolean;
131 repositoryUrl: string | null;
132 openOnHostText: string;
133 onBack: (() => void) | undefined;
134}) {
135 const gt = useGT();
136 const openLink = useOpenLink(threadRef);
137 const ready = detail !== null && toneClassName !== null;
138 return (
139 <div className="pl-4 grid h-7 min-w-0 items-center overflow-hidden">
140 <div
141 aria-hidden={condensed}
142 inert={condensed}
143 data-workspace-scroll-swap
144 className={cn(
145 ROW_CLASS_NAME,
146 condensed
147 ? "pointer-events-none -translate-y-1 opacity-0 duration-100"
148 : "translate-y-0 opacity-100 delay-50 duration-150",
149 )}
150 >
151 {ready ? (
152 <>
153 {onBack ? <BackButton onBack={onBack} focusable /> : null}
154 <Tooltip>
155 <TooltipTrigger
156 render={
157 repositoryUrl ? (
158 <button
159 type="button"
160 onClick={(event) =>
161 void openLink(repositoryUrl, { event }).catch(console.error)
162 }
163 onAuxClick={(event) => {
164 if (event.button === 1)
165 void openLink(repositoryUrl, { event }).catch(console.error);
166 }}
167 className="min-w-0 cursor-pointer truncate text-left font-medium text-muted-foreground underline-offset-2 hover:text-foreground hover:underline"
168 >
169 {detail.repository}
170 </button>
171 ) : (
172 <span className="min-w-0 truncate font-medium text-muted-foreground">
173 {detail.repository}
174 </span>
175 )
176 }
177 />
178 <TooltipPopup side="top">
179 {repositoryUrl
180 ? gt("Open {repository} repository", { repository: detail.repository })
181 : detail.repository}
182 </TooltipPopup>
183 </Tooltip>
184 <NumberLink
185 detail={detail}
186 threadRef={threadRef}
187 toneClassName={toneClassName}
188 openOnHostText={openOnHostText}
189 focusable
190 />
191 </>
192 ) : null}
193 </div>
194 <div
195 aria-hidden={!condensed}
196 inert={!condensed}
197 data-workspace-scroll-swap
198 className={cn(
199 ROW_CLASS_NAME,
200 condensed
201 ? "translate-y-0 opacity-100 delay-50 duration-150"
202 : "pointer-events-none translate-y-1 opacity-0 duration-100",
203 )}
204 >
205 {ready ? (
206 <>
207 {onBack ? <BackButton onBack={onBack} focusable={condensed} /> : null}
208 <NumberLink
209 detail={detail}
210 threadRef={threadRef}
211 toneClassName={toneClassName}
212 openOnHostText={openOnHostText}
213 focusable={condensed}
214 />
215 <Tooltip>
216 <TooltipTrigger
217 render={
218 <span className="min-w-0 truncate font-medium text-foreground">
219 {detail.title}
220 </span>
221 }
222 />
223 <TooltipPopup side="top">{detail.title}</TooltipPopup>
224 </Tooltip>
225 </>
226 ) : null}
227 </div>
228 </div>
229 );
230}