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