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

PullRequestDetailCrumbs.tsx

220 lines6.9 KB

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 { SharedTooltipTrigger } from "../ui/shared-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;
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 <SharedTooltipTrigger
42 render={
43 <Button
44 size="icon-micro"
45 variant="ghost-muted"
46 {...(focusable ? {} : { tabIndex: -1 })}
47 onClick={onBack}
48 className="-ml-1.5"
49 aria-label={gt("Back to this thread's pull requests")}
50 >
51 <ArrowLeftIcon aria-hidden className="size-3.5" />
52 </Button>
53 }
54 label={
55 <>
56 <T>Back to pull requests</T>
57 </>
58 }
59 />
60 );
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 <SharedTooltipTrigger
80 render={
81 <button
82 type="button"
83 {...(focusable ? {} : { tabIndex: -1 })}
84 onClick={(event) => void openLink(detail.url, { event }).catch(console.error)}
85 onAuxClick={(event) => {
86 if (event.button === 1) void openLink(detail.url, { event }).catch(console.error);
87 }}
88 onContextMenu={(event) => openNumberContextMenu(event, detail)}
89 className={cn(
90 "inline-flex shrink-0 cursor-pointer items-center gap-0.5 font-medium underline-offset-2 hover:underline",
91 toneClassName,
92 )}
93 aria-label={gt(
94 "Open {term, select, mr {merge request} cr {change request} other {pull request}} #{number} on host",
95 {
96 term: changeRequestTermKind(sourceControlClients.get(detail.provider).changeRequest),
97 number: detail.number,
98 },
99 )}
100 >
101 #{detail.number}
102 <ExternalLinkIcon aria-hidden className="size-2.5" />
103 </button>
104 }
105 label={openOnHostText}
106 />
107 );
109​
110/**
111 * The repository and number above the title, swapped for the number and title once the reader has
112 * scrolled the title away.
113 */
114export function PullRequestDetailCrumbs({
115 detail,
116 threadRef,
117 toneClassName,
118 condensed,
119 repositoryUrl,
120 openOnHostText,
121 onBack,
122}: {
123 detail: CrumbsPullRequest | null;
124 threadRef: ScopedThreadRef | null;
125 toneClassName: string | null;
126 condensed: boolean;
127 repositoryUrl: string | null;
128 openOnHostText: string;
129 onBack: (() => void) | undefined;
130}) {
131 const gt = useGT();
132 const openLink = useOpenLink(threadRef);
133 const ready = detail !== null && toneClassName !== null;
134 return (
135 <div className="pl-4 grid h-7 min-w-0 items-center overflow-hidden">
136 <div
137 aria-hidden={condensed}
138 inert={condensed}
139 data-workspace-scroll-swap
140 className={cn(
141 ROW_CLASS_NAME,
142 condensed
143 ? "pointer-events-none -translate-y-1 opacity-0 duration-100"
144 : "translate-y-0 opacity-100 delay-50 duration-150",
145 )}
146 >
147 {ready ? (
148 <>
149 {onBack ? <BackButton onBack={onBack} focusable /> : null}
150 <SharedTooltipTrigger
151 render={
152 repositoryUrl ? (
153 <button
154 type="button"
155 onClick={(event) =>
156 void openLink(repositoryUrl, { event }).catch(console.error)
157 }
158 onAuxClick={(event) => {
159 if (event.button === 1)
160 void openLink(repositoryUrl, { event }).catch(console.error);
161 }}
162 className="min-w-0 cursor-pointer truncate text-left font-medium text-muted-foreground underline-offset-2 hover:text-foreground hover:underline"
163 >
164 {detail.repository}
165 </button>
166 ) : (
167 <span className="min-w-0 truncate font-medium text-muted-foreground">
168 {detail.repository}
169 </span>
170 )
171 }
172 label={
173 repositoryUrl
174 ? gt("Open {repository} repository", { repository: detail.repository })
175 : detail.repository
176 }
177 />
178 <NumberLink
179 detail={detail}
180 threadRef={threadRef}
181 toneClassName={toneClassName}
182 openOnHostText={openOnHostText}
183 focusable
184 />
185 </>
186 ) : null}
187 </div>
188 <div
189 aria-hidden={!condensed}
190 inert={!condensed}
191 data-workspace-scroll-swap
192 className={cn(
193 ROW_CLASS_NAME,
194 condensed
195 ? "translate-y-0 opacity-100 delay-50 duration-150"
196 : "pointer-events-none translate-y-1 opacity-0 duration-100",
197 )}
198 >
199 {ready ? (
200 <>
201 {onBack ? <BackButton onBack={onBack} focusable={condensed} /> : null}
202 <NumberLink
203 detail={detail}
204 threadRef={threadRef}
205 toneClassName={toneClassName}
206 openOnHostText={openOnHostText}
207 focusable={condensed}
208 />
209 <SharedTooltipTrigger
210 render={
211 <span className="min-w-0 truncate font-medium text-foreground">{detail.title}</span>
212 }
213 label={detail.title}
214 />
215 </>
216 ) : null}
217 </div>
218 </div>
219 );