diff --git a/apps/web/src/cloud/linkEnvironment.test.ts b/apps/web/src/cloud/linkEnvironment.test.ts index 2cb80fac1a..a937151e52 100644 --- a/apps/web/src/cloud/linkEnvironment.test.ts +++ b/apps/web/src/cloud/linkEnvironment.test.ts @@ -29,7 +29,11 @@ import { } from "../environments/primary"; const getSavedEnvironmentSecretMock = vi.fn(); -const confirmRelayClientInstallMock = vi.fn(); +const relayClientInstallDialogHarness = vi.hoisted(() => ({ + requestConfirmation: vi.fn(), + reportProgress: vi.fn(), + finish: vi.fn(), +})); const getRelayClientStatusMock = vi.fn(); const installRelayClientMock = vi.fn(); const environmentConnectionMock = { @@ -70,15 +74,18 @@ const withCloudServices = ( vi.mock("../localApi", () => ({ ensureLocalApi: () => ({ - dialogs: { - confirm: confirmRelayClientInstallMock, - }, persistence: { getSavedEnvironmentSecret: getSavedEnvironmentSecretMock, }, }), })); +vi.mock("./relayClientInstallDialog", () => ({ + requestRelayClientInstallConfirmation: relayClientInstallDialogHarness.requestConfirmation, + reportRelayClientInstallProgress: relayClientInstallDialogHarness.reportProgress, + finishRelayClientInstall: relayClientInstallDialogHarness.finish, +})); + vi.mock("../environments/primary", () => ({ readPrimaryEnvironmentDescriptor: vi.fn(() => null), readPrimaryEnvironmentTarget: vi.fn(() => null), @@ -137,7 +144,7 @@ describe("web cloud link environment client", () => { createProofMock.mockClear(); vi.stubEnv("VITE_T3CODE_RELAY_URL", "https://relay.example.test"); getSavedEnvironmentSecretMock.mockResolvedValue("local-bearer"); - confirmRelayClientInstallMock.mockResolvedValue(true); + relayClientInstallDialogHarness.requestConfirmation.mockResolvedValue(true); getRelayClientStatusMock.mockResolvedValue(availableRelayClient()); installRelayClientMock.mockResolvedValue(availableRelayClient()); vi.mocked(readPrimaryEnvironmentDescriptor).mockReturnValue(null); @@ -181,6 +188,10 @@ describe("web cloud link environment client", () => { .mockResolvedValueOnce(Response.json(validChallenge())) .mockResolvedValueOnce(Response.json({ malformed: true })); vi.stubGlobal("fetch", fetchMock); + installRelayClientMock.mockImplementationOnce(async (onProgress) => { + onProgress({ type: "progress", stage: "downloading" }); + return availableRelayClient(); + }); yield* withCloudServices( linkPrimaryEnvironmentToCloud({ @@ -188,9 +199,16 @@ describe("web cloud link environment client", () => { }), ).pipe(Effect.flip); - expect(confirmRelayClientInstallMock).toHaveBeenCalledOnce(); + expect(relayClientInstallDialogHarness.requestConfirmation).toHaveBeenCalledWith( + "2026.5.2", + ); expect(getRelayClientStatusMock).toHaveBeenCalledOnce(); expect(installRelayClientMock).toHaveBeenCalledOnce(); + expect(relayClientInstallDialogHarness.reportProgress).toHaveBeenCalledWith({ + type: "progress", + stage: "downloading", + }); + expect(relayClientInstallDialogHarness.finish).toHaveBeenCalledOnce(); expect(installRelayClientMock.mock.invocationCallOrder[0]).toBeLessThan( fetchMock.mock.invocationCallOrder[0]!, ); diff --git a/apps/web/src/cloud/linkEnvironment.ts b/apps/web/src/cloud/linkEnvironment.ts index 21fdf52f43..4d34c854a4 100644 --- a/apps/web/src/cloud/linkEnvironment.ts +++ b/apps/web/src/cloud/linkEnvironment.ts @@ -43,6 +43,11 @@ import { } from "../environments/primary"; import { withPrimaryEnvironmentRequestInit } from "../environments/primary/requestInit"; import { resolveCloudPublicConfig } from "./publicConfig"; +import { + finishRelayClientInstall, + reportRelayClientInstallProgress, + requestRelayClientInstallConfirmation, +} from "./relayClientInstallDialog"; export function normalizeRelayBaseUrl(value: string | null | undefined): string | null { const trimmed = value?.trim(); @@ -67,9 +72,6 @@ const relayClientRpcError = (message: string) => (cause: unknown) => cause, }); -const RELAY_CLIENT_INSTALL_PROMPT = - "T3 Code needs the relay client to make this environment available through T3 Cloud. Download and install it now?"; - function ensureRelayClientAvailable( client: WsRpcClient, ): Effect.Effect { @@ -86,7 +88,7 @@ function ensureRelayClientAvailable( } const confirmed = yield* Effect.tryPromise({ - try: () => ensureLocalApi().dialogs.confirm(RELAY_CLIENT_INSTALL_PROMPT), + try: () => requestRelayClientInstallConfirmation(status.version), catch: relayClientRpcError("Could not confirm relay client installation."), }); if (!confirmed) { @@ -96,9 +98,9 @@ function ensureRelayClientAvailable( } const installed = yield* Effect.tryPromise({ - try: () => client.cloud.installRelayClient(), + try: () => client.cloud.installRelayClient(reportRelayClientInstallProgress), catch: relayClientRpcError("Could not install the relay client."), - }); + }).pipe(Effect.ensuring(Effect.sync(finishRelayClientInstall))); if (installed.status !== "available") { return yield* new CloudEnvironmentLinkError({ message: diff --git a/apps/web/src/cloud/relayClientInstallDialog.test.ts b/apps/web/src/cloud/relayClientInstallDialog.test.ts new file mode 100644 index 0000000000..b93a14e415 --- /dev/null +++ b/apps/web/src/cloud/relayClientInstallDialog.test.ts @@ -0,0 +1,50 @@ +import { beforeEach, describe, expect, it } from "vitest"; + +import { + finishRelayClientInstall, + readRelayClientInstallDialogState, + reportRelayClientInstallProgress, + requestRelayClientInstallConfirmation, + resetRelayClientInstallDialogForTests, + respondToRelayClientInstallConfirmation, +} from "./relayClientInstallDialog"; + +describe("relay client install dialog coordinator", () => { + beforeEach(() => { + resetRelayClientInstallDialogForTests(); + }); + + it("moves a confirmed installation through streamed progress stages", async () => { + const confirmation = requestRelayClientInstallConfirmation("2026.5.2"); + expect(readRelayClientInstallDialogState()).toEqual({ + status: "confirming", + version: "2026.5.2", + }); + + respondToRelayClientInstallConfirmation(true); + await expect(confirmation).resolves.toBe(true); + expect(readRelayClientInstallDialogState()).toEqual({ + status: "installing", + version: "2026.5.2", + stage: "checking", + }); + + reportRelayClientInstallProgress({ type: "progress", stage: "downloading" }); + expect(readRelayClientInstallDialogState()).toEqual({ + status: "installing", + version: "2026.5.2", + stage: "downloading", + }); + + finishRelayClientInstall(); + expect(readRelayClientInstallDialogState()).toEqual({ status: "idle" }); + }); + + it("returns to idle when installation is declined", async () => { + const confirmation = requestRelayClientInstallConfirmation("2026.5.2"); + respondToRelayClientInstallConfirmation(false); + + await expect(confirmation).resolves.toBe(false); + expect(readRelayClientInstallDialogState()).toEqual({ status: "idle" }); + }); +}); diff --git a/apps/web/src/cloud/relayClientInstallDialog.ts b/apps/web/src/cloud/relayClientInstallDialog.ts new file mode 100644 index 0000000000..4c0b251b50 --- /dev/null +++ b/apps/web/src/cloud/relayClientInstallDialog.ts @@ -0,0 +1,78 @@ +import type { + RelayClientInstallProgressEvent, + RelayClientInstallProgressStage, +} from "@t3tools/contracts"; + +export type RelayClientInstallDialogState = + | { readonly status: "idle" } + | { readonly status: "confirming"; readonly version: string } + | { + readonly status: "installing"; + readonly version: string; + readonly stage: RelayClientInstallProgressStage; + }; + +const idleState: RelayClientInstallDialogState = { status: "idle" }; +let state: RelayClientInstallDialogState = idleState; +let resolveConfirmation: ((confirmed: boolean) => void) | null = null; +const listeners = new Set<() => void>(); + +function publish(next: RelayClientInstallDialogState) { + state = next; + for (const listener of listeners) { + listener(); + } +} + +export function readRelayClientInstallDialogState(): RelayClientInstallDialogState { + return state; +} + +export function subscribeRelayClientInstallDialog(listener: () => void): () => void { + listeners.add(listener); + return () => { + listeners.delete(listener); + }; +} + +export function requestRelayClientInstallConfirmation(version: string): Promise { + if (state.status !== "idle") { + return Promise.reject(new Error("A relay client installation is already in progress.")); + } + + publish({ status: "confirming", version }); + return new Promise((resolve) => { + resolveConfirmation = resolve; + }); +} + +export function respondToRelayClientInstallConfirmation(confirmed: boolean): void { + if (state.status !== "confirming" || !resolveConfirmation) { + return; + } + + const resolve = resolveConfirmation; + resolveConfirmation = null; + publish( + confirmed ? { status: "installing", version: state.version, stage: "checking" } : idleState, + ); + resolve(confirmed); +} + +export function reportRelayClientInstallProgress(event: RelayClientInstallProgressEvent): void { + if (state.status !== "installing" || event.type !== "progress") { + return; + } + publish({ ...state, stage: event.stage }); +} + +export function finishRelayClientInstall(): void { + resolveConfirmation?.(false); + resolveConfirmation = null; + publish(idleState); +} + +export function resetRelayClientInstallDialogForTests(): void { + finishRelayClientInstall(); + listeners.clear(); +} diff --git a/apps/web/src/components/cloud/RelayClientInstallDialog.browser.tsx b/apps/web/src/components/cloud/RelayClientInstallDialog.browser.tsx new file mode 100644 index 0000000000..a454861fd5 --- /dev/null +++ b/apps/web/src/components/cloud/RelayClientInstallDialog.browser.tsx @@ -0,0 +1,46 @@ +import "../../index.css"; + +import { page } from "vitest/browser"; +import { beforeEach, describe, expect, it } from "vitest"; +import { render } from "vitest-browser-react"; + +import { + finishRelayClientInstall, + reportRelayClientInstallProgress, + requestRelayClientInstallConfirmation, + resetRelayClientInstallDialogForTests, +} from "../../cloud/relayClientInstallDialog"; +import { RelayClientInstallDialog } from "./RelayClientInstallDialog"; + +describe("RelayClientInstallDialog", () => { + beforeEach(() => { + resetRelayClientInstallDialogForTests(); + }); + + it("confirms installation and renders streamed progress", async () => { + render(); + const confirmation = requestRelayClientInstallConfirmation("2026.5.2"); + + await expect.element(page.getByText("Install relay client?")).toBeInTheDocument(); + await expect.element(page.getByText(/version 2026\.5\.2 locally/)).toBeInTheDocument(); + + await page.getByRole("button", { name: "Download and install" }).click(); + await expect(confirmation).resolves.toBe(true); + await expect + .element(page.getByRole("heading", { name: "Installing relay client" })) + .toBeInTheDocument(); + + reportRelayClientInstallProgress({ type: "progress", stage: "downloading" }); + await expect + .element(page.getByText("Downloading relay client").first()) + .toHaveTextContent("Downloading relay client"); + await expect + .element(page.getByText("Downloading relay client").first()) + .toHaveAttribute("class", expect.stringContaining("font-medium")); + + finishRelayClientInstall(); + await expect + .element(page.getByRole("heading", { name: "Installing relay client" })) + .not.toBeInTheDocument(); + }); +}); diff --git a/apps/web/src/components/cloud/RelayClientInstallDialog.tsx b/apps/web/src/components/cloud/RelayClientInstallDialog.tsx new file mode 100644 index 0000000000..fabe857c91 --- /dev/null +++ b/apps/web/src/components/cloud/RelayClientInstallDialog.tsx @@ -0,0 +1,129 @@ +import { CircleCheckIcon, CircleIcon, DownloadIcon } from "lucide-react"; +import { useSyncExternalStore } from "react"; +import type { RelayClientInstallProgressStage } from "@t3tools/contracts"; + +import { + readRelayClientInstallDialogState, + respondToRelayClientInstallConfirmation, + subscribeRelayClientInstallDialog, +} from "../../cloud/relayClientInstallDialog"; +import { Button } from "../ui/button"; +import { + Dialog, + DialogDescription, + DialogFooter, + DialogHeader, + DialogPanel, + DialogPopup, + DialogTitle, +} from "../ui/dialog"; +import { Spinner } from "../ui/spinner"; + +const installSteps: ReadonlyArray<{ + readonly stage: RelayClientInstallProgressStage; + readonly label: string; +}> = [ + { stage: "checking", label: "Checking current installation" }, + { stage: "waiting_for_lock", label: "Waiting for installer" }, + { stage: "downloading", label: "Downloading relay client" }, + { stage: "verifying", label: "Verifying download" }, + { stage: "installing", label: "Installing relay client" }, + { stage: "validating", label: "Validating executable" }, + { stage: "activating", label: "Activating installation" }, +]; + +export function RelayClientInstallDialog() { + const state = useSyncExternalStore( + subscribeRelayClientInstallDialog, + readRelayClientInstallDialogState, + readRelayClientInstallDialogState, + ); + const isConfirming = state.status === "confirming"; + const isInstalling = state.status === "installing"; + const activeStepIndex = isInstalling + ? installSteps.findIndex(({ stage }) => stage === state.stage) + : -1; + + return ( + { + if (!open && isConfirming) { + respondToRelayClientInstallConfirmation(false); + } + }} + > + + +
+ +
+ + {isInstalling ? "Installing relay client" : "Install relay client?"} + + + {isInstalling + ? "T3 Code is preparing this environment for secure access through T3 Cloud." + : "T3 Code needs the relay client to make this environment available through T3 Cloud."} + +
+ + {isInstalling ? ( +
    + {installSteps.map((step, index) => { + const isComplete = activeStepIndex > index; + const isActive = activeStepIndex === index; + return ( +
  1. + {isComplete ? ( + + ) : isActive ? ( + + ) : ( + + )} + + {step.label} + +
  2. + ); + })} +
  3. + {activeStepIndex >= 0 + ? installSteps[activeStepIndex]?.label + : "Starting installation"} +
  4. +
+ ) : ( +
+

Managed relay client

+

+ T3 Code will download and install version{" "} + {state.status === "confirming" ? state.version : ""} locally. +

+
+ )} +
+ {isConfirming ? ( + + + + + ) : null} +
+
+ ); +} diff --git a/apps/web/src/routes/__root.tsx b/apps/web/src/routes/__root.tsx index ec525fa231..88283d451c 100644 --- a/apps/web/src/routes/__root.tsx +++ b/apps/web/src/routes/__root.tsx @@ -13,6 +13,7 @@ import { QueryClient, useQueryClient } from "@tanstack/react-query"; import { APP_DISPLAY_NAME } from "../branding"; import { AppSidebarLayout } from "../components/AppSidebarLayout"; import { CommandPalette } from "../components/CommandPalette"; +import { RelayClientInstallDialog } from "../components/cloud/RelayClientInstallDialog"; import { SshPasswordPromptDialog } from "../components/desktop/SshPasswordPromptDialog"; import { ProviderUpdateLaunchNotification } from "../components/ProviderUpdateLaunchNotification"; import { @@ -135,6 +136,7 @@ function RootRouteView() { {primaryEnvironmentAuthenticated ? : null} {primaryEnvironmentAuthenticated ? : null} + {primaryEnvironmentAuthenticated ? : null}