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

PullRequestDetailCrumbs.tsx

223 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, select } 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`Open ${select(
94 {
95 term: changeRequestTermKind(sourceControlClients.get(detail.provider).changeRequest),
96 },
97 {
98 mr: "merge request",
99 cr: "change request",
100 other: "pull request",
101 },
102 )} #${{ number: detail.number }} on host`}
103 >
104 #{detail.number}
105 <ExternalLinkIcon aria-hidden className="size-2.5" />
106 </button>
107 }
108 label={openOnHostText}
109 />
110 );
112​
113/**
114 * The repository and number above the title, swapped for the number and title once the reader has
115 * scrolled the title away.
116 */
117export function PullRequestDetailCrumbs({
118 detail,
119 threadRef,
120 toneClassName,
121 condensed,
122 repositoryUrl,
123 openOnHostText,
124 onBack,
125}: {
126 detail: CrumbsPullRequest | null;
127 threadRef: ScopedThreadRef | null;
128 toneClassName: string | null;
129 condensed: boolean;
130 repositoryUrl: string | null;
131 openOnHostText: string;
132 onBack: (() => void) | undefined;
133}) {
134 const gt = useGT();
135 const openLink = useOpenLink(threadRef);
136 const ready = detail !== null && toneClassName !== null;
137 return (
138 <div className="pl-4 grid h-7 min-w-0 items-center overflow-hidden">
139 <div
140 aria-hidden={condensed}
141 inert={condensed}
142 data-workspace-scroll-swap
143 className={cn(
144 ROW_CLASS_NAME,
145 condensed
146 ? "pointer-events-none -translate-y-1 opacity-0 duration-100"
147 : "translate-y-0 opacity-100 delay-50 duration-150",
148 )}
149 >
150 {ready ? (
151 <>
152 {onBack ? <BackButton onBack={onBack} focusable /> : null}
153 <SharedTooltipTrigger
154 render={
155 repositoryUrl ? (
156 <button
157 type="button"
158 onClick={(event) =>
159 void openLink(repositoryUrl, { event }).catch(console.error)
160 }
161 onAuxClick={(event) => {
162 if (event.button === 1)
163 void openLink(repositoryUrl, { event }).catch(console.error);
164 }}
165 className="min-w-0 cursor-pointer truncate text-left font-medium text-muted-foreground underline-offset-2 hover:text-foreground hover:underline"
166 >
167 {detail.repository}
168 </button>
169 ) : (
170 <span className="min-w-0 truncate font-medium text-muted-foreground">
171 {detail.repository}
172 </span>
173 )
174 }
175 label={
176 repositoryUrl
177 ? gt`Open ${{ repository: detail.repository }} repository`
178 : detail.repository
179 }
180 />
181 <NumberLink
182 detail={detail}
183 threadRef={threadRef}
184 toneClassName={toneClassName}
185 openOnHostText={openOnHostText}
186 focusable
187 />
188 </>
189 ) : null}
190 </div>
191 <div
192 aria-hidden={!condensed}
193 inert={!condensed}
194 data-workspace-scroll-swap
195 className={cn(
196 ROW_CLASS_NAME,
197 condensed
198 ? "translate-y-0 opacity-100 delay-50 duration-150"
199 : "pointer-events-none translate-y-1 opacity-0 duration-100",
200 )}
201 >
202 {ready ? (
203 <>
204 {onBack ? <BackButton onBack={onBack} focusable={condensed} /> : null}
205 <NumberLink
206 detail={detail}
207 threadRef={threadRef}
208 toneClassName={toneClassName}
209 openOnHostText={openOnHostText}
210 focusable={condensed}
211 />
212 <SharedTooltipTrigger
213 render={
214 <span className="min-w-0 truncate font-medium text-foreground">{detail.title}</span>
215 }
216 label={detail.title}
217 />
218 </>
219 ) : null}
220 </div>
221 </div>
222 );