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

SourceControlSettings.tsx

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