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 isVcsNotReady(item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem): boolean {
73 return !isProviderDiscoveryItem(item) && !item.implemented;
74}
75
76function authPresentation(auth: SourceControlProviderAuth): {
77 readonly label: string;
78 readonly badge: "warning" | null;
79} {
80 if (auth.status === "authenticated") {
81 return { label: "Authenticated", badge: null };
82 }
83 if (auth.status === "unauthenticated") {
84 return { label: "Not authenticated", badge: "warning" };
85 }
86 return { label: "Status unknown", badge: null };
87}
88
89function RedactedAccount(props: { readonly account: string | null }) {
90 return (
91 <RedactedSensitiveText
92 value={props.account}
93 ariaLabel="Toggle source control account visibility"
94 revealTooltip="Click to reveal account"
95 hideTooltip="Click to hide account"
96 />
97 );
98}
99
100function itemStatusDot(item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem): string {
101 if (isVcsNotReady(item)) return "bg-muted-foreground/35";
102 if (item.status !== "available") return "bg-warning";
103 if (isProviderDiscoveryItem(item) && item.auth.status !== "authenticated") return "bg-warning";
104 return "bg-success";
105}
106
107function SourceControlItemMark({
108 item,
109}: {
110 readonly item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem;
111}) {
112 const dotClassName = itemStatusDot(item);
113 const Icon = isProviderDiscoveryItem(item)
114 ? SOURCE_CONTROL_PROVIDER_ICONS[item.kind]
115 : VCS_ICONS[item.kind];
116
117 if (!Icon) {
118 return <span className={cn("size-2 shrink-0 rounded-full", dotClassName)} aria-hidden />;
119 }
120
121 return (
122 <span className="relative inline-flex size-5 shrink-0 items-center justify-center">
123 <Icon className="size-4.5 text-foreground/80" aria-hidden />
124 <span
125 className={cn(
126 "pointer-events-none absolute -left-0.5 -top-0.5 size-2 rounded-full ring-2 ring-background",
127 dotClassName,
128 )}
129 aria-hidden
130 />
131 </span>
132 );
133}
134
135function itemSummary({
136 item,
137 auth,
138 authAccount,
139}: {
140 readonly item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem;
141 readonly auth: SourceControlProviderAuth | null;
142 readonly authAccount: string | null;
143}) {
144 if (isVcsNotReady(item)) {
145 return <span>Support for {item.label} is coming soon.</span>;
146 }
147
148 if (item.status !== "available") {
149 return <span>Not available on this server — {item.installHint}</span>;
150 }
151
152 if (auth) {
153 if (auth.status === "authenticated") {
154 return (
155 <>
156 <span>Authenticated</span>
157 {authAccount ? (
158 <>
159 <span aria-hidden>as</span>
160 <RedactedAccount account={authAccount} />
161 </>
162 ) : null}
163 </>
164 );
165 }
166
167 if (!item.executable) {
168 return <span>{item.installHint}</span>;
169 }
170
171 if (auth.status === "unauthenticated") {
172 return (
173 <span>
174 {item.label} is not authenticated on this server. Sign in or configure credentials using
175 the <code className="rounded bg-muted px-1 py-px text-[11px]">{item.executable}</code>{" "}
176 tool on the server host to enable pull request features.
177 </span>
178 );
179 }
180 return (
181 <span>
182 Could not verify {item.label}. {item.installHint}
183 </span>
184 );
185 }
186
187 return <span>Available</span>;
188}
189
190function DiscoveryItemRow({
191 item,
192}: {
193 readonly item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem;
194}) {
195 const version = optionLabel(item.version);
196 const enabled =
197 item.status === "available" && (isProviderDiscoveryItem(item) || item.implemented);
198 const auth = isProviderDiscoveryItem(item) ? item.auth : null;
199 const authStatus = auth ? authPresentation(auth) : null;
200 const authAccount = auth ? optionLabel(auth.account) : null;
201
202 return (
203 <div
204 className={cn(
205 "border-t border-border/60 first:border-t-0",
206 isVcsNotReady(item) && "opacity-80",
207 )}
208 >
209 <div className="px-4 py-3.5 sm:px-5">
210 <div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
211 <div className="min-w-0 flex-1 space-y-1">
212 <div className="flex min-w-0 flex-wrap items-center gap-2">
213 <SourceControlItemMark item={item} />
214 <h3 className="truncate text-[13px] font-semibold tracking-[-0.01em] text-foreground">
215 {item.label}
216 </h3>
217 {version ? <code className="text-xs text-muted-foreground">{version}</code> : null}
218 {isVcsNotReady(item) ? (
219 <Badge variant="warning" size="sm">
220 Coming Soon
221 </Badge>
222 ) : null}
223 {authStatus?.badge ? (
224 <Badge variant={authStatus.badge} size="sm">
225 {authStatus.label}
226 </Badge>
227 ) : null}
228 </div>
229 <p className="flex min-w-0 flex-wrap items-center gap-x-1 text-xs text-muted-foreground">
230 {itemSummary({ item, auth, authAccount })}
231 </p>
232 </div>
233 <div className="flex w-full shrink-0 items-center gap-2 sm:w-auto sm:justify-end">
234 {!isVcsNotReady(item) ? (
235 <Switch checked={enabled} disabled aria-label={`${item.label} availability`} />
236 ) : null}
237 </div>
238 </div>
239 </div>
240 </div>
241 );
242}
243
244function SourceControlSectionSkeleton({
245 title,
246 headerAction,
247}: {
248 readonly title: string;
249 readonly headerAction?: ReactNode;
250}) {
251 return (
252 <SettingsSection title={title} headerAction={headerAction}>
253 {SOURCE_CONTROL_SKELETON_ROWS.map((row) => (
254 <div key={row} className="border-t border-border/60 px-4 py-3.5 first:border-t-0 sm:px-5">
255 <div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
256 <div className="min-w-0 flex-1 space-y-2">
257 <div className="flex items-center gap-2">
258 <span className="relative inline-flex size-5 shrink-0 items-center justify-center">
259 <Skeleton className="size-4.5 rounded-md" />
260 <Skeleton
261 className="pointer-events-none absolute -left-0.5 -top-0.5 size-2 rounded-full ring-2 ring-background"
262 aria-hidden
263 />
264 </span>
265 <Skeleton className="h-4 w-28 rounded-full" />
266 <Skeleton className="h-5 w-14 rounded-full" />
267 </div>
268 <Skeleton className="h-3 w-full max-w-xs rounded-full" />
269 </div>
270 <div className="flex shrink-0 items-center gap-2">
271 <Skeleton className="size-7 rounded-md" />
272 <Skeleton className="h-5 w-9 rounded-full" />
273 </div>
274 </div>
275 </div>
276 ))}
277 </SettingsSection>
278 );
279}
280
281function EmptySourceControlDiscovery({
282 error,
283 isPending,
284 onScan,
285}: {
286 readonly error: string | null;
287 readonly isPending: boolean;
288 readonly onScan: () => void;
289}) {
290 const hasError = error !== null;
291
292 return (
293 <SettingsSection title="Server environment">
294 <Empty className="min-h-88">
295 <EmptyMedia variant="icon">
296 <GitPullRequestIcon />
297 </EmptyMedia>
298 <EmptyHeader>
299 <EmptyTitle>
300 {hasError ? "Could not scan the server environment" : "Nothing detected yet"}
301 </EmptyTitle>
302 <EmptyDescription>
303 {hasError
304 ? error
305 : "Install Git on the server, add optional hosting integrations or credentials your workspace needs, then rescan."}
306 </EmptyDescription>
307 </EmptyHeader>
308 <EmptyContent>
309 <Button
310 size="sm"
311 variant="outline"
312 className="h-8 gap-1.5 px-3 text-xs"
313 onClick={onScan}
314 disabled={isPending}
315 >
316 <RefreshCwIcon className={cn("size-3.5", isPending && "animate-spin")} />
317 Scan
318 </Button>
319 </EmptyContent>
320 </Empty>
321 </SettingsSection>
322 );
323}
324
325export function SourceControlSettingsPanel() {
326 const discovery = useSourceControlDiscovery();
327
328 const result = discovery.data ?? EMPTY_DISCOVERY_RESULT;
329 const hasDiscoveryItems =
330 result.versionControlSystems.length > 0 || result.sourceControlProviders.length > 0;
331 const isInitialScanPending = discovery.isPending && discovery.data === null;
332 const handleScan = () => {
333 void refreshSourceControlDiscovery();
334 };
335 const scanButton = (
336 <Tooltip>
337 <TooltipTrigger
338 render={
339 <Button
340 size="icon-xs"
341 variant="ghost"
342 className="size-5 rounded-sm p-0 text-muted-foreground hover:text-foreground"
343 onClick={handleScan}
344 disabled={discovery.isPending}
345 aria-label="Rescan server environment"
346 >
347 <RefreshCwIcon className={cn("size-3", discovery.isPending && "animate-spin")} />
348 </Button>
349 }
350 />
351 <TooltipPopup side="top">Rescan Git and hosting integrations</TooltipPopup>
352 </Tooltip>
353 );
354
355 return (
356 <SettingsPageContainer>
357 {isInitialScanPending ? (
358 <>
359 <SourceControlSectionSkeleton title="Version Control" headerAction={scanButton} />
360 <SourceControlSectionSkeleton title="Source Control Providers" />
361 </>
362 ) : hasDiscoveryItems ? (
363 <>
364 {result.versionControlSystems.length > 0 ? (
365 <SettingsSection title="Version Control" headerAction={scanButton}>
366 {result.versionControlSystems.map((item) => (
367 <DiscoveryItemRow key={`vcs:${item.kind}`} item={item} />
368 ))}
369 </SettingsSection>
370 ) : null}
371
372 {result.sourceControlProviders.length > 0 ? (
373 <SettingsSection
374 title="Source Control Providers"
375 headerAction={result.versionControlSystems.length === 0 ? scanButton : null}
376 >
377 {result.sourceControlProviders.map((item) => (
378 <DiscoveryItemRow key={`provider:${item.kind}`} item={item} />
379 ))}
380 </SettingsSection>
381 ) : null}
382 </>
383 ) : (
384 <EmptySourceControlDiscovery
385 error={discovery.error}
386 isPending={discovery.isPending}
387 onScan={handleScan}
388 />
389 )}
390 </SettingsPageContainer>
391 );
392}