Open History to see the last commit
1import { GitPullRequestIcon, RefreshCwIcon } from "lucide-react";
2import { Option } from "effect";
3import { type ReactNode } from "react";
4import type {
5 SourceControlProviderKind,
6 SourceControlDiscoveryResult,
7 SourceControlProviderAuth,
8 SourceControlProviderDiscoveryItem,
9 VcsDriverKind,
10 VcsDiscoveryItem,
11} from "@t3tools/contracts";
12
13import { cn } from "../../lib/utils";
14import {
15 refreshSourceControlDiscovery,
16 useSourceControlDiscovery,
17} from "../../lib/sourceControlDiscoveryState";
18import { Badge } from "../ui/badge";
19import { Button } from "../ui/button";
20import {
21 Empty,
22 EmptyContent,
23 EmptyDescription,
24 EmptyHeader,
25 EmptyMedia,
26 EmptyTitle,
27} from "../ui/empty";
28import { Skeleton } from "../ui/skeleton";
29import { Switch } from "../ui/switch";
30import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
31import {
32 AzureDevOpsIcon,
33 BitbucketIcon,
34 GitHubIcon,
35 GitIcon,
36 GitLabIcon,
37 JujutsuIcon,
38 type Icon,
39} from "../Icons";
40import { RedactedSensitiveText } from "./RedactedSensitiveText";
41import { SettingsPageContainer, SettingsSection } from "./settingsLayout";
42
43const EMPTY_DISCOVERY_RESULT: SourceControlDiscoveryResult = {
44 versionControlSystems: [],
45 sourceControlProviders: [],
46};
47
48const SOURCE_CONTROL_PROVIDER_ICONS: Partial<Record<SourceControlProviderKind, Icon>> = {
49 github: GitHubIcon,
50 gitlab: GitLabIcon,
51 "azure-devops": AzureDevOpsIcon,
52 bitbucket: BitbucketIcon,
53};
54
55const VCS_ICONS: Partial<Record<VcsDriverKind, Icon>> = {
56 git: GitIcon,
57 jj: JujutsuIcon,
58};
59
60const SOURCE_CONTROL_SKELETON_ROWS = ["primary", "secondary"] as const;
61
62function optionLabel(value: Option.Option<string>): string | null {
63 return Option.getOrNull(value);
64}
65
66function isProviderDiscoveryItem(
67 item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem,
68): item is SourceControlProviderDiscoveryItem {
69 return "auth" in item;
70}
71
72function authPresentation(auth: SourceControlProviderAuth): {
73 readonly label: string;
74 readonly badge: "warning" | null;
75} {
76 if (auth.status === "authenticated") {
77 return { label: "Signed in", badge: null };
78 }
79 if (auth.status === "unauthenticated") {
80 return { label: "Sign in", badge: "warning" };
81 }
82 return { label: "Sign in", badge: null };
83}
84
85function RedactedAccount(props: { readonly account: string | null }) {
86 return (
87 <RedactedSensitiveText
88 value={props.account}
89 ariaLabel="Toggle source control account visibility"
90 revealTooltip="Click to reveal account"
91 hideTooltip="Click to hide account"
92 />
93 );
94}
95
96function itemStatusDot(item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem): string {
97 if (!item.implemented) return "bg-muted-foreground/35";
98 if (item.status !== "available") return "bg-warning";
99 if (isProviderDiscoveryItem(item) && item.auth.status !== "authenticated") return "bg-warning";
100 return "bg-success";
101}
102
103function SourceControlItemMark({
104 item,
105}: {
106 readonly item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem;
107}) {
108 const dotClassName = itemStatusDot(item);
109 const Icon = isProviderDiscoveryItem(item)
110 ? SOURCE_CONTROL_PROVIDER_ICONS[item.kind]
111 : VCS_ICONS[item.kind];
112
113 if (!Icon) {
114 return <span className={cn("size-2 shrink-0 rounded-full", dotClassName)} aria-hidden />;
115 }
116
117 return (
118 <span className="relative inline-flex size-5 shrink-0 items-center justify-center">
119 <Icon className="size-4.5 text-foreground/80" aria-hidden />
120 <span
121 className={cn(
122 "pointer-events-none absolute -left-0.5 -top-0.5 size-2 rounded-full ring-2 ring-background",
123 dotClassName,
124 )}
125 aria-hidden
126 />
127 </span>
128 );
129}
130
131function itemSummary({
132 item,
133 auth,
134 authAccount,
135}: {
136 readonly item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem;
137 readonly auth: SourceControlProviderAuth | null;
138 readonly authAccount: string | null;
139}) {
140 if (!item.implemented) {
141 return <span>Support for {item.label} is coming soon.</span>;
142 }
143
144 if (item.status !== "available") {
145 return <span>Not found - {item.installHint}</span>;
146 }
147
148 if (auth) {
149 if (auth.status === "authenticated") {
150 return (
151 <>
152 <span>Authenticated</span>
153 {authAccount ? (
154 <>
155 <span aria-hidden>as</span>
156 <RedactedAccount account={authAccount} />
157 </>
158 ) : null}
159 </>
160 );
161 }
162
163 if (!item.executable) {
164 return <span>{item.installHint}</span>;
165 }
166
167 if (auth.status === "unauthenticated") {
168 return <span>Sign in with the {item.executable} CLI to enable pull request actions.</span>;
169 }
170 return (
171 <span>
172 Install and sign in with the {item.executable} CLI to enable pull request actions.
173 </span>
174 );
175 }
176
177 return <span>Available</span>;
178}
179
180function DiscoveryItemRow({
181 item,
182}: {
183 readonly item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem;
184}) {
185 const version = optionLabel(item.version);
186 const enabled = item.implemented && item.status === "available";
187 const auth = isProviderDiscoveryItem(item) ? item.auth : null;
188 const authStatus = auth ? authPresentation(auth) : null;
189 const authAccount = auth ? optionLabel(auth.account) : null;
190
191 return (
192 <div
193 className={cn(
194 "border-t border-border/60 first:border-t-0",
195 !item.implemented && "opacity-80",
196 )}
197 >
198 <div className="px-4 py-3.5 sm:px-5">
199 <div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
200 <div className="min-w-0 flex-1 space-y-1">
201 <div className="flex min-w-0 flex-wrap items-center gap-2">
202 <SourceControlItemMark item={item} />
203 <h3 className="truncate text-[13px] font-semibold tracking-[-0.01em] text-foreground">
204 {item.label}
205 </h3>
206 {version ? <code className="text-xs text-muted-foreground">{version}</code> : null}
207 {!item.implemented ? (
208 <Badge variant="warning" size="sm">
209 Coming Soon
210 </Badge>
211 ) : null}
212 {authStatus?.badge ? (
213 <Badge variant={authStatus.badge} size="sm">
214 {authStatus.label}
215 </Badge>
216 ) : null}
217 </div>
218 <p className="flex min-w-0 flex-wrap items-center gap-x-1 text-xs text-muted-foreground">
219 {itemSummary({ item, auth, authAccount })}
220 </p>
221 </div>
222 <div className="flex w-full shrink-0 items-center gap-2 sm:w-auto sm:justify-end">
223 {item.implemented ? (
224 <Switch checked={enabled} disabled aria-label={`${item.label} availability`} />
225 ) : null}
226 </div>
227 </div>
228 </div>
229 </div>
230 );
231}
232
233function SourceControlSectionSkeleton({
234 title,
235 headerAction,
236}: {
237 readonly title: string;
238 readonly headerAction?: ReactNode;
239}) {
240 return (
241 <SettingsSection title={title} headerAction={headerAction}>
242 {SOURCE_CONTROL_SKELETON_ROWS.map((row) => (
243 <div key={row} className="border-t border-border/60 px-4 py-3.5 first:border-t-0 sm:px-5">
244 <div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
245 <div className="min-w-0 flex-1 space-y-2">
246 <div className="flex items-center gap-2">
247 <span className="relative inline-flex size-5 shrink-0 items-center justify-center">
248 <Skeleton className="size-4.5 rounded-md" />
249 <Skeleton
250 className="pointer-events-none absolute -left-0.5 -top-0.5 size-2 rounded-full ring-2 ring-background"
251 aria-hidden
252 />
253 </span>
254 <Skeleton className="h-4 w-28 rounded-full" />
255 <Skeleton className="h-5 w-14 rounded-full" />
256 </div>
257 <Skeleton className="h-3 w-full max-w-xs rounded-full" />
258 </div>
259 <div className="flex shrink-0 items-center gap-2">
260 <Skeleton className="size-7 rounded-md" />
261 <Skeleton className="h-5 w-9 rounded-full" />
262 </div>
263 </div>
264 </div>
265 ))}
266 </SettingsSection>
267 );
268}
269
270function EmptySourceControlDiscovery({
271 error,
272 isPending,
273 onScan,
274}: {
275 readonly error: string | null;
276 readonly isPending: boolean;
277 readonly onScan: () => void;
278}) {
279 const hasError = error !== null;
280
281 return (
282 <SettingsSection title="Detected tools">
283 <Empty className="min-h-88">
284 <EmptyMedia variant="icon">
285 <GitPullRequestIcon />
286 </EmptyMedia>
287 <EmptyHeader>
288 <EmptyTitle>
289 {hasError ? "Could not scan source control" : "No source control tools found"}
290 </EmptyTitle>
291 <EmptyDescription>
292 {hasError ? error : "Install a supported Git or pull request CLI, then scan again."}
293 </EmptyDescription>
294 </EmptyHeader>
295 <EmptyContent>
296 <Button
297 size="sm"
298 variant="outline"
299 className="h-8 gap-1.5 px-3 text-xs"
300 onClick={onScan}
301 disabled={isPending}
302 >
303 <RefreshCwIcon className={cn("size-3.5", isPending && "animate-spin")} />
304 Scan
305 </Button>
306 </EmptyContent>
307 </Empty>
308 </SettingsSection>
309 );
310}
311
312export function SourceControlSettingsPanel() {
313 const discovery = useSourceControlDiscovery();
314
315 const result = discovery.data ?? EMPTY_DISCOVERY_RESULT;
316 const hasDiscoveryItems =
317 result.versionControlSystems.length > 0 || result.sourceControlProviders.length > 0;
318 const isInitialScanPending = discovery.isPending && discovery.data === null;
319 const handleScan = () => {
320 void refreshSourceControlDiscovery();
321 };
322 const scanButton = (
323 <Tooltip>
324 <TooltipTrigger
325 render={
326 <Button
327 size="icon-xs"
328 variant="ghost"
329 className="size-5 rounded-sm p-0 text-muted-foreground hover:text-foreground"
330 onClick={handleScan}
331 disabled={discovery.isPending}
332 aria-label="Scan source control tools"
333 >
334 <RefreshCwIcon className={cn("size-3", discovery.isPending && "animate-spin")} />
335 </Button>
336 }
337 />
338 <TooltipPopup side="top">Scan source control tools</TooltipPopup>
339 </Tooltip>
340 );
341
342 return (
343 <SettingsPageContainer>
344 {isInitialScanPending ? (
345 <>
346 <SourceControlSectionSkeleton title="Version Control" headerAction={scanButton} />
347 <SourceControlSectionSkeleton title="Source Control Providers" />
348 </>
349 ) : hasDiscoveryItems ? (
350 <>
351 {result.versionControlSystems.length > 0 ? (
352 <SettingsSection title="Version Control" headerAction={scanButton}>
353 {result.versionControlSystems.map((item) => (
354 <DiscoveryItemRow key={`vcs:${item.kind}`} item={item} />
355 ))}
356 </SettingsSection>
357 ) : null}
358
359 {result.sourceControlProviders.length > 0 ? (
360 <SettingsSection
361 title="Source Control Providers"
362 headerAction={result.versionControlSystems.length === 0 ? scanButton : null}
363 >
364 {result.sourceControlProviders.map((item) => (
365 <DiscoveryItemRow key={`provider:${item.kind}`} item={item} />
366 ))}
367 </SettingsSection>
368 ) : null}
369 </>
370 ) : (
371 <EmptySourceControlDiscovery
372 error={discovery.error}
373 isPending={discovery.isPending}
374 onScan={handleScan}
375 />
376 )}
377 </SettingsPageContainer>
378 );
379}