Skip to content

Commit a5b08ae

Browse files
committed
fix(web): disconnect offline servers from threads
1 parent c07575f commit a5b08ae

1 file changed

Lines changed: 77 additions & 35 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 77 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ import {
308308
} from "../lib/composerContextRecords";
309309
import { type ReviewCommentContext } from "../reviewCommentContext";
310310
import { environmentCatalog } from "../connection/catalog";
311+
import { isDesktopLocalConnectionTarget } from "../connection/desktopLocal";
311312
import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
312313
import { useKnownTerminalSessions, useThreadRunningTerminalIds } from "../state/terminalSessions";
313314
import { useEnvironmentQuery } from "../state/query";
@@ -1497,6 +1498,9 @@ export default function ChatView(props: ChatViewProps) {
14971498
const { environments } = useEnvironments();
14981499
const primaryEnvironment = usePrimaryEnvironment();
14991500
const retryEnvironment = useAtomCommand(environmentCatalog.retryNow, { reportFailure: false });
1501+
const setEnvironmentEnabled = useAtomCommand(environmentCatalog.setEnabled, {
1502+
reportFailure: false,
1503+
});
15001504
const environmentById = useMemo(
15011505
() => new Map(environments.map((environment) => [environment.environmentId, environment])),
15021506
[environments],
@@ -2208,6 +2212,33 @@ export default function ChatView(props: ChatViewProps) {
22082212
},
22092213
[retryEnvironment],
22102214
);
2215+
const canDisconnectActiveEnvironment =
2216+
activeEnvironment !== null &&
2217+
activeEnvironment.entry.target._tag !== "PrimaryConnectionTarget" &&
2218+
!isDesktopLocalConnectionTarget(activeEnvironment.entry.target);
2219+
const [disconnectingEnvironment, setDisconnectingEnvironment] = useState(false);
2220+
const handleDisconnectActiveEnvironment = useCallback(
2221+
async (environmentId: EnvironmentId) => {
2222+
setDisconnectingEnvironment(true);
2223+
const result = await setEnvironmentEnabled({ environmentId, enabled: false });
2224+
setDisconnectingEnvironment(false);
2225+
if (result._tag === "Failure") {
2226+
if (!isAtomCommandInterrupted(result)) {
2227+
const error = squashAtomCommandFailure(result);
2228+
toastManager.add(
2229+
stackedThreadToast({
2230+
type: "error",
2231+
title: "Could not disconnect server",
2232+
description: error instanceof Error ? error.message : "Failed to disconnect.",
2233+
}),
2234+
);
2235+
}
2236+
return;
2237+
}
2238+
void navigate({ to: "/", replace: true });
2239+
},
2240+
[navigate, setEnvironmentEnabled],
2241+
);
22112242
const logicalProjectEnvironments = useMemo(() => {
22122243
if (!activeProject) return [];
22132244
const logicalKey = deriveLogicalProjectKeyFromSettings(activeProject, projectGroupingSettings);
@@ -2448,6 +2479,20 @@ export default function ChatView(props: ChatViewProps) {
24482479
const items: ComposerBannerStackItem[] = [];
24492480
const updateRunning = serverUpdateState.status === "running";
24502481
const unavailableConnection = activeEnvironmentUnavailableState?.connection ?? null;
2482+
const disconnectAction =
2483+
canDisconnectActiveEnvironment && activeEnvironmentUnavailableState ? (
2484+
<Button
2485+
size="xs"
2486+
variant="ghost"
2487+
disabled={disconnectingEnvironment}
2488+
title="Hide this server's threads. Switch it on again in Connections."
2489+
onClick={() =>
2490+
void handleDisconnectActiveEnvironment(activeEnvironmentUnavailableState.environmentId)
2491+
}
2492+
>
2493+
Disconnect server
2494+
</Button>
2495+
) : undefined;
24512496
const environmentReconnecting =
24522497
unavailableConnection !== null &&
24532498
(unavailableConnection.phase === "connecting" ||
@@ -2481,35 +2526,30 @@ export default function ChatView(props: ChatViewProps) {
24812526
),
24822527
title: `${unavailableConnection.phase === "connecting" ? "Connecting" : "Reconnecting"} to ${activeEnvironmentUnavailableState.label}`,
24832528
description: "Finishing an update",
2529+
actions: disconnectAction,
24842530
});
24852531
} else {
24862532
items.push({
24872533
id: `environment-unavailable:${activeEnvironmentUnavailableState.environmentId}`,
24882534
variant: unavailableConnection.phase === "error" ? "error" : "warning",
24892535
icon: <WifiOffIcon />,
24902536
title: `${activeEnvironmentUnavailableState.label} is ${environmentReconnecting ? "reconnecting" : "offline"}`,
2491-
description: environmentReconnecting ? "Trying again" : "Reconnect to continue",
24922537
actions: (
24932538
<>
2494-
<Button
2495-
size="xs"
2496-
variant="ghost"
2497-
disabled={environmentReconnecting}
2498-
onClick={() =>
2499-
void handleReconnectActiveEnvironment(
2500-
activeEnvironmentUnavailableState.environmentId,
2501-
)
2502-
}
2503-
>
2504-
{environmentReconnecting ? "Reconnecting..." : "Reconnect"}
2505-
</Button>
2506-
<Button
2507-
size="xs"
2508-
variant="ghost"
2509-
onClick={() => void navigate({ to: "/settings/connections" })}
2510-
>
2511-
Connections
2512-
</Button>
2539+
{!environmentReconnecting ? (
2540+
<Button
2541+
size="xs"
2542+
variant="ghost"
2543+
onClick={() =>
2544+
void handleReconnectActiveEnvironment(
2545+
activeEnvironmentUnavailableState.environmentId,
2546+
)
2547+
}
2548+
>
2549+
Reconnect
2550+
</Button>
2551+
) : null}
2552+
{disconnectAction}
25132553
</>
25142554
),
25152555
});
@@ -2564,22 +2604,22 @@ export default function ChatView(props: ChatViewProps) {
25642604
(versionMismatchSelfUpdate !== "desktop-managed" || !versionMismatchDesktopAppUpdate)
25652605
? serverUpdateGuidance(versionMismatchSelfUpdate)
25662606
: undefined,
2567-
actions:
2568-
updateInProgress ||
2569-
!versionMismatch ||
2607+
actions: updateInProgress ? (
2608+
disconnectAction
2609+
) : !versionMismatch ||
25702610
(versionMismatchSelfUpdate === "desktop-managed" &&
25712611
!versionMismatchDesktopAppUpdate) ? undefined : (
2572-
<ServerUpdateAction
2573-
environmentId={serverUpdateEnvironmentId}
2574-
serverLabel={versionMismatchServerLabel}
2575-
selfUpdate={versionMismatchSelfUpdate}
2576-
desktopAppUpdate={versionMismatchDesktopAppUpdate}
2577-
threadContinuation={versionMismatchThreadContinuation}
2578-
targetVersion={versionMismatch.clientVersion}
2579-
label={updateFailed ? "Retry" : "Update"}
2580-
variant="ghost"
2581-
/>
2582-
),
2612+
<ServerUpdateAction
2613+
environmentId={serverUpdateEnvironmentId}
2614+
serverLabel={versionMismatchServerLabel}
2615+
selfUpdate={versionMismatchSelfUpdate}
2616+
desktopAppUpdate={versionMismatchDesktopAppUpdate}
2617+
threadContinuation={versionMismatchThreadContinuation}
2618+
targetVersion={versionMismatch.clientVersion}
2619+
label={updateFailed ? "Retry" : "Update"}
2620+
variant="ghost"
2621+
/>
2622+
),
25832623
...(updateInProgress || (!updateFailed && !versionMismatchDismissKey)
25842624
? {}
25852625
: {
@@ -2603,7 +2643,9 @@ export default function ChatView(props: ChatViewProps) {
26032643
activeEnvironmentUnavailableState,
26042644
reconnectWarningGraceElapsed,
26052645
handleReconnectActiveEnvironment,
2606-
navigate,
2646+
canDisconnectActiveEnvironment,
2647+
disconnectingEnvironment,
2648+
handleDisconnectActiveEnvironment,
26072649
setDismissedVersionMismatchKey,
26082650
showVersionMismatchBanner,
26092651
serverUpdateFailureDismissed,

0 commit comments

Comments
 (0)