diff --git a/apps/mobile/src/state/peerLinks.ts b/apps/mobile/src/state/peerLinks.ts index baed9be90412..f44558c66b12 100644 --- a/apps/mobile/src/state/peerLinks.ts +++ b/apps/mobile/src/state/peerLinks.ts @@ -14,6 +14,7 @@ import { relayEnvironmentDiscovery } from "./relay"; import { useEnvironmentQuery } from "./query"; import { useAtomCommand } from "./use-atom-command"; +// Web adds linked machines on its own (`linkedEnvironmentsToAutoAdd`); mobile does not yet. export const peerLinkEnvironment = createPeerLinkEnvironmentAtoms(connectionAtomRuntime); /** diff --git a/apps/web/src/components/cloud/CloudEnvironmentConnectList.tsx b/apps/web/src/components/cloud/CloudEnvironmentConnectList.tsx index 8c3df5338074..9a56caa3a401 100644 --- a/apps/web/src/components/cloud/CloudEnvironmentConnectList.tsx +++ b/apps/web/src/components/cloud/CloudEnvironmentConnectList.tsx @@ -39,6 +39,7 @@ import { Skeleton } from "../ui/skeleton"; import { toastManager } from "../ui/toast"; import { Tooltip, TooltipTrigger, TooltipPopup } from "../ui/tooltip"; import { presentSavedCloudEnvironmentConnection } from "./cloudEnvironmentConnectionPresentation"; +import { undismissLinkedEnvironment } from "../../state/linkedEnvironmentDismissals"; const EMPTY_DISCOVERY_REFRESH_INTERVAL_MS = 5_000; @@ -111,7 +112,11 @@ export function CloudEnvironmentConnectRows({ label: environment.label, }), }), - ); + ).then((result) => { + // Adding it back is the way back from removing it, so it may be added on its own again. + if (result._tag === "Success") void undismissLinkedEnvironment(environment.environmentId); + return result; + }); const [connectingEnvironmentIds, setConnectingEnvironmentIds] = useState< ReadonlySet >(new Set()); diff --git a/apps/web/src/components/settings/AddEnvironmentDialog.tsx b/apps/web/src/components/settings/AddEnvironmentDialog.tsx index 68f1da1a7c10..5dd78355aaea 100644 --- a/apps/web/src/components/settings/AddEnvironmentDialog.tsx +++ b/apps/web/src/components/settings/AddEnvironmentDialog.tsx @@ -58,6 +58,7 @@ import { desktopSshHostsStateAtom, filterDiscoveredSshHosts } from "~/state/desk import { type EnvironmentPresentation, useRelayEnvironmentDiscovery } from "~/state/environments"; import { useAtomCommand } from "../../state/use-atom-command"; import { primaryServerKeybindingsAtom } from "~/state/server"; +import { undismissLinkedEnvironment } from "~/state/linkedEnvironmentDismissals"; import { resolveShortcutCommand, shortcutLabelForCommand, @@ -474,6 +475,7 @@ export function AddEnvironmentDialog({ } return; } + void undismissLinkedEnvironment(relayRouteOffer.environmentId); handleOpenChange(false); toastManager.add({ type: "success", diff --git a/apps/web/src/components/settings/ConnectionsSettings.tsx b/apps/web/src/components/settings/ConnectionsSettings.tsx index 8abc58bea493..bb99f1bc1e83 100644 --- a/apps/web/src/components/settings/ConnectionsSettings.tsx +++ b/apps/web/src/components/settings/ConnectionsSettings.tsx @@ -51,6 +51,7 @@ import { peerLinkEnvironment, usePeerLinkHosts, } from "~/state/peerLinks"; +import { dismissLinkedEnvironment } from "~/state/linkedEnvironmentDismissals"; import { LoadBalancingSettings } from "./LoadBalancingSettings"; import { PullRequestRoutingSettings } from "./PullRequestRoutingSettings"; import { Input } from "../ui/input"; @@ -691,10 +692,15 @@ function ConnectionsSettingsContent({ setSavedBackendError(null); // Read before removing: the lists are this app's, and removal changes what it reports. const linkingHosts = hostsLinking(peerLinkHosts, environmentId); + // A host linking it, loaded here or not, would add it straight back. Recorded first, so it + // never does. + const undoDismiss = dismissLinkedEnvironment(environmentId); const result = await removeEnvironment(environmentId); setRemovingSavedEnvironmentId(null); if (result._tag === "Failure") { if (isAtomCommandInterrupted(result)) return; + // Still in the app, so nothing should keep it from coming back. + void undoDismiss.then((undo) => undo()); const error = squashAtomCommandFailure(result); const message = error instanceof Error ? error.message : "Failed to remove backend."; setSavedBackendError(message); diff --git a/apps/web/src/components/settings/LinkedEnvironmentsSettings.tsx b/apps/web/src/components/settings/LinkedEnvironmentsSettings.tsx index 209a4a5e1e6e..12a90de531fa 100644 --- a/apps/web/src/components/settings/LinkedEnvironmentsSettings.tsx +++ b/apps/web/src/components/settings/LinkedEnvironmentsSettings.tsx @@ -1,7 +1,6 @@ import { useAtomValue } from "@effect/atom-react"; import { ANOTHER_ADDRESS, - appPairingAddresses, type KnownEnvironmentRow, linkRequestFailureMessage, } from "@t3tools/client-runtime/link-request"; @@ -25,9 +24,15 @@ import { useId, useRef, useState } from "react"; import { requestConfirmDialog } from "~/confirmDialog"; import { mintPairingCodeOn } from "~/state/peerPairingCode"; import { environmentCatalog } from "~/connection/catalog"; -import { connectPairing as connectPairingAtom } from "~/connection/onboarding"; -import { useKnownEnvironments } from "~/state/knownEnvironments"; -import { forgetLinkOnConnect, peerLinkEnvironment, useLinkableMachines } from "~/state/peerLinks"; +import { linkedEnvironmentsRefreshKey, useKnownEnvironments } from "~/state/knownEnvironments"; +import { dismissLinkedEnvironment } from "~/state/linkedEnvironmentDismissals"; +import { + addingLinkedEnvironmentsAtom, + addLinkedEnvironmentToApp, + forgetLinkOnConnect, + peerLinkEnvironment, + useLinkableMachines, +} from "~/state/peerLinks"; import { useEnvironmentScope, usePreparedConnection } from "~/state/session"; import { useAtomCommand } from "~/state/use-atom-command"; import { accessConfig } from "../auth/ConnectAgentSurface"; @@ -76,7 +81,7 @@ export function LinkedEnvironmentsSettings({ if (!known.isPending) known.refresh(); }, { - key: `linked-environments:${environmentId}`, + key: linkedEnvironmentsRefreshKey(environmentId), intervalMs: null, refreshOnShow: true, }, @@ -176,11 +181,11 @@ function LinkedEnvironmentRow({ const health = peerLinkHealth(link, DateTime.makeUnsafe(nowMinute)); const unlink = useAtomCommand(peerLinkEnvironment.unlink, { label: "unlink environment" }); const canUnlink = useAtomValue(peerLinkEnvironment.unlink.permissionAtom(environmentId)); - const addToApp = useAtomCommand(peerLinkEnvironment.addToApp, { reportFailure: false }); const canAddToApp = useAtomValue(peerLinkEnvironment.addToApp.permissionAtom(environmentId)); - const connectPairing = useAtomCommand(connectPairingAtom, { reportFailure: false }); const removeFromApp = useAtomCommand(environmentCatalog.remove, { reportFailure: false }); const [busy, setBusy] = useState(false); + // An add of this machine runs from any host's row or the automatic add; one at a time. + const adding = useAtomValue(addingLinkedEnvironmentsAtom).has(link.environmentId); const forget = async () => { const confirmed = await (requestConfirmDialog( `Forget ${link.label}? Agents here stop working there. Revoke "T3 Code · …" in ${link.label}'s Sharing & access to end the session there too. An app added through this link stays under Authorized clients there until you revoke it.`, @@ -203,55 +208,33 @@ function LinkedEnvironmentRow({ { variant: "destructive" }, ) ?? Promise.resolve(false)); if (!alsoRemove) return; + // Another host may still link it. Recorded first, so it is never added back on its own. + const undoDismiss = dismissLinkedEnvironment(link.environmentId); const removed = await removeFromApp(link.environmentId); if (removed._tag === "Success") forgetLinkOnConnect(link.environmentId); else if (!isAtomCommandInterrupted(removed)) { + // Still in the app, so it is not removed, and nothing should keep it from coming back. + void undoDismiss.then((undo) => undo()); failureToast(`Could not remove ${row.label}`, String(squashAtomCommandFailure(removed))); } }; - const connectAt = async (hosts: ReadonlyArray, pairingCode: string) => { - // A host that does not answer as this machine never receives the code, so the next can. - let result: Awaited> | undefined; - for (const host of hosts) { - result = await connectPairing({ - host, - pairingCode, - expectedEnvironmentId: link.environmentId, - }); - if (result._tag === "Success" || isAtomCommandInterrupted(result)) return result; - } - return result; - }; const add = async () => { if (row.appAddress === null) return; - setBusy(true); - const issued = await addToApp({ environmentId, input: { environmentId: link.environmentId } }); - // Where the peer just answered first, then its other addresses this client reaches. - const hosts = - issued._tag === "Success" ? appPairingAddresses(issued.value.urls, row.onHostMachine) : []; - const added = - issued._tag === "Success" ? await connectAt(hosts, issued.value.credential) : issued; - setBusy(false); - if (added === undefined) { - failureToast( - `Could not add ${link.label} to this app`, - `${link.label} answers only at addresses this device cannot open.`, - ); - return; - } - if (added._tag === "Failure") { - if (!isAtomCommandInterrupted(added)) { - failureToast( - `Could not add ${link.label} to this app`, - String(squashAtomCommandFailure(added)), - ); - } + // Pairing clears a dismissal on success, so a later removal starts fresh. + const added = await addLinkedEnvironmentToApp(environmentId, { + environmentId: link.environmentId, + label: link.label, + onHostMachine: row.onHostMachine, + }); + if (added._tag === "Interrupted") return; + if (added._tag === "Failed") { + failureToast(`Could not add ${link.label} to this app`, added.message); return; } toastManager.add({ type: "success", title: `Added ${link.label} to this app`, - ...(issued._tag === "Success" && issued.value.access === "read-only" + ...(added.access === "read-only" ? { description: "Read-only: the link allows no more. Link it with full access to act there.", @@ -300,15 +283,21 @@ function LinkedEnvironmentRow({ {canUnlink || (canAddToApp && row.action === "add-to-app") ? (
{canAddToApp && row.action === "add-to-app" ? ( - ) : null} {canUnlink ? (