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

SourceControlSettings.tsx

523 lines18.2 KB

1import { RefreshIcon } from "~/components/ui/refresh-icon";
2import { ChevronDownIcon } from "lucide-react";
3import * as Option from "effect/Option";
4import { useState, type ReactNode } from "react";
5import type {
6 SourceControlDiscoveryResult,
7 SourceControlProviderAuth,
8 SourceControlProviderDiscoveryItem,
9 VcsDriverKind,
10 VcsDiscoveryItem,
11} from "@t3tools/contracts";
12import { useSettingsScope } from "./SettingsScopeContext";
13import { ProjectDefaultsSettings } from "./ProjectDefaultsSettings";
14import { cn } from "../../lib/utils";
15import { useEnvironmentQuery } from "../../state/query";
16import { sourceControlEnvironment } from "../../state/sourceControl";
17import { Badge } from "../ui/badge";
18import { Button } from "../ui/button";
19import { Collapsible, CollapsibleContent } from "../ui/collapsible";
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";
31​
32import { GitIcon, JujutsuIcon, type Icon } from "../Icons";
33import { SourceControlHostSettings } from "./SourceControlHostSettings";
34import { SourceControlAccountSettings } from "./SourceControlAccountSettings";
35import { SourceControlAppSignInSettings } from "./SourceControlAppSignInSettings";
36import { SourceControlTokenSettings } from "./SourceControlTokenSettings";
37import { RedactedSensitiveText } from "./RedactedSensitiveText";
38import { SourceControlWritingSettingsSection } from "./SourceControlWritingSettings";
39import {
40 SettingsPageContainer,
41 SettingsSearchTarget,
42 SettingsSection,
43 useSettingsSearchTargetId,
44} from "./settingsLayout";
45import { searchableSetting, sourceControlHostSettingsSearchId } from "./settingsSearch";
46import { PullRequestGlyph } from "~/components/pullRequest/pullRequestIcons";
47import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
48import { sourceControlIcon } from "~/sourceControlPresentation";
49​
50const EMPTY_DISCOVERY_RESULT: SourceControlDiscoveryResult = {
51 versionControlSystems: [],
52 sourceControlProviders: [],
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;
61function optionLabel(value: Option.Option<string>): string | null {
62 return Option.getOrNull(value);
64​
65function isProviderDiscoveryItem(
66 item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem,
67): item is SourceControlProviderDiscoveryItem {
68 return "auth" in item;
70​
71function isVcsNotReady(item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem): boolean {
72 return !isProviderDiscoveryItem(item) && !item.implemented;
74​
75function authPresentation(auth: SourceControlProviderAuth): {
76 readonly label: string;
77 readonly badge: "warning" | null;
78} {
79 if (auth.status === "authenticated") {
80 return { label: "Authenticated", badge: null };
81 }
82 if (auth.status === "unauthenticated") {
83 return { label: "Not authenticated", badge: "warning" };
84 }
85 return { label: "Status unknown", badge: null };
87​
88function RedactedAccount(props: { readonly account: string | null }) {
89 return (
90 <RedactedSensitiveText
91 value={props.account}
92 ariaLabel="Toggle source control account visibility"
93 revealTooltip="Click to reveal account"
94 hideTooltip="Click to hide account"
95 />
96 );
98​
99function itemStatusDot(item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem): string {
100 if (isVcsNotReady(item)) return "bg-muted-foreground/35";
101 if (item.status !== "available") return "bg-warning";
102 if (isProviderDiscoveryItem(item) && item.auth.status !== "authenticated") return "bg-warning";
103 return "bg-success";
105​
106function sourceControlItemIcon(
107 item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem,
108): ReactNode {
109 const host = isProviderDiscoveryItem(item) ? sourceControlClients.find(item.kind) : undefined;
110 const Icon = isProviderDiscoveryItem(item)
111 ? host && sourceControlIcon(host)
112 : VCS_ICONS[item.kind];
113 return Icon ? <Icon className="size-4.5 text-foreground/80" aria-hidden /> : null;
115​
116function SourceControlItemMark({
117 item,
118}: {
119 readonly item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem;
120}) {
121 const dotClassName = itemStatusDot(item);
122 const icon = sourceControlItemIcon(item);
123​
124 if (!icon) {
125 return <span className={cn("size-2 shrink-0 rounded-full", dotClassName)} aria-hidden />;
126 }
127​
128 return (
129 <span className="relative inline-flex size-5 shrink-0 items-center justify-center">
130 {icon}
131 <span
132 className={cn(
133 "pointer-events-none absolute -left-0.5 -top-0.5 size-2 rounded-full ring-2 ring-background",
134 dotClassName,
135 )}
136 aria-hidden
137 />
138 </span>
139 );
141​
142function itemSummary({
143 item,
144 auth,
145 authAccount,
146}: {
147 readonly item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem;
148 readonly auth: SourceControlProviderAuth | null;
149 readonly authAccount: string | null;
150}) {
151 if (isVcsNotReady(item)) {
152 return <span>Support for {item.label} is coming soon.</span>;
153 }
154​
155 if (item.status !== "available") {
156 return <span>Not available on this server: {item.installHint}</span>;
157 }
158​
159 if (auth) {
160 if (auth.status === "authenticated") {
161 const authDetail = optionLabel(auth.detail);
162 return (
163 <>
164 <span>Authenticated</span>
165 {authAccount ? (
166 <>
167 <span aria-hidden>as</span>
168 <RedactedAccount account={authAccount} />
169 </>
170 ) : null}
171 {authDetail ? <span>· {authDetail}</span> : null}
172 </>
173 );
174 }
175​
176 if (!item.executable && auth.status === "unauthenticated") {
177 return <span>Available. {item.installHint}</span>;
178 }
179​
180 if (auth.status === "unauthenticated" && auth.accounts?.some((entry) => entry.authenticated)) {
181 return <span>{optionLabel(auth.detail) ?? `Every ${item.label} host is turned off.`}</span>;
182 }
183​
184 if (auth.status === "unauthenticated") {
185 return (
186 <span>
187 {item.label} is not authenticated on this server. Sign in or configure credentials using
188 the <code className="rounded bg-muted px-1 py-px text-2xs">{item.executable}</code> tool
189 on the server host to enable change request features.
190 </span>
191 );
192 }
193 const authDetail = optionLabel(auth.detail);
194 return (
195 <span>
196 Could not verify {item.label}. {authDetail ?? item.installHint}
197 </span>
198 );
199 }
200​
201 return <span>Available</span>;
203​
204function DiscoveryItemRow({
205 item,
206 children,
207}: {
208 readonly item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem;
209 readonly children?: ReactNode;
210}) {
211 const version = optionLabel(item.version);
212 const enabled = isProviderDiscoveryItem(item)
213 ? item.status === "available" && item.auth.status === "authenticated"
214 : item.status === "available" && item.implemented;
215 const auth = isProviderDiscoveryItem(item) ? item.auth : null;
216 const authStatus = auth ? authPresentation(auth) : null;
217 const authAccount = auth ? optionLabel(auth.account) : null;
218 const [isExpanded, setIsExpanded] = useState(false);
219 const hasDetails = children !== undefined;
220 const searchTargetId = useSettingsSearchTargetId();
221 const [seenSearchTargetId, setSeenSearchTargetId] = useState<string | null>(null);
222 const targetsThisRow = searchTargetId === sourceControlHostSettingsSearchId(item.kind);
223 if (targetsThisRow && !isExpanded && seenSearchTargetId !== searchTargetId) {
224 setIsExpanded(true);
225 }
226 if (seenSearchTargetId !== searchTargetId) setSeenSearchTargetId(searchTargetId);
227​
228 return (
229 <div
230 className={cn(
231 "first:rounded-t-xl last:rounded-b-xl transition-colors hover:bg-muted/20",
232 isVcsNotReady(item) && "opacity-80",
233 )}
234 >
235 <div className="px-3 py-3 sm:px-4">
236 <div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
237 <div className="min-w-0 flex-1 space-y-1">
238 <div className="flex min-w-0 flex-wrap items-center gap-2">
239 <SourceControlItemMark item={item} />
240 <span className="truncate text-sm font-medium text-foreground">{item.label}</span>
241 {version ? <code className="text-xs text-muted-foreground">{version}</code> : null}
242 {isVcsNotReady(item) ? (
243 <Badge variant="warning" size="sm">
244 Coming Soon
245 </Badge>
246 ) : null}
247 {authStatus?.badge ? (
248 <Badge variant={authStatus.badge} size="sm">
249 {authStatus.label}
250 </Badge>
251 ) : null}
252 </div>
253 <p className="flex min-w-0 flex-wrap items-center gap-x-1 text-xs leading-normal text-muted-foreground/80">
254 {itemSummary({ item, auth, authAccount })}
255 </p>
256 </div>
257 <div className="flex w-full shrink-0 items-center gap-2 sm:w-auto sm:justify-end">
258 {hasDetails ? (
259 <Button
260 size="icon-xs"
261 variant="ghost-muted"
262 onClick={() => setIsExpanded((open) => !open)}
263 aria-expanded={isExpanded}
264 aria-label={`Toggle ${item.label} details`}
265 >
266 <ChevronDownIcon
267 className={cn("size-3.5 transition-transform", isExpanded && "rotate-180")}
268 />
269 </Button>
270 ) : null}
271 {!isVcsNotReady(item) ? (
272 <Switch checked={enabled} disabled aria-label={`${item.label} availability`} />
273 ) : null}
274 </div>
275 </div>
276 </div>
277​
278 {hasDetails ? (
279 <Collapsible open={isExpanded} onOpenChange={setIsExpanded}>
280 <CollapsibleContent>
281 <div className="px-3 pb-4 pt-1 sm:px-4">{children}</div>
282 </CollapsibleContent>
283 </Collapsible>
284 ) : null}
285 </div>
286 );
288​
289function SourceControlSectionSkeleton({
290 title,
291 headerAction,
292}: {
293 readonly title: string;
294 readonly headerAction?: ReactNode;
295}) {
296 return (
297 <SettingsSection title={title} headerAction={headerAction}>
298 {SOURCE_CONTROL_SKELETON_ROWS.map((row) => (
299 <div key={row} className="first:rounded-t-xl last:rounded-b-xl px-3 py-3 sm:px-4">
300 <div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
301 <div className="min-w-0 flex-1 space-y-2">
302 <div className="flex items-center gap-2">
303 <span className="relative inline-flex size-5 shrink-0 items-center justify-center">
304 <Skeleton className="size-4.5" />
305 <Skeleton
306 shape="pill"
307 className="pointer-events-none absolute -left-0.5 -top-0.5 size-2"
308 aria-hidden
309 />
310 </span>
311 <Skeleton shape="pill" className="h-4 w-28" />
312 <Skeleton shape="pill" className="h-5 w-14" />
313 </div>
314 <Skeleton shape="pill" className="h-3 w-full max-w-xs" />
315 </div>
316 <div className="flex shrink-0 items-center gap-2">
317 <Skeleton className="size-7" />
318 <Skeleton shape="pill" className="h-5 w-9" />
319 </div>
320 </div>
321 </div>
322 ))}
323 </SettingsSection>
324 );
326​
327function EmptySourceControlDiscovery({
328 error,
329 isPending,
330 onScan,
331}: {
332 readonly error: string | null;
333 readonly isPending: boolean;
334 readonly onScan: () => void;
335}) {
336 const hasError = error !== null;
337​
338 return (
339 <SettingsSection id={searchableSetting("source-control").id} title="Server environment">
340 <Empty>
341 <EmptyMedia variant="icon">
342 <PullRequestGlyph.pullRequest />
343 </EmptyMedia>
344 <EmptyHeader>
345 <EmptyTitle>
346 {hasError ? "Could not scan the server environment" : "Nothing detected yet"}
347 </EmptyTitle>
348 <EmptyDescription>
349 {hasError
350 ? error
351 : "Install Git on the server, add optional hosting integrations or credentials your workspace needs, then rescan."}
352 </EmptyDescription>
353 </EmptyHeader>
354 <EmptyContent>
355 <Button size="sm" variant="outline" onClick={onScan} disabled={isPending}>
356 <RefreshIcon size="sm" refreshing={isPending} />
357 Scan
358 </Button>
359 </EmptyContent>
360 </Empty>
361 </SettingsSection>
362 );
364​
365export function SourceControlSettingsPanel() {
366 const { scope, environment, connectedEnvironments } = useSettingsScope();
367​
368 const environmentId =
369 environment?.connection.phase === "connected" ? environment.environmentId : null;
370 const aggregate = scope.environmentIds.length !== 1 && connectedEnvironments.length > 1;
371 const environmentSuffix = aggregate && environment ? ` · ${environment.label}` : "";
372 const discovery = useEnvironmentQuery(
373 environmentId === null
374 ? null
375 : sourceControlEnvironment.discovery({
376 environmentId,
377 input: {},
378 }),
379 );
380 const result = discovery.data ?? EMPTY_DISCOVERY_RESULT;
381 const hasVersionControlSystems = result.versionControlSystems.length > 0;
382 const hasDiscoveryItems = hasVersionControlSystems || result.sourceControlProviders.length > 0;
383 const isInitialScanPending = discovery.isPending && discovery.data === null;
384 const handleScan = () => {
385 discovery.refresh();
386 };
387 const hostSettingsPanel = (item: SourceControlProviderDiscoveryItem) => {
388 const definition = sourceControlClients.find(item.kind);
389 if (!definition?.settings || environmentId === null) return undefined;
390 const { credentials } = definition;
391 if (credentials) {
392 return (
393 <SettingsSearchTarget id={sourceControlHostSettingsSearchId(item.kind)}>
394 <div className="grid gap-6">
395 {credentials.appSignIn && item.appSignIn === true ? (
396 <SourceControlAppSignInSettings
397 key={`sign-in-${environmentId}`}
398 environmentId={environmentId}
399 definition={definition}
400 appSignIn={credentials.appSignIn}
401 account={Option.getOrNull(item.auth.account)}
402 onChanged={handleScan}
403 />
404 ) : null}
405 {credentials.appSignIn && item.appSignIn === true ? (
406 <div className="flex items-center gap-3 text-2xs text-muted-foreground">
407 <span aria-hidden className="h-px flex-1 bg-border" />
408 Or use your own credentials
409 <span aria-hidden className="h-px flex-1 bg-border" />
410 </div>
411 ) : null}
412 <SourceControlTokenSettings
413 key={`token-${environmentId}`}
414 environmentId={environmentId}
415 definition={definition}
416 credentials={credentials}
417 onSaved={handleScan}
418 />
419 {item.status === "available" ? (
420 <SourceControlAccountSettings
421 key={environmentId}
422 environmentId={environmentId}
423 definition={definition}
424 credentials={credentials}
425 auth={item.auth}
426 onSaved={handleScan}
427 />
428 ) : null}
429 </div>
430 </SettingsSearchTarget>
431 );
432 }
433 return (
434 <SettingsSearchTarget id={sourceControlHostSettingsSearchId(item.kind)}>
435 <SourceControlHostSettings
436 key={environmentId}
437 environmentId={environmentId}
438 definition={{ ...definition, settings: definition.settings }}
439 onSaved={handleScan}
440 />
441 </SettingsSearchTarget>
442 );
443 };
444 const scanButton = (
445 <Tooltip>
446 <TooltipTrigger
447 render={
448 <Button
449 size="icon-xs"
450 variant="ghost-muted"
451 onClick={handleScan}
452 disabled={discovery.isPending}
453 aria-label="Rescan server environment"
454 >
455 <RefreshIcon refreshing={discovery.isPending} />
456 </Button>
457 }
458 />
459 <TooltipPopup side="top">Rescan Git and hosting integrations</TooltipPopup>
460 </Tooltip>
461 );
462​
463 return (
464 <SettingsPageContainer>
465 <ProjectDefaultsSettings category="source-control" />
466 {environmentId === null ? (
467 <SettingsSection id={searchableSetting("source-control").id} title="Server environment">
468 <p className="px-4 py-3 text-sm text-muted-foreground">
469 Connect an environment to inspect its version control tools and hosting integrations.
470 </p>
471 </SettingsSection>
472 ) : isInitialScanPending ? (
473 <>
474 <SourceControlSectionSkeleton
475 title={`Version Control${environmentSuffix}`}
476 headerAction={scanButton}
477 />
478 <SourceControlSectionSkeleton title="Source control providers" />
479 </>
480 ) : hasDiscoveryItems ? (
481 <>
482 {hasVersionControlSystems ? (
483 <SettingsSection
484 id={searchableSetting("source-control").id}
485 title={`Version Control${environmentSuffix}`}
486 headerAction={scanButton}
487 >
488 {result.versionControlSystems.map((item) => (
489 <DiscoveryItemRow key={`vcs:${item.kind}`} item={item} />
490 ))}
491 </SettingsSection>
492 ) : null}
493​
494 {result.sourceControlProviders.length > 0 ? (
495 <SettingsSection
496 id={hasVersionControlSystems ? undefined : searchableSetting("source-control").id}
497 title={
498 hasVersionControlSystems
499 ? "Source control providers"
500 : `Source control providers${environmentSuffix}`
501 }
502 headerAction={hasVersionControlSystems ? null : scanButton}
503 >
504 {result.sourceControlProviders.map((item) => (
505 <DiscoveryItemRow key={`provider:${item.kind}`} item={item}>
506 {hostSettingsPanel(item)}
507 </DiscoveryItemRow>
508 ))}
509 </SettingsSection>
510 ) : null}
511 </>
512 ) : (
513 <EmptySourceControlDiscovery
514 error={discovery.error}
515 isPending={discovery.isPending}
516 onScan={handleScan}
517 />
518 )}
519​
520 <SourceControlWritingSettingsSection />
521 </SettingsPageContainer>
522 );