From f4180a404f159a38218216c2308fd6ff86bf1a6e Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 2 May 2026 14:02:42 -0700 Subject: [PATCH] Polish source control settings Co-authored-by: codex --- .../SourceControlDiscovery.test.ts | 1 - .../sourceControl/SourceControlDiscovery.ts | 8 - .../settings/SettingsPanels.browser.tsx | 151 ++++- .../settings/SourceControlSettings.tsx | 537 ++++++++++++------ .../components/settings/settingsLayout.tsx | 4 +- .../src/lib/sourceControlDiscoveryState.ts | 20 +- packages/contracts/src/vcs.ts | 2 +- 7 files changed, 523 insertions(+), 200 deletions(-) diff --git a/apps/server/src/sourceControl/SourceControlDiscovery.test.ts b/apps/server/src/sourceControl/SourceControlDiscovery.test.ts index 3146212db4..53ab480659 100644 --- a/apps/server/src/sourceControl/SourceControlDiscovery.test.ts +++ b/apps/server/src/sourceControl/SourceControlDiscovery.test.ts @@ -74,7 +74,6 @@ Logged in to github.com account juliusmarminge (keyring) [ { kind: "git", implemented: true, status: "available" }, { kind: "jj", implemented: false, status: "missing" }, - { kind: "sapling", implemented: false, status: "missing" }, ], ); assert.deepStrictEqual( diff --git a/apps/server/src/sourceControl/SourceControlDiscovery.ts b/apps/server/src/sourceControl/SourceControlDiscovery.ts index a3b309995b..7d8b5bc2d8 100644 --- a/apps/server/src/sourceControl/SourceControlDiscovery.ts +++ b/apps/server/src/sourceControl/SourceControlDiscovery.ts @@ -63,14 +63,6 @@ const VCS_PROBES: ReadonlyArray = [ implemented: false, installHint: "Install Jujutsu with `brew install jj` or from https://github.com/jj-vcs/jj.", }, - { - kind: "sapling", - label: "Sapling", - executable: "sl", - versionArgs: ["--version"], - implemented: false, - installHint: "Install Sapling (`sl`) from https://sapling-scm.com/.", - }, ]; const SOURCE_CONTROL_PROVIDER_PROBES: ReadonlyArray = [ diff --git a/apps/web/src/components/settings/SettingsPanels.browser.tsx b/apps/web/src/components/settings/SettingsPanels.browser.tsx index 4406d1607c..51cfe65327 100644 --- a/apps/web/src/components/settings/SettingsPanels.browser.tsx +++ b/apps/web/src/components/settings/SettingsPanels.browser.tsx @@ -11,17 +11,19 @@ import { type DesktopUpdateState, type LocalApi, type ServerConfig, + type SourceControlDiscoveryResult, } from "@t3tools/contracts"; -import { DateTime } from "effect"; +import { DateTime, Option } from "effect"; import { page } from "vitest/browser"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { render } from "vitest-browser-react"; import { __resetLocalApiForTests } from "../../localApi"; -import { AppAtomRegistryProvider } from "../../rpc/atomRegistry"; +import { AppAtomRegistryProvider, resetAppAtomRegistryForTests } from "../../rpc/atomRegistry"; import { resetServerStateForTests, setServerConfigSnapshot } from "../../rpc/serverState"; import { ConnectionsSettings } from "./ConnectionsSettings"; import { GeneralSettingsPanel } from "./SettingsPanels"; +import { SourceControlSettingsPanel } from "./SourceControlSettings"; const authAccessHarness = vi.hoisted(() => { type Snapshot = AuthAccessSnapshot; @@ -723,3 +725,148 @@ describe("GeneralSettingsPanel observability", () => { await expect.element(page.getByPlaceholder("Optional")).toBeInTheDocument(); }); }); + +describe("SourceControlSettingsPanel discovery states", () => { + let mounted: + | (Awaited> & { + cleanup?: () => Promise; + unmount?: () => Promise; + }) + | null = null; + + beforeEach(async () => { + resetAppAtomRegistryForTests(); + await __resetLocalApiForTests(); + document.body.innerHTML = ""; + }); + + afterEach(async () => { + if (mounted) { + const teardown = mounted.cleanup ?? mounted.unmount; + await teardown?.call(mounted).catch(() => {}); + } + mounted = null; + Reflect.deleteProperty(window, "nativeApi"); + document.body.innerHTML = ""; + await __resetLocalApiForTests(); + resetAppAtomRegistryForTests(); + }); + + function setSourceControlDiscoveryStub( + discoverSourceControl: () => Promise, + ) { + window.nativeApi = { + server: { + discoverSourceControl, + }, + } as LocalApi; + } + + it("shows skeleton sections while the first source control scan is pending", async () => { + setSourceControlDiscoveryStub(() => new Promise(() => {})); + + mounted = await render( + + + , + ); + + await expect.element(page.getByText("Version Control")).toBeInTheDocument(); + await expect.element(page.getByText("Source Control Providers")).toBeInTheDocument(); + await expect + .element(page.getByRole("button", { name: "Scan source control tools" })) + .toBeDisabled(); + await expect.element(page.getByText("No source control tools found")).not.toBeInTheDocument(); + }); + + it("uses the shared empty state when discovery completes without tools", async () => { + setSourceControlDiscoveryStub(async () => ({ + versionControlSystems: [], + sourceControlProviders: [], + })); + + mounted = await render( + + + , + ); + + await expect.element(page.getByText("No source control tools found")).toBeInTheDocument(); + await expect + .element(page.getByText("Install a supported Git or pull request CLI, then scan again.")) + .toBeInTheDocument(); + await expect.element(page.getByRole("button", { name: "Scan" })).toBeInTheDocument(); + }); + + it("keeps discovered rows instead of showing the empty state", async () => { + setSourceControlDiscoveryStub(async () => ({ + versionControlSystems: [ + { + kind: "git", + label: "Git", + executable: "git", + implemented: true, + status: "available", + version: Option.some("git version 2.50.0"), + installHint: "Install Git.", + detail: Option.none(), + }, + ], + sourceControlProviders: [], + })); + + mounted = await render( + + + , + ); + + await expect.element(page.getByRole("heading", { name: "Git" })).toBeInTheDocument(); + await expect.element(page.getByText("No source control tools found")).not.toBeInTheDocument(); + }); + + it("does not rescan on remount while the discovery atom is fresh", async () => { + let calls = 0; + setSourceControlDiscoveryStub(async () => { + calls += 1; + return { + versionControlSystems: [ + { + kind: "git", + label: "Git", + executable: "git", + implemented: true, + status: "available", + version: Option.some("git version 2.50.0"), + installHint: "Install Git.", + detail: Option.none(), + }, + ], + sourceControlProviders: [], + }; + }); + + mounted = await render( + + + , + ); + + await expect.element(page.getByRole("heading", { name: "Git" })).toBeInTheDocument(); + expect(calls).toBe(1); + + const teardown = mounted.cleanup ?? mounted.unmount; + await teardown?.call(mounted).catch(() => {}); + mounted = null; + document.body.innerHTML = ""; + + mounted = await render( + + + , + ); + + await expect.element(page.getByRole("heading", { name: "Git" })).toBeInTheDocument(); + expect(calls).toBe(1); + }); +}); diff --git a/apps/web/src/components/settings/SourceControlSettings.tsx b/apps/web/src/components/settings/SourceControlSettings.tsx index 96993ab777..c5f7909e86 100644 --- a/apps/web/src/components/settings/SourceControlSettings.tsx +++ b/apps/web/src/components/settings/SourceControlSettings.tsx @@ -1,8 +1,8 @@ -import { ChevronDownIcon, GitBranchIcon, GitPullRequestIcon, RefreshCwIcon } from "lucide-react"; +import { GitPullRequestIcon, RefreshCwIcon } from "lucide-react"; import { Option } from "effect"; -import { useMemo, useState } from "react"; +import { type ReactNode, useId } from "react"; import type { - SourceControlDiscoveryItem, + SourceControlProviderKind, SourceControlDiscoveryResult, SourceControlProviderAuth, SourceControlProviderDiscoveryItem, @@ -16,8 +16,18 @@ import { } from "../../lib/sourceControlDiscoveryState"; import { Badge } from "../ui/badge"; import { Button } from "../ui/button"; -import { Collapsible, CollapsibleContent } from "../ui/collapsible"; +import { + Empty, + EmptyContent, + EmptyDescription, + EmptyHeader, + EmptyMedia, + EmptyTitle, +} from "../ui/empty"; +import { Skeleton } from "../ui/skeleton"; import { Switch } from "../ui/switch"; +import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; +import { GitHubIcon, type Icon } from "../Icons"; import { RedactedSensitiveText } from "./RedactedSensitiveText"; import { SettingsPageContainer, SettingsSection } from "./settingsLayout"; @@ -26,6 +36,134 @@ const EMPTY_DISCOVERY_RESULT: SourceControlDiscoveryResult = { sourceControlProviders: [], }; +const GitLabIcon: Icon = (props) => ( + + + + + + +); + +const AzureDevOpsIcon: Icon = (props) => { + const id = useId().replaceAll(":", ""); + const gradientA = `${id}-azure-a`; + const gradientB = `${id}-azure-b`; + const gradientC = `${id}-azure-c`; + + return ( + + + + + + + + + + + + + + + + + + + + + + + + ); +}; + +const BitbucketIcon: Icon = (props) => { + const id = useId().replaceAll(":", ""); + const gradientId = `${id}-bitbucket-a`; + + return ( + + + + + + + + + + ); +}; + +const SOURCE_CONTROL_PROVIDER_ICONS: Partial> = { + github: GitHubIcon, + gitlab: GitLabIcon, + "azure-devops": AzureDevOpsIcon, + bitbucket: BitbucketIcon, +}; + function optionLabel(value: Option.Option): string | null { return Option.getOrNull(value); } @@ -38,25 +176,15 @@ function isProviderDiscoveryItem( function authPresentation(auth: SourceControlProviderAuth): { readonly label: string; - readonly badge: "success" | "warning" | "outline"; + readonly badge: "warning" | null; } { if (auth.status === "authenticated") { - return { label: "Signed in", badge: "success" }; + return { label: "Signed in", badge: null }; } if (auth.status === "unauthenticated") { return { label: "Sign in", badge: "warning" }; } - return { label: "Auth unknown", badge: "outline" }; -} - -function authSummary(auth: SourceControlProviderAuth): string { - if (auth.status === "authenticated") { - return "Authenticated"; - } - if (auth.status === "unauthenticated") { - return "Not authenticated"; - } - return "Auth not checked"; + return { label: "Sign in", badge: null }; } function RedactedAccount(props: { readonly account: string | null }) { @@ -70,57 +198,93 @@ function RedactedAccount(props: { readonly account: string | null }) { ); } -function statusPresentation(item: SourceControlDiscoveryItem): { - readonly label: string; - readonly badge: "success" | "warning" | "outline"; - readonly dot: string; -} { - if (item.implemented && item.status === "available") { - return { - label: "Ready", - badge: "success", - dot: "bg-success", - }; +function itemStatusDot(item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem): string { + if (!item.implemented) return "bg-muted-foreground/35"; + if (item.status !== "available") return "bg-warning"; + if (isProviderDiscoveryItem(item) && item.auth.status !== "authenticated") return "bg-warning"; + return "bg-success"; +} + +function SourceControlItemMark({ + item, +}: { + readonly item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem; +}) { + const dotClassName = itemStatusDot(item); + const Icon = isProviderDiscoveryItem(item) ? SOURCE_CONTROL_PROVIDER_ICONS[item.kind] : null; + + if (!Icon) { + return ; } - if (item.implemented) { - return { - label: "CLI missing", - badge: "warning", - dot: "bg-warning", - }; + + return ( + + + + + ); +} + +function itemSummary({ + item, + auth, + authAccount, +}: { + readonly item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem; + readonly auth: SourceControlProviderAuth | null; + readonly authAccount: string | null; +}) { + if (!item.implemented) { + return Support for {item.label} is coming soon.; } - if (item.status === "available") { - return { - label: "Detected", - badge: "outline", - dot: "bg-muted-foreground/60", - }; + + if (item.status !== "available") { + return Not found - {item.installHint}; } - return { - label: "Placeholder", - badge: "outline", - dot: "bg-muted-foreground/40", - }; + + if (auth) { + if (auth.status === "authenticated") { + return ( + <> + Authenticated + {authAccount ? ( + <> + as + + + ) : null} + + ); + } + if (auth.status === "unauthenticated") { + return Sign in with the {item.executable} CLI to enable pull request actions.; + } + return ( + + Install and sign in with the {item.executable} CLI to enable pull request actions. + + ); + } + + return Available; } function DiscoveryItemRow({ item, - expanded, - onExpandedChange, }: { readonly item: VcsDiscoveryItem | SourceControlProviderDiscoveryItem; - readonly expanded: boolean; - readonly onExpandedChange: (expanded: boolean) => void; }) { - const status = statusPresentation(item); const version = optionLabel(item.version); - const detail = optionLabel(item.detail); const enabled = item.implemented && item.status === "available"; const auth = isProviderDiscoveryItem(item) ? item.auth : null; const authStatus = auth ? authPresentation(auth) : null; const authAccount = auth ? optionLabel(auth.account) : null; - const authHost = auth ? optionLabel(auth.host) : null; - const authDetail = auth ? optionLabel(auth.detail) : null; return (
- +

{item.label}

- - {status.label} - + {version ? {version} : null} {!item.implemented ? ( - - Not in this branch + + Coming Soon ) : null} - {authStatus ? ( + {authStatus?.badge ? ( {authStatus.label} ) : null}

- CLI - - {item.executable} - - {version ? - {version} : null} - {auth ? ( - <> - - - {authSummary(auth)} - {authAccount ? : null} - - ) : null} + {itemSummary({ item, auth, authAccount })}

- - + {item.implemented ? ( + + ) : null}
- - - -
-
-
Command
-
{item.executable}
-
Version
-
{version ?? "Not detected"}
- {auth ? ( - <> -
Auth
-
-
- {authSummary(auth)} - {authAccount ? : null} - {authHost ? ( - on {authHost} - ) : null} -
- {authDetail ? ( -
{authDetail}
- ) : null} -
- - ) : null} -
Install
-
{item.installHint}
-
Build
-
- {item.implemented - ? "Enabled in this branch and available for repository routing when the CLI is present." - : "Placeholder only in this branch. The matching driver/provider PR enables this row."} -
- {detail ? ( - <> -
Probe
-
{detail}
- - ) : null} -
-
-
-
); } +function SourceControlSectionSkeleton({ + title, + headerAction, +}: { + readonly title: string; + readonly headerAction?: ReactNode; +}) { + return ( + + {Array.from({ length: 2 }, (_, index) => ( +
+
+
+
+ + + +
+ +
+
+ + +
+
+
+ ))} +
+ ); +} + +function EmptySourceControlDiscovery({ + error, + isPending, + onScan, +}: { + readonly error: string | null; + readonly isPending: boolean; + readonly onScan: () => void; +}) { + const hasError = error !== null; + + return ( + + + + + + + + {hasError ? "Could not scan source control" : "No source control tools found"} + + + {hasError ? error : "Install a supported Git or pull request CLI, then scan again."} + + + + + + + + ); +} + export function SourceControlSettingsPanel() { const discovery = useSourceControlDiscovery(); - const [expanded, setExpanded] = useState>>({}); const result = discovery.data ?? EMPTY_DISCOVERY_RESULT; - const statusText = useMemo(() => { - if (discovery.isPending) return "Scanning installed tools..."; - if (discovery.error) return discovery.error; - return "Detected source control tools on this system."; - }, [discovery.error, discovery.isPending]); - - const setItemExpanded = (key: string, value: boolean) => { - setExpanded((current) => ({ ...current, [key]: value })); + const hasDiscoveryItems = + result.versionControlSystems.length > 0 || result.sourceControlProviders.length > 0; + const isInitialScanPending = discovery.isPending && discovery.data === null; + const handleScan = () => { + void refreshSourceControlDiscovery(); }; + const scanButton = ( + + + + + } + /> + Scan source control tools + + ); return ( -
-

Source Control

-

{statusText}

-
+ {isInitialScanPending ? ( + <> + + + + ) : hasDiscoveryItems ? ( + <> + {result.versionControlSystems.length > 0 ? ( + + {result.versionControlSystems.map((item) => ( + + ))} + + ) : null} - } - headerAction={ - - } - > - {result.versionControlSystems.map((item) => ( - setItemExpanded(`vcs:${item.kind}`, value)} - /> - ))} - - - } - > - {result.sourceControlProviders.map((item) => ( - setItemExpanded(`provider:${item.kind}`, value)} - /> - ))} - + {result.sourceControlProviders.length > 0 ? ( + + {result.sourceControlProviders.map((item) => ( + + ))} + + ) : null} + + ) : ( + + )}
); } diff --git a/apps/web/src/components/settings/settingsLayout.tsx b/apps/web/src/components/settings/settingsLayout.tsx index f41e32f9e5..98769aca74 100644 --- a/apps/web/src/components/settings/settingsLayout.tsx +++ b/apps/web/src/components/settings/settingsLayout.tsx @@ -28,13 +28,13 @@ export function SettingsSection({ }) { return (
-
+

{icon} {title}

- {headerAction} +
{headerAction}
{children} diff --git a/apps/web/src/lib/sourceControlDiscoveryState.ts b/apps/web/src/lib/sourceControlDiscoveryState.ts index 5189c22a12..fad4f53490 100644 --- a/apps/web/src/lib/sourceControlDiscoveryState.ts +++ b/apps/web/src/lib/sourceControlDiscoveryState.ts @@ -8,18 +8,32 @@ import { sourceControlDiscoveryStateAtom, } from "@t3tools/client-runtime"; import type { SourceControlDiscoveryResult } from "@t3tools/contracts"; -import { useEffect } from "react"; +import { Effect } from "effect"; +import { Atom } from "effect/unstable/reactivity"; import { readLocalApi } from "../localApi"; import { appAtomRegistry } from "../rpc/atomRegistry"; const SOURCE_CONTROL_DISCOVERY_TARGET = { key: "primary" } as const; +const SOURCE_CONTROL_DISCOVERY_STALE_TIME_MS = 30_000; +const SOURCE_CONTROL_DISCOVERY_IDLE_TTL_MS = 5 * 60_000; export const sourceControlDiscoveryManager = createSourceControlDiscoveryManager({ getRegistry: () => appAtomRegistry, getClient: () => readLocalApi()?.server ?? null, }); +const sourceControlDiscoveryAutoRefreshAtom = Atom.make(() => + Effect.promise(() => refreshSourceControlDiscovery()), +).pipe( + Atom.swr({ + staleTime: SOURCE_CONTROL_DISCOVERY_STALE_TIME_MS, + revalidateOnMount: true, + }), + Atom.setIdleTTL(SOURCE_CONTROL_DISCOVERY_IDLE_TTL_MS), + Atom.withLabel("source-control-discovery:auto-refresh"), +); + export function refreshSourceControlDiscovery(): Promise { return sourceControlDiscoveryManager.refresh(SOURCE_CONTROL_DISCOVERY_TARGET); } @@ -31,9 +45,7 @@ export function resetSourceControlDiscoveryStateForTests(): void { export function useSourceControlDiscovery(): SourceControlDiscoveryState { const targetKey = getSourceControlDiscoveryTargetKey(SOURCE_CONTROL_DISCOVERY_TARGET); - useEffect(() => { - void sourceControlDiscoveryManager.refresh(SOURCE_CONTROL_DISCOVERY_TARGET); - }, []); + useAtomValue(sourceControlDiscoveryAutoRefreshAtom); const state = useAtomValue( targetKey !== null diff --git a/packages/contracts/src/vcs.ts b/packages/contracts/src/vcs.ts index 72ba544147..2dd09cf04c 100644 --- a/packages/contracts/src/vcs.ts +++ b/packages/contracts/src/vcs.ts @@ -1,7 +1,7 @@ import { Schema } from "effect"; import { TrimmedNonEmptyString } from "./baseSchemas.ts"; -export const VcsDriverKind = Schema.Literals(["git", "jj", "sapling", "unknown"]); +export const VcsDriverKind = Schema.Literals(["git", "jj", "unknown"]); export type VcsDriverKind = typeof VcsDriverKind.Type; export const VcsFreshnessSource = Schema.Literals([