Skip to content

Commit 78f5b59

Browse files
committed
refactor(web): simplify auto balance update handling
1 parent 8a0968e commit 78f5b59

2 files changed

Lines changed: 29 additions & 58 deletions

File tree

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

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export interface ServerUpdateTarget {
4444
readonly continueThreadsAfterServerUpdate?: boolean;
4545
}
4646

47+
type UpdateButtonProps = Pick<ComponentProps<typeof Button>, "variant" | "size"> & {
48+
readonly label?: string;
49+
};
50+
4751
function useServerUpdate() {
4852
const updateServer = useAtomCommand(serverEnvironment.updateServer, { reportFailure: false });
4953
return async (target: ServerUpdateTarget, failureTitle = "Server update failed") => {
@@ -62,12 +66,7 @@ function useServerUpdate() {
6266
});
6367
if (result._tag === "Failure") {
6468
if (isAtomCommandInterrupted(result)) return;
65-
toastManager.add({
66-
type: "error",
67-
title: failureTitle,
68-
description: updateFailureMessage(squashAtomCommandFailure(result)),
69-
});
70-
return;
69+
throw squashAtomCommandFailure(result);
7170
}
7271
toastManager.add({
7372
type: "success",
@@ -95,11 +94,8 @@ export function ServerUpdatesAction({
9594
label = "Update all",
9695
variant = "outline",
9796
size = "xs",
98-
}: {
97+
}: UpdateButtonProps & {
9998
readonly targets: ReadonlyArray<ServerUpdateTarget>;
100-
readonly label?: string;
101-
readonly variant?: ComponentProps<typeof Button>["variant"];
102-
readonly size?: ComponentProps<typeof Button>["size"];
10399
}) {
104100
const update = useServerUpdate();
105101
const pending = useRef(false);
@@ -195,20 +191,7 @@ export function ServerUpdateAction({
195191
label = "Update",
196192
variant = "outline",
197193
size = "xs",
198-
}: {
199-
readonly environmentId: EnvironmentId;
200-
readonly serverLabel: string;
201-
readonly selfUpdate: ServerSelfUpdateCapability | null;
202-
/** The desktop app supervising this server accepts remote update
203-
requests (capabilities.desktopAppUpdate). */
204-
readonly desktopAppUpdate?: boolean;
205-
/** The server can durably continue running provider turns after updating. */
206-
readonly threadContinuation?: boolean;
207-
readonly targetVersion: string;
208-
readonly label?: string;
209-
readonly variant?: ComponentProps<typeof Button>["variant"];
210-
readonly size?: ComponentProps<typeof Button>["size"];
211-
}) {
194+
}: Omit<ServerUpdateTarget, "continueThreadsAfterServerUpdate"> & UpdateButtonProps) {
212195
const isDesktopAppUpdate = selfUpdate === "desktop-managed";
213196
const continueThreadsAfterServerUpdate = useEnvironmentSettings(
214197
environmentId,

‎apps/web/src/components/chat/useAutoBalanceUpdateBanner.tsx‎

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,21 @@ export function useAutoBalanceUpdateBanner(
7777
});
7878
if (machines.length === 0) return null;
7979

80-
const running = machines.filter((machine) => machine.state.status === "running");
81-
const failed = machines.filter((machine) => machine.state.status === "failed");
82-
const manual = machines.filter((machine) => !machine.remoteUpdate);
80+
const running = machines.filter((machine) => machine.state.status === "running").length;
81+
const failed = machines.filter((machine) => machine.state.status === "failed").length;
82+
const manual = machines.filter((machine) => !machine.remoteUpdate).length;
8383
const targets = machines.filter(
8484
(machine) => machine.connected && machine.remoteUpdate && machine.state.status !== "running",
8585
);
86-
const title =
87-
running.length > 0
88-
? `Updating ${running.length} ${running.length === 1 ? "machine" : "machines"}`
89-
: failed.length > 0
90-
? `Could not update ${failed.length} ${failed.length === 1 ? "machine" : "machines"}`
91-
: `Update available for ${machines.length} ${machines.length === 1 ? "machine" : "machines"}`;
86+
const count = running || failed || machines.length;
87+
const status = running ? "running" : failed ? "failed" : "idle";
88+
const prefix = running ? "Updating" : failed ? "Could not update" : "Update available for";
89+
const title = `${prefix} ${count} ${count === 1 ? "machine" : "machines"}`;
9290
return {
9391
id: `auto-balance-server-updates-${dismissedNotices.size}`,
94-
variant: failed.length > 0 ? "error" : "default",
95-
priority: running.length > 0 ? "urgent" : "notice",
96-
icon: (
97-
<ComposerServerUpdateIcon
98-
status={running.length > 0 ? "running" : failed.length > 0 ? "failed" : "idle"}
99-
/>
100-
),
92+
variant: failed ? "error" : "default",
93+
priority: running ? "urgent" : "notice",
94+
icon: <ComposerServerUpdateIcon status={status} />,
10195
title: (
10296
<Popover>
10397
<PopoverTrigger
@@ -132,40 +126,34 @@ export function useAutoBalanceUpdateBanner(
132126
</Popover>
133127
),
134128
description:
135-
manual.length > 0
136-
? `${manual.length} ${manual.length === 1 ? "needs" : "need"} a manual update`
137-
: undefined,
129+
manual > 0 ? `${manual} ${manual === 1 ? "needs" : "need"} a manual update` : undefined,
138130
actions:
139-
running.length === 0 && targets.length > 0 ? (
131+
running === 0 && targets.length > 0 ? (
140132
<ServerUpdatesAction
141133
targets={targets}
142134
variant="ghost"
143135
label={
144-
failed.length > 0
136+
failed > 0
145137
? "Retry"
146138
: targets.length === machines.length
147139
? "Update all"
148140
: `Update ${targets.length} ${targets.length === 1 ? "machine" : "machines"}`
149141
}
150142
/>
151143
) : undefined,
152-
...(running.length > 0
144+
dismissLabel: "Dismiss update notice",
145+
...(running
153146
? {}
154147
: {
155-
dismissLabel: "Dismiss update notice",
156148
onDismiss: () => {
157-
for (const machine of machines) {
158-
dismissServerUpdateFailure(machine.state);
159-
dismissVersionMismatch(machine.dismissKey);
149+
const next = new Set(dismissedNotices);
150+
for (const { state, dismissKey } of machines) {
151+
dismissServerUpdateFailure(state);
152+
dismissVersionMismatch(dismissKey);
153+
if (dismissKey) next.add(dismissKey);
154+
if (state.status === "failed") next.add(state);
160155
}
161-
setDismissedNotices((current) => {
162-
const next = new Set(current);
163-
for (const machine of machines) {
164-
if (machine.dismissKey) next.add(machine.dismissKey);
165-
if (machine.state.status === "failed") next.add(machine.state);
166-
}
167-
return next;
168-
});
156+
setDismissedNotices(next);
169157
},
170158
}),
171159
};

0 commit comments

Comments
 (0)