import { errorDisplayMessage, thrownErrorMessage } from "@t3tools/client-runtime/errors"; import { m as resolveMessage } from "~/i18n/messages"; import { managedCapabilityDenial } from "@t3tools/client-runtime/contract-labels"; import { SessionPermissions } from "./SessionPermissions"; import { useSearch } from "@tanstack/react-router"; import { ChevronRightIcon, EllipsisIcon } from "lucide-react"; import { useAtomValue } from "@effect/atom-react"; import { type ReactNode, useState } from "react"; import { T, useGT, useMessages, Var } from "gt-react"; import { AuthAccessReadScope, AuthAccessWriteScope, AuthEnvironmentMaintainScope, AuthRelayReadScope, AuthRelayWriteScope, type AuthClientSession, type AuthPairingLink, type AuthPairingCredentialResult, type AdvertisedEndpoint, type DesktopServerExposureState, type DesktopWslState, EnvironmentId, resolveEnvironmentMachineKind, } from "@t3tools/contracts"; import { isAtomCommandInterrupted, settlePromise, squashAtomCommandFailure, } from "@t3tools/client-runtime/state/runtime"; import * as Cause from "effect/Cause"; import { isLocalEnvironmentDisabled } from "../../localEnvironment"; import { applyWslEnableSelection, connectDeviceName, isWslSettingsRowVisible, missingTailscaleEndpointDescription, resolveLocalVersionStatusLabel, resolveTailscaleServeBaseUrl, } from "./ConnectionsSettings.logic"; import { settingsScopeEnvironmentLabel } from "./settingsScopeAxis"; import { useAccessEnvironment } from "./useAccessEnvironment"; import { SettingsPageContainer, SettingsRow, SettingsSection } from "./settingsLayout"; import { EnvironmentMachineSummary } from "./EnvironmentMachineSummary"; import { ManagedByOrganization } from "../ManagedPolicy"; import { LocalEnvironmentSetting } from "./LocalEnvironmentSetting"; import { useSearchableSetting } from "./settingsSearch"; import { ConnectDeviceApprovalSettings } from "./ConnectDeviceApprovalSettings"; import { EnvironmentIconMenu } from "./EnvironmentIconPicker"; import { FoldedSettingsSection } from "./FoldedSettingsSection"; import { LinkedEnvironmentsSettings } from "./LinkedEnvironmentsSettings"; import { usePeerLinkHosts } from "~/state/peerLinks"; import { LoadBalancingSettings } from "./LoadBalancingSettings"; import { PullRequestRoutingSettings } from "./PullRequestRoutingSettings"; import { Input } from "../ui/input"; import { Dialog, DialogClose, DialogFooter, DialogDescription, DialogHeader, DialogPanel, DialogPopup, DialogTitle, } from "../ui/dialog"; import { ScrollArea } from "../ui/scroll-area"; import { AlertDialog, AlertDialogClose, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogPopup, AlertDialogTitle, } from "../ui/alert-dialog"; import { Spinner } from "../ui/spinner"; import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select"; import { Menu, MenuPopup, MenuTrigger } from "../ui/menu"; import { Switch } from "../ui/switch"; import { stackedThreadToast, toastManager } from "../ui/toast"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import { Button } from "../ui/button"; import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon"; import { type ServerClientSessionRecord } from "~/environments/primary"; import { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal"; import { useUiStateStore } from "~/uiStateStore"; import { resolveServerConfigVersionMismatch, resolveServerSelfUpdateCapability, supportsDesktopAppUpdate, } from "~/versionSkew"; import { hasCloudPublicConfig } from "~/cloud/publicConfig"; import { RemoveT3ConnectEnvironmentDialog } from "../clerk/RemoveT3ConnectEnvironmentDialog"; import { authEnvironment } from "~/state/auth"; import { environmentCatalog } from "~/connection/catalog"; import { readEnvironmentScope, useEnvironmentScope, useEnvironmentSessionState, } from "~/state/session"; import { useEnvironmentQuery } from "~/state/query"; import { desktopNetworkAccessStateAtom, refreshDesktopNetworkAccessState, } from "~/state/desktopNetworkAccess"; import { desktopWslAccessStateAtom, refreshDesktopWslState } from "~/state/desktopWslState"; import { useDesktopUpdateState } from "~/state/desktopUpdate"; import { type EnvironmentPresentation, useEnvironment, useEnvironments, usePrimaryEnvironment, } from "~/state/environments"; import { requestConfirmDialog } from "~/confirmDialog"; import { useUpdateEnvironmentSettings } from "~/hooks/useSettings"; import { useAtomCommand } from "../../state/use-atom-command"; import { environmentIdsKey } from "~/state/environmentAtomList"; import { environmentUpdateStatesAtom, serverEnvironment } from "~/state/server"; import { ServerUpdateAction, ServerUpdateProgress, ServerUpdatesAction, type ServerUpdateTarget, } from "../ServerUpdateAction"; import { sortDesktopPairingLinks, summarizeAuthorizedClients, sortDesktopClientSessions, toDesktopPairingLinkRecord, toDesktopClientSessionRecord, selectPairingEndpoint, isServerNetworkAccessible, isTailscaleHttpsEndpoint, endpointDefaultPreferenceKey, } from "./ConnectionsSettings.logic"; import { AdvertisedEndpointListRow, AuthorizedClientsHeaderAction, NetworkAccessDescription, PairingClientsList, } from "./AuthorizedClientsSettings"; import { ITEM_ROW_CLASSNAME } from "./itemRows"; import { CloudLinkRow, CloudRemoteEnvironmentRows, SavedBackendListRow, } from "./SavedEnvironmentRows"; import { AddEnvironmentDialog } from "./AddEnvironmentDialog"; import { type ConnectDeviceRevokeRequest, RevokeConnectDeviceDialog, } from "./RevokeConnectDeviceDialog"; // Sessions T3 Connect minted for a device signed in to the linked account. const CLOUD_CONNECT_SESSION_SUBJECT = "cloud-connect"; const DEFAULT_TAILSCALE_SERVE_PORT = 443; const EMPTY_ADVERTISED_ENDPOINTS: ReadonlyArray = []; // Colons are invalid in distro names, keeping picker sentinels collision-free. const BACKEND_VALUE_DEFAULT_WSL = "backend:default-wsl"; const BACKEND_VALUE_WSL_OFF = "backend:wsl-off"; // Subscribes on its own so desktop download progress does not re-render the page. function LocalVersionStatusLabel() { const m = useMessages(); const updateState = useDesktopUpdateState(); return ( {resolveLocalVersionStatusLabel(updateState, m)} ); } /** Environments lists the machines this app connects to; access covers who may reach one of them. */ export function ConnectionsSettings({ view }: { readonly view: "environments" | "access" }) { return view === "access" ? ( ) : ( ); } function AccessSettings() { const gt = useGT(); // A link that names an environment, such as the MCP wizard's, opens the page on it. The // settings layout remounts this page when the link changes. const linkedEnvironmentId = useSearch({ strict: false, select: (search) => typeof search.machine === "string" ? EnvironmentId.make(search.machine) : null, }); const [selectedEnvironmentId, setSelectedEnvironmentId] = useState(linkedEnvironmentId); const { candidates, environment: accessEnvironment } = useAccessEnvironment(selectedEnvironmentId); const accessEnvironmentId = accessEnvironment?.environmentId ?? null; const picker = candidates.length > 1 && accessEnvironmentId !== null ? (
Showing access for
) : null; // Keyed so pairing credentials and pending work never carry over to another environment. return ( ); } function ConnectionsSettingsContent({ view, accessEnvironmentId, accessEnvironmentPicker = null, }: { readonly view: "environments" | "access"; readonly accessEnvironmentId: EnvironmentId | null; readonly accessEnvironmentPicker?: ReactNode; }) { const searchableSetting = useSearchableSetting(); const gt = useGT(); const m = useMessages(); const desktopBridge = window.desktopBridge; const { environments } = useEnvironments(); const primaryEnvironment = usePrimaryEnvironment(); const peerLinkHosts = usePeerLinkHosts(); const removeEnvironment = useAtomCommand(environmentCatalog.remove, { reportFailure: false }); const setEnvironmentEnabled = useAtomCommand(environmentCatalog.setEnabled, { reportFailure: false, }); const primaryEnvironmentId = primaryEnvironment?.environmentId ?? null; const accessEnvironment = useEnvironment(accessEnvironmentId); // Desktop network, Tailscale, and WSL controls change the backend this app spawned. const isPrimaryAccess = accessEnvironmentId !== null && accessEnvironmentId === primaryEnvironmentId; const accessSessionState = useEnvironmentSessionState(accessEnvironmentId); const currentSessionScopes = accessSessionState.data?.authenticated ? (accessSessionState.data.permissions ?? accessSessionState.data.scopes ?? null) : null; const currentAuthPolicy = desktopBridge ? null : (accessSessionState.data?.auth.policy ?? null); const savedEnvironments = environments.filter( (environment) => environment.entry.target._tag !== "PrimaryConnectionTarget", ); const listedEnvironments = savedEnvironments.filter( (environment) => !isDesktopLocalConnectionTarget(environment.entry.target), ); const savedUpdateStates = useAtomValue( environmentUpdateStatesAtom( environmentIdsKey(savedEnvironments.map(({ environmentId }) => environmentId)), ), ); const savedServerUpdateStates = savedEnvironments.map((environment, index) => ({ environment, updateStatus: savedUpdateStates[index]!.status, })); const savedServerUpdateTargets = savedServerUpdateStates.flatMap( ({ environment, updateStatus }): ServerUpdateTarget[] => { const mismatch = resolveServerConfigVersionMismatch(environment.serverConfig, m); const selfUpdate = resolveServerSelfUpdateCapability(environment.serverConfig); const desktopAppUpdate = supportsDesktopAppUpdate(environment.serverConfig); if ( !mismatch || updateStatus === "running" || !environment.entry.enabled || environment.connection.phase !== "connected" || isDesktopLocalConnectionTarget(environment.entry.target) || selfUpdate === null || (selfUpdate === "desktop-managed" && !desktopAppUpdate) ) { return []; } return [ { environmentId: environment.environmentId, serverLabel: environment.label, selfUpdate, installation: environment.serverConfig?.environment.capabilities.serverInstallation, desktopAppUpdate, targetVersion: mismatch.clientVersion, }, ]; }, ); const loadBalancingEnvironments = [ ...(primaryEnvironment ? [primaryEnvironment] : []), ...savedEnvironments.filter((environment) => environment.entry.enabled), ].filter((environment) => environment.connection.phase !== "unsupported"); const [desktopServerExposureMutationError, setDesktopServerExposureMutationError] = useState< string | null >(null); const [desktopAccessManagementMutationError, setDesktopAccessManagementMutationError] = useState< string | null >(null); const [createdPairingCredentials, setCreatedPairingCredentials] = useState< ReadonlyMap >(() => new Map()); const [newestPairingLinkId, setNewestPairingLinkId] = useState(null); const handlePairingLinkCreated = (created: AuthPairingCredentialResult) => { setCreatedPairingCredentials((current) => new Map(current).set(created.id, created.credential)); setNewestPairingLinkId(created.id); }; const [revokingDesktopPairingLinkId, setRevokingDesktopPairingLinkId] = useState( null, ); const [revokingDesktopClientSessionId, setRevokingDesktopClientSessionId] = useState< string | null >(null); const [isRevokingOtherDesktopClients, setIsRevokingOtherDesktopClients] = useState(false); const [addBackendDialogOpen, setAddBackendDialogOpen] = useState(false); const [routeTarget, setRouteTarget] = useState(null); const [savedBackendError, setSavedBackendError] = useState(null); const handleAddBackendDialogOpenChange = (open: boolean) => { setAddBackendDialogOpen(open); if (open) { setRouteTarget(null); } else { setSavedBackendError(null); } }; const [removingSavedEnvironmentId, setRemovingSavedEnvironmentId] = useState(null); const [isUpdatingDesktopServerExposure, setIsUpdatingDesktopServerExposure] = useState(false); const [isUpdatingTailscaleServe, setIsUpdatingTailscaleServe] = useState(false); const [isUpdatingWslBackend, setIsUpdatingWslBackend] = useState(false); const [desktopWslMutationError, setDesktopWslMutationError] = useState(null); type PendingWslChange = | { readonly kind: "disable"; readonly wasWslOnly: boolean } | { readonly kind: "distro"; readonly nextDistro: string | null } | { readonly kind: "enable"; readonly nextDistro: string | null } | { readonly kind: "wsl-only"; readonly nextValue: boolean }; const [pendingWslChange, setPendingWslChange] = useState(null); const isWslConfirmDialogOpen = pendingWslChange !== null; const [pendingTailscaleServeEndpoint, setPendingTailscaleServeEndpoint] = useState(null); const [disableTailscaleServeDialogOpen, setDisableTailscaleServeDialogOpen] = useState(false); const [tailscaleServePortInput, setTailscaleServePortInput] = useState( String(DEFAULT_TAILSCALE_SERVE_PORT), ); const [pendingDesktopServerExposureMode, setPendingDesktopServerExposureMode] = useState< DesktopServerExposureState["mode"] | null >(null); const primaryServerConfig = primaryEnvironment?.serverConfig ?? null; // Turning remote access off stays possible; only the way in is managed. const remoteClientsDenial = managedCapabilityDenial( primaryServerConfig?.managedPolicy, "remoteClients", m, ); const primaryVersionMismatch = resolveServerConfigVersionMismatch(primaryServerConfig, m); const primaryServerUpdateState = useAtomValue( serverEnvironment.updateStateAtom(primaryEnvironmentId), ); const [isAdvertisedEndpointListExpanded, setIsAdvertisedEndpointListExpanded] = useState(false); const defaultAdvertisedEndpointKey = useUiStateStore( (state) => state.defaultAdvertisedEndpointKey, ); const setDefaultAdvertisedEndpointKey = useUiStateStore( (state) => state.setDefaultAdvertisedEndpointKey, ); const canReadAccess = useEnvironmentScope(accessEnvironmentId, AuthAccessReadScope); const canWriteAccess = useEnvironmentScope(accessEnvironmentId, AuthAccessWriteScope); const canReadRelay = useEnvironmentScope(accessEnvironmentId, AuthRelayReadScope); const canMaintain = useEnvironmentScope(accessEnvironmentId, AuthEnvironmentMaintainScope); const canManageRelay = useEnvironmentScope(accessEnvironmentId, AuthRelayWriteScope); const canManageLocalBackend = isPrimaryAccess && !isLocalEnvironmentDisabled() && canMaintain; const showsAccess = view === "access"; const authAccessChanges = useEnvironmentQuery( showsAccess && canReadAccess && accessEnvironmentId !== null ? authEnvironment.accessChanges({ environmentId: accessEnvironmentId, input: null, }) : null, m, ); const revokePairingLink = useAtomCommand(authEnvironment.revokePairingLink, { reportFailure: false, }); const revokeClient = useAtomCommand(authEnvironment.revokeClient, { reportFailure: false }); const revokeOtherClients = useAtomCommand(authEnvironment.revokeOtherClients, { reportFailure: false, }); const desktopNetworkAccess = useEnvironmentQuery( showsAccess && canManageLocalBackend && desktopBridge ? desktopNetworkAccessStateAtom : null, m, ); const desktopWsl = useEnvironmentQuery( showsAccess && canManageLocalBackend && desktopBridge ? desktopWslAccessStateAtom : null, m, ); const desktopWslState = desktopWsl.data; const desktopWslError = desktopWslMutationError ?? desktopWsl.error; const isLoadingWslState = desktopWsl.isPending && desktopWsl.data === null; const desktopServerExposureState = desktopNetworkAccess.data?.serverExposureState ?? null; const desktopAdvertisedEndpoints = desktopNetworkAccess.data?.advertisedEndpoints ?? EMPTY_ADVERTISED_ENDPOINTS; const desktopServerExposureError = desktopServerExposureMutationError ?? desktopNetworkAccess.error; const desktopAccessManagementError = desktopAccessManagementMutationError ?? authAccessChanges.error; const isLoadingDesktopAccessManagement = authAccessChanges.isPending && authAccessChanges.data === null; const desktopPairingLinks = (() => { const event = authAccessChanges.data; if (event?.type !== "snapshot") return []; return sortDesktopPairingLinks( event.payload.pairingLinks.map((pairingLink: AuthPairingLink) => toDesktopPairingLinkRecord(pairingLink), ), ); })(); const desktopClientSessions = (() => { const event = authAccessChanges.data; if (event?.type !== "snapshot") return []; return sortDesktopClientSessions( event.payload.clientSessions.map((clientSession: AuthClientSession) => toDesktopClientSessionRecord(clientSession), ), ); })(); const isLocalBackendNetworkAccessible = desktopBridge ? desktopServerExposureState?.mode === "network-accessible" : isServerNetworkAccessible({ authPolicy: currentAuthPolicy, directEndpoints: accessEnvironment?.serverConfig?.directEndpoints, }); const trimmedTailscaleServePortInput = tailscaleServePortInput.trim(); const parsedTailscaleServePort = Number(trimmedTailscaleServePortInput); const isTailscaleServePortValid = /^\d+$/u.test(trimmedTailscaleServePortInput) && Number.isInteger(parsedTailscaleServePort) && parsedTailscaleServePort >= 1 && parsedTailscaleServePort <= 65_535; const pendingTailscaleServeBaseUrl = resolveTailscaleServeBaseUrl( pendingTailscaleServeEndpoint, parsedTailscaleServePort, isTailscaleServePortValid, ); const handleDesktopServerExposureChange = async (checked: boolean) => { if ( !desktopBridge || primaryEnvironmentId === null || !readEnvironmentScope(primaryEnvironmentId, AuthEnvironmentMaintainScope) ) return; setIsUpdatingDesktopServerExposure(true); setDesktopServerExposureMutationError(null); try { await desktopBridge.setServerExposureMode(checked ? "network-accessible" : "local-only"); refreshDesktopNetworkAccessState(); setPendingDesktopServerExposureMode(null); setIsUpdatingDesktopServerExposure(false); } catch (error) { const message = error instanceof Error ? (thrownErrorMessage(error, resolveMessage) ?? error.message) : gt("Failed to update network exposure."); setPendingDesktopServerExposureMode(null); setDesktopServerExposureMutationError(message); toastManager.add( stackedThreadToast({ type: "error", title: gt("Could not update network access"), description: message, }), ); setIsUpdatingDesktopServerExposure(false); } }; const handleConfirmDesktopServerExposureChange = () => { if (pendingDesktopServerExposureMode === null) return; const checked = pendingDesktopServerExposureMode === "network-accessible"; void handleDesktopServerExposureChange(checked); }; const handleConfirmTailscaleServeSetup = async () => { if ( !desktopBridge || primaryEnvironmentId === null || !readEnvironmentScope(primaryEnvironmentId, AuthEnvironmentMaintainScope) ) return; if (!isTailscaleServePortValid) return; setIsUpdatingTailscaleServe(true); setDesktopServerExposureMutationError(null); const settled = await settlePromise(async () => { await desktopBridge.setTailscaleServeEnabled({ enabled: true, port: parsedTailscaleServePort, }); refreshDesktopNetworkAccessState(); setPendingTailscaleServeEndpoint(null); }); if (settled._tag === "Failure") { const error = Cause.squash(settled.cause); const message = error instanceof Error ? (thrownErrorMessage(error, resolveMessage) ?? error.message) : gt("Failed to configure Tailscale HTTPS."); setDesktopServerExposureMutationError(message); toastManager.add( stackedThreadToast({ type: "error", title: gt("Could not set up Tailscale HTTPS"), description: message, }), ); } setIsUpdatingTailscaleServe(false); }; const handleStartTailscaleServeSetup = (endpoint: AdvertisedEndpoint) => { setTailscaleServePortInput( String(desktopServerExposureState?.tailscaleServePort ?? DEFAULT_TAILSCALE_SERVE_PORT), ); setPendingTailscaleServeEndpoint(endpoint); }; const handleConfirmTailscaleServeDisable = async () => { if ( !desktopBridge || primaryEnvironmentId === null || !readEnvironmentScope(primaryEnvironmentId, AuthEnvironmentMaintainScope) ) return; setIsUpdatingTailscaleServe(true); setDesktopServerExposureMutationError(null); const settled = await settlePromise(async () => { await desktopBridge.setTailscaleServeEnabled({ enabled: false, port: desktopServerExposureState?.tailscaleServePort ?? DEFAULT_TAILSCALE_SERVE_PORT, }); refreshDesktopNetworkAccessState(); setDisableTailscaleServeDialogOpen(false); }); if (settled._tag === "Failure") { const error = Cause.squash(settled.cause); const message = error instanceof Error ? (thrownErrorMessage(error, resolveMessage) ?? error.message) : gt("Failed to disable Tailscale HTTPS."); setDesktopServerExposureMutationError(message); toastManager.add( stackedThreadToast({ type: "error", title: gt("Could not disable Tailscale HTTPS"), description: message, }), ); } setIsUpdatingTailscaleServe(false); }; const handleStartTailscaleServeDisable = (_endpoint: AdvertisedEndpoint) => { setDisableTailscaleServeDialogOpen(true); }; const handleRevokeDesktopPairingLink = async (id: string) => { if ( accessEnvironmentId === null || !readEnvironmentScope(accessEnvironmentId, AuthAccessWriteScope) ) return; setRevokingDesktopPairingLinkId(id); setDesktopAccessManagementMutationError(null); const result = await revokePairingLink({ environmentId: accessEnvironmentId, input: { id } }); if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { const error = squashAtomCommandFailure(result); const message = error instanceof Error ? (thrownErrorMessage(error, resolveMessage) ?? error.message) : gt("Failed to revoke pairing link."); setDesktopAccessManagementMutationError(message); toastManager.add( stackedThreadToast({ type: "error", title: gt("Could not revoke pairing link"), description: message, }), ); } setRevokingDesktopPairingLinkId(null); }; const handleRevokeDesktopClientSession = async ( sessionId: ServerClientSessionRecord["sessionId"], ) => { if ( accessEnvironmentId === null || !readEnvironmentScope(accessEnvironmentId, AuthAccessWriteScope) ) return; setRevokingDesktopClientSessionId(sessionId); setDesktopAccessManagementMutationError(null); const result = await revokeClient({ environmentId: accessEnvironmentId, input: { sessionId } }); if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { const error = squashAtomCommandFailure(result); const message = error instanceof Error ? (thrownErrorMessage(error, resolveMessage) ?? error.message) : gt("Failed to revoke client access."); setDesktopAccessManagementMutationError(message); toastManager.add( stackedThreadToast({ type: "error", title: gt("Could not revoke client access"), description: message, }), ); } setRevokingDesktopClientSessionId(null); }; const handleRevokeOtherDesktopClients = async () => { if ( accessEnvironmentId === null || !readEnvironmentScope(accessEnvironmentId, AuthAccessWriteScope) ) return; setIsRevokingOtherDesktopClients(true); setDesktopAccessManagementMutationError(null); const result = await revokeOtherClients({ environmentId: accessEnvironmentId, input: null }); if (result._tag === "Success") { toastManager.add({ type: "success", title: result.value === 1 ? gt("Revoked 1 other client") : gt("{count, plural, other {Revoked {count} clients}}", { count: result.value }), description: gt("Other paired clients will need a new pairing link before reconnecting."), }); } else if (!isAtomCommandInterrupted(result)) { const error = squashAtomCommandFailure(result); const message = error instanceof Error ? (thrownErrorMessage(error, resolveMessage) ?? error.message) : gt("Failed to revoke other clients."); setDesktopAccessManagementMutationError(message); toastManager.add( stackedThreadToast({ type: "error", title: gt("Could not revoke other clients"), description: message, }), ); } setIsRevokingOtherDesktopClients(false); }; const handleSetSavedBackendEnabled = async (environmentId: EnvironmentId, enabled: boolean) => { setSavedBackendError(null); const result = await setEnvironmentEnabled({ environmentId, enabled }); if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { const error = squashAtomCommandFailure(result); const message = error instanceof Error ? (errorDisplayMessage(error, resolveMessage) ?? error.message) : enabled ? gt("Failed to switch the backend on.") : gt("Failed to switch the backend off."); setSavedBackendError(message); toastManager.add( stackedThreadToast({ type: "error", title: enabled ? gt("Could not switch backend on") : gt("Could not switch backend off"), description: message, }), ); } }; const removeSavedBackend = async (environment: EnvironmentPresentation) => { const environmentId = environment.environmentId; setRemovingSavedEnvironmentId(environmentId); setSavedBackendError(null); const result = await removeEnvironment(environmentId); setRemovingSavedEnvironmentId(null); if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { const error = squashAtomCommandFailure(result); const message = error instanceof Error ? (thrownErrorMessage(error, resolveMessage) ?? error.message) : gt("Failed to remove backend."); setSavedBackendError(message); toastManager.add( stackedThreadToast({ type: "error", title: gt("Could not remove backend"), description: message, }), ); } }; // Removing a T3 Connect route preserves account registration; use a separate dialog pointing to // deregistration. const [pendingT3ConnectRemoval, setPendingT3ConnectRemoval] = useState(null); const handleRemoveSavedBackend = async (environment: EnvironmentPresentation) => { if (environment.relayManaged && hasCloudPublicConfig()) { setPendingT3ConnectRemoval(environment); return; } const confirmed = await requestConfirmDialog( gt( "Remove {environment} from this device?\nThis forgets its pairing, credentials, and cached threads here. Switch it off instead to keep it saved.", { environment: environment.label }, ), { variant: "destructive" }, ); if (confirmed === true) await removeSavedBackend(environment); }; // T3 Connect devices waiting for approval have their own list. const visibleDesktopPairingLinks = desktopPairingLinks.filter( (pairingLink) => pairingLink.connectDeviceRequest === undefined, ); const connectDeviceRequests = desktopPairingLinks.filter( (pairingLink) => pairingLink.connectDeviceRequest !== undefined, ); // Older servers omit the list; they cannot hold devices for approval. const connectDevices = authAccessChanges.data?.type === "snapshot" ? (authAccessChanges.data.payload.connectDevices ?? null) : null; const updateAccessSettings = useUpdateEnvironmentSettings(accessEnvironmentId); const [pendingConnectRevoke, setPendingConnectRevoke] = useState(null); // With approval off a revoked T3 Connect device signs straight back in, so offer to turn it // on. Older servers have no such setting, and revoking there goes ahead as before. const connectApprovalOff = accessEnvironment?.serverConfig?.settings.requireConnectDeviceApproval === false; const requestConnectRevoke = (title: string, revoke: () => Promise) => { if (connectApprovalOff && canWriteAccess) setPendingConnectRevoke({ title, revoke }); else void revoke(); }; const requireConnectApproval = () => new Promise((resolve) => updateAccessSettings( { requireConnectDeviceApproval: true }, { onSaved: () => resolve(true), onFailed: () => resolve(false) }, ), ); const requestRevokeClientSession = (sessionId: ServerClientSessionRecord["sessionId"]) => { const session = desktopClientSessions.find((candidate) => candidate.sessionId === sessionId); if (session?.subject === CLOUD_CONNECT_SESSION_SUBJECT) { requestConnectRevoke(gt("Revoke {name}?", { name: connectDeviceName(session, m) }), () => handleRevokeDesktopClientSession(sessionId), ); } else { void handleRevokeDesktopClientSession(sessionId); } }; const requestRevokeOtherClients = () => { const revokesConnectDevices = (connectDevices?.length ?? 0) > 0 || desktopClientSessions.some( (session) => !session.current && session.subject === CLOUD_CONNECT_SESSION_SUBJECT, ); if (revokesConnectDevices) { requestConnectRevoke(gt("Revoke all other clients?"), handleRevokeOtherDesktopClients); } else { void handleRevokeOtherDesktopClients(); } }; const tailscaleHttpsEndpoint = desktopAdvertisedEndpoints.find(isTailscaleHttpsEndpoint) ?? null; const visibleDesktopNetworkAdvertisedEndpoints = isLocalBackendNetworkAccessible ? desktopAdvertisedEndpoints.filter((endpoint) => !isTailscaleHttpsEndpoint(endpoint)) : []; const visibleDesktopAdvertisedEndpoints = tailscaleHttpsEndpoint ? [...visibleDesktopNetworkAdvertisedEndpoints, tailscaleHttpsEndpoint] : visibleDesktopNetworkAdvertisedEndpoints; const defaultDesktopNetworkAdvertisedEndpoint = selectPairingEndpoint( visibleDesktopNetworkAdvertisedEndpoints, defaultAdvertisedEndpointKey, ); const defaultDesktopAdvertisedEndpoint = defaultDesktopNetworkAdvertisedEndpoint ?? selectPairingEndpoint( tailscaleHttpsEndpoint ? [tailscaleHttpsEndpoint] : [], defaultAdvertisedEndpointKey, ); const defaultDesktopAdvertisedEndpointKey = defaultDesktopAdvertisedEndpoint ? endpointDefaultPreferenceKey(defaultDesktopAdvertisedEndpoint) : null; const handleSetDefaultAdvertisedEndpoint = (endpoint: AdvertisedEndpoint) => { setDefaultAdvertisedEndpointKey(endpointDefaultPreferenceKey(endpoint)); }; const renderNetworkAccessToggle = () => ( { setPendingDesktopServerExposureMode(checked ? "network-accessible" : "local-only"); }} aria-label={gt("Enable network access")} /> ); const renderEndpointRows = () => isAdvertisedEndpointListExpanded ? visibleDesktopNetworkAdvertisedEndpoints.map((endpoint) => { const endpointKey = endpointDefaultPreferenceKey(endpoint); return ( ); }) : null; const applyWslSettingChange = async (apply: () => Promise) => { if ( !desktopBridge || primaryEnvironmentId === null || !readEnvironmentScope(primaryEnvironmentId, AuthEnvironmentMaintainScope) ) return; setIsUpdatingWslBackend(true); setDesktopWslMutationError(null); const settled = await settlePromise(async () => { await apply(); refreshDesktopWslState(); }); if (settled._tag === "Failure") { const error = Cause.squash(settled.cause); const message = error instanceof Error ? (thrownErrorMessage(error, resolveMessage) ?? error.message) : gt("Failed to update WSL backend."); setDesktopWslMutationError(message); toastManager.add( stackedThreadToast({ type: "error", title: gt("Could not change WSL backend"), description: message, }), ); refreshDesktopWslState(); } setIsUpdatingWslBackend(false); }; const loadWslState = () => { setDesktopWslMutationError(null); refreshDesktopWslState(); }; const hasWslRegistrationToLose = environments.some((environment) => isDesktopLocalConnectionTarget(environment.entry.target), ); const handleSelectWslMode = (value: string) => { if (!desktopBridge || !desktopWslState) return; const defaultDistroName = desktopWslState.distros.find((distro) => distro.isDefault)?.name ?? null; if (value === BACKEND_VALUE_WSL_OFF) { // Unavailable WSL can remain wslOnly while disabled; switching to Windows must clear both. if (!desktopWslState.enabled && !desktopWslState.wslOnly) return; const wasWslOnly = desktopWslState.wslOnly; if (hasWslRegistrationToLose || wasWslOnly) { setPendingWslChange({ kind: "disable", wasWslOnly }); return; } void applyWslSettingChange(() => desktopBridge.setWslBackendEnabled(false)); return; } const nextDistro = value === BACKEND_VALUE_DEFAULT_WSL ? null : value; const resolvedNext = nextDistro ?? defaultDistroName; if (!desktopWslState.enabled) { setPendingWslChange({ kind: "enable", nextDistro }); return; } const resolvedCurrent = desktopWslState.distro ?? defaultDistroName; if (resolvedCurrent === resolvedNext) return; if (hasWslRegistrationToLose || desktopWslState.wslOnly) { setPendingWslChange({ kind: "distro", nextDistro }); return; } void applyWslSettingChange(() => desktopBridge.setWslDistro(nextDistro)); }; const handleConfirmEnableWsl = (mode: "both" | "wsl-only") => { if (!desktopBridge || !pendingWslChange || pendingWslChange.kind !== "enable") return; const nextDistro = pendingWslChange.nextDistro; setPendingWslChange(null); const persistedDistro = desktopWslState?.distro ?? null; void applyWslSettingChange(() => applyWslEnableSelection({ bridge: desktopBridge, mode, nextDistro, persistedDistro, }), ); }; const handleToggleWslOnly = (enabled: boolean) => { if (!desktopBridge || !desktopWslState || desktopWslState.wslOnly === enabled) return; setPendingWslChange({ kind: "wsl-only", nextValue: enabled }); }; const handleConfirmWslChange = () => { if (!desktopBridge || !pendingWslChange) return; const change = pendingWslChange; if (change.kind === "enable") return; setPendingWslChange(null); if (change.kind === "disable") { void applyWslSettingChange(async () => { const next = await desktopBridge.setWslBackendEnabled(false); if (change.wasWslOnly) { return await desktopBridge.setWslOnly(false); } return next; }); return; } if (change.kind === "distro") { void applyWslSettingChange(() => desktopBridge.setWslDistro(change.nextDistro)); return; } void applyWslSettingChange(() => desktopBridge.setWslOnly(change.nextValue)); }; const renderWslRow = () => { if (!desktopWslState) { if ( isWslSettingsRowVisible({ state: null, error: desktopWslError }) && canManageLocalBackend ) { return ( {desktopWslError}} control={ } /> ); } return null; } if (!isWslSettingsRowVisible({ state: desktopWslState, error: desktopWslError })) { return null; } if (!desktopWslState.available) { return ( {desktopWslError} ) : null } control={ } /> ); } const defaultDistroName = desktopWslState.distros.find((distro) => distro.isDefault)?.name ?? null; const selectValue = !desktopWslState.enabled ? BACKEND_VALUE_WSL_OFF : (desktopWslState.distro ?? defaultDistroName ?? BACKEND_VALUE_DEFAULT_WSL); const selectLabel = selectValue === BACKEND_VALUE_WSL_OFF ? gt("Off", { $context: "WSL backend is off" }) : selectValue === BACKEND_VALUE_DEFAULT_WSL ? gt("Default distro") : selectValue; return ( <> {desktopWslError} ) : desktopWslState.preflightError ? ( WSL backend couldn't start: {desktopWslState.preflightError} ) : null } control={ } /> {desktopWslState.enabled ? ( handleToggleWslOnly(checked)} aria-label={gt("Run WSL only")} /> } /> ) : null} ); }; const renderTailscaleRow = () => ( { if (checked) { handleStartTailscaleServeSetup(tailscaleHttpsEndpoint); return; } handleStartTailscaleServeDisable(tailscaleHttpsEndpoint); }} aria-label={gt("Enable Tailscale HTTPS")} /> ) : null } managed={tailscaleHttpsEndpoint?.status === "available" ? undefined : remoteClientsDenial} /> ); const newestPairingLink = visibleDesktopPairingLinks.find( (pairingLink) => pairingLink.id === newestPairingLinkId, ); const renderPairingClientsList = ( pairingLinks: typeof visibleDesktopPairingLinks, clientSessions: typeof desktopClientSessions, ) => ( ); const renderAuthorizedClients = () => ( <> {desktopAccessManagementError ? (

{desktopAccessManagementError}

) : null} {renderPairingClientsList(visibleDesktopPairingLinks, desktopClientSessions)} ); const renderNetworkAccessRow = () => ( setIsAdvertisedEndpointListExpanded((expanded) => !expanded)} fallback={ desktopServerExposureState?.endpointUrl ? gt("Reachable at {url}", { url: desktopServerExposureState.endpointUrl }) : desktopServerExposureState?.advertisedHost ? gt("Exposed on all interfaces. Pairing links use {host}.", { host: desktopServerExposureState.advertisedHost, }) : gt("Exposed on all interfaces.") } /> ) : desktopServerExposureState ? ( gt("Limited to this machine.") ) : ( gt("Loading…") ) } status={ desktopServerExposureError ? ( {desktopServerExposureError} ) : null } control={renderNetworkAccessToggle()} managed={isLocalBackendNetworkAccessible ? undefined : remoteClientsDenial} /> ); const renderDisabledNetworkAccessRow = () => ( } /> Network exposure changes restart the backend and must be controlled where the server process is launched. ) : undefined } /> ); const accessServerConfig = accessEnvironment?.serverConfig ?? null; const primarySettings = ( <> {accessEnvironmentPicker} {accessEnvironmentId !== null ? ( <> } headerAction={ accessEnvironmentId !== null ? ( } > ) : null } > {accessServerConfig?.managedPolicy ? (
) : null} {accessEnvironment?.connection.phase === "connected" ? ( ) : null} {canManageLocalBackend ? ( ) : ( [ primaryServerConfig?.environment.serverVersion ?? null, primaryEnvironment?.displayUrl ?? null, ] .filter((value): value is string => value !== null) .join(" · ") || gt("Loading…") ) } control={ primaryVersionMismatch && primaryEnvironmentId !== null && primaryServerUpdateState.status !== "running" ? ( ) : primaryServerUpdateState.status === "idle" && primaryServerConfig ? ( ) : undefined } /> ) : null} {canManageLocalBackend && desktopBridge ? ( <> {renderNetworkAccessRow()} {renderEndpointRows()} {renderTailscaleRow()} {renderWslRow()} ) : canManageLocalBackend ? ( renderDisabledNetworkAccessRow() ) : null} {canReadRelay || canManageRelay ? ( ) : null} {accessEnvironment ? (
Permissions
) : null}
{canReadAccess || canWriteAccess ? ( ) : undefined } > {canReadAccess ? ( renderAuthorizedClients() ) : (

This connection can create access links but cannot view authorized clients.

)}
) : null} {canReadAccess && connectDevices !== null && (connectDeviceRequests.length > 0 || connectDevices.length > 0) ? ( ) : null} setPendingConnectRevoke(null)} requireApproval={requireConnectApproval} /> { if (!open && !isUpdatingDesktopServerExposure) setPendingDesktopServerExposureMode(null); }} > {pendingDesktopServerExposureMode === "network-accessible" ? gt("Enable network access?") : gt("Disable network access?")} {pendingDesktopServerExposureMode === "network-accessible" ? gt( "Let your other devices connect to T3 Code over the network. Pair devices to give them access. T3 Code will restart.", ) : gt( "Devices connected over your local network will disconnect. Existing tunnels, such as T3 Connect or Tailscale HTTPS, keep working. T3 Code will restart.", )} } > Cancel { if (isUpdatingWslBackend) return; if (!open) setPendingWslChange(null); }} > {pendingWslChange?.kind === "disable" ? pendingWslChange.wasWslOnly ? gt("Turn off WSL and switch back to Windows?") : gt("Disable WSL backend?") : pendingWslChange?.kind === "distro" ? gt("Switch WSL distro?") : pendingWslChange?.kind === "enable" ? gt("Start the WSL backend") : pendingWslChange?.nextValue ? gt("Run only the WSL backend?") : gt("Re-enable the Windows backend?")} {pendingWslChange?.kind === "disable" ? pendingWslChange.wasWslOnly ? gt( "T3 Code will restart on the Windows backend. Threads and projects opened against WSL stay safe inside the distro and become available again when you re-enable WSL.", ) : gt( "The WSL backend will stop. Threads and projects opened against WSL stay safe inside the distro, but they'll be unavailable in T3 Code until you re-enable WSL.", ) : pendingWslChange?.kind === "distro" ? gt( "T3 Code will restart the WSL backend on the new distro. Sessions still running on the current distro will be interrupted.", ) : pendingWslChange?.kind === "enable" ? gt( "Run the WSL backend alongside the Windows one, or stop the Windows backend and use only WSL? You can change this later from Settings.", ) : pendingWslChange?.nextValue ? gt( "T3 Code will restart and start only the WSL backend. Your Windows-side projects won't be accessible until you turn this off again.", ) : gt( "T3 Code will restart and bring the Windows backend back up alongside WSL.", )} } > Cancel {pendingWslChange?.kind === "enable" ? ( <> ) : ( )} { if (isUpdatingTailscaleServe) return; setDisableTailscaleServeDialogOpen(open); }} > Disable Tailscale HTTPS? T3 Code will restart the local backend without Tailscale Serve. } > Cancel { if (isUpdatingTailscaleServe) return; if (!open) setPendingTailscaleServeEndpoint(null); }} > Set up Tailscale HTTPS? T3 Code will restart the local backend with Tailscale Serve enabled and ask Tailscale to proxy HTTPS traffic to this backend. {!isTailscaleServePortValid ? (

Enter a port from 1 to 65535.

) : null}

HTTPS endpoint

{pendingTailscaleServeBaseUrl ?? gt("Pending MagicDNS endpoint")}

} /> {pendingTailscaleServeBaseUrl ? ( {pendingTailscaleServeBaseUrl} ) : null}
} > Cancel
) : ( )} ); if (showsAccess) return {primarySettings}; return ( {desktopBridge ? ( ) : null} {savedServerUpdateTargets.length > 0 ? ( ) : null} } > {listedEnvironments.map((environment) => ( { setRouteTarget(target); setSavedBackendError(null); setAddBackendDialogOpen(true); }} /> ))} {hasCloudPublicConfig() ? ( setPendingT3ConnectRemoval(null)} onConfirm={() => { if (!pendingT3ConnectRemoval) return; setPendingT3ConnectRemoval(null); void removeSavedBackend(pendingT3ConnectRemoval); }} /> ) : null} {peerLinkHosts.map((environment, index) => ( 1 ? environment.label : null} isSearchTarget={index === 0} /> ))} ); }