The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 180 lines 6.78 KB
Raw
1 import { T, useGT, useMessages } from "gt-react";
2 import { useAtomValue } from "@effect/atom-react";
3 import type { ServerUpdateState } from "@t3tools/client-runtime/state/server";
4 import { useState } from "react";
5
6 import { environmentIdsKey } from "~/state/environmentAtomList";
7 import type { EnvironmentPresentation } from "~/state/environments";
8 import { environmentUpdateStatesAtom } from "~/state/server";
9 import {
10 buildVersionMismatchDismissalKey,
11 dismissServerUpdateFailure,
12 dismissVersionMismatch,
13 isServerUpdateFailureDismissed,
14 isVersionMismatchDismissed,
15 resolveServerConfigVersionMismatch,
16 resolveServerSelfUpdateCapability,
17 supportsDesktopAppUpdate,
18 } from "~/versionSkew";
19 import {
20 ServerUpdateAction,
21 ServerUpdateProgress,
22 ServerUpdatesAction,
23 } from "../ServerUpdateAction";
24 import { InlineButton } from "../ui/button";
25 import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
26 import type { ComposerBannerStackItem } from "./ComposerBannerStack";
27 import { ComposerServerUpdateIcon } from "./ComposerServerUpdateStatus";
28
29 export function useAutoBalanceUpdateBanner(
30 environments: readonly EnvironmentPresentation[],
31 ): ComposerBannerStackItem | null {
32 const gt = useGT();
33 const m = useMessages();
34 const updateStates = useAtomValue(
35 environmentUpdateStatesAtom(
36 environmentIdsKey(environments.map(({ environmentId }) => environmentId)),
37 ),
38 );
39 const states = environments.flatMap((environment, index) => {
40 const state = updateStates[index];
41 return state ? [{ environment, state }] : [];
42 });
43 const [dismissedNotices, setDismissedNotices] = useState<ReadonlySet<string | ServerUpdateState>>(
44 () => new Set(),
45 );
46 const machines = states.flatMap(({ environment, state }) => {
47 const mismatch = resolveServerConfigVersionMismatch(environment.serverConfig, m);
48 const dismissKey = mismatch
49 ? buildVersionMismatchDismissalKey(environment.environmentId, mismatch)
50 : null;
51 if (
52 state.status === "idle"
53 ? !mismatch ||
54 (dismissKey !== null && dismissedNotices.has(dismissKey)) ||
55 isVersionMismatchDismissed(dismissKey)
56 : dismissedNotices.has(state) || isServerUpdateFailureDismissed(state)
57 )
58 return [];
59 const selfUpdate = resolveServerSelfUpdateCapability(environment.serverConfig);
60 const desktopAppUpdate = supportsDesktopAppUpdate(environment.serverConfig);
61 return [
62 {
63 environmentId: environment.environmentId,
64 serverLabel: environment.label,
65 selfUpdate,
66 installation: environment.serverConfig?.environment.capabilities.serverInstallation,
67 desktopAppUpdate,
68 targetVersion: state.status === "idle" ? mismatch!.clientVersion : state.targetVersion,
69 connected: environment.connection.phase === "connected",
70 remoteUpdate: selfUpdate !== null && (selfUpdate !== "desktop-managed" || desktopAppUpdate),
71 state,
72 dismissKey,
73 },
74 ];
75 });
76 if (machines.length === 0) return null;
77
78 const running = machines.filter((machine) => machine.state.status === "running").length;
79 const failed = machines.filter((machine) => machine.state.status === "failed").length;
80 const manual = machines.filter((machine) => !machine.remoteUpdate).length;
81 const targets = machines.filter(
82 (machine) => machine.connected && machine.remoteUpdate && machine.state.status !== "running",
83 );
84 const count = running || failed || machines.length;
85 const status = running ? "running" : failed ? "failed" : "idle";
86 const title = running
87 ? gt("{count, plural, one {Updating {count} machine} other {Updating {count} machines}}", {
88 count,
89 })
90 : failed
91 ? gt(
92 "{count, plural, one {Could not update {count} machine} other {Could not update {count} machines}}",
93 { count },
94 )
95 : gt(
96 "{count, plural, one {Update available for {count} machine} other {Update available for {count} machines}}",
97 { count },
98 );
99 return {
100 id: `auto-balance-server-updates-${dismissedNotices.size}`,
101 variant: failed ? "error" : "default",
102 group: "environment",
103 icon: <ComposerServerUpdateIcon status={status} />,
104 title: (
105 <Popover>
106 <PopoverTrigger
107 render={<InlineButton />}
108 className="max-w-full"
109 aria-label={gt("{title}. View machines", { title })}
110 >
111 <span className="min-w-0 truncate">{title}</span>
112 </PopoverTrigger>
113 <PopoverPopup side="top" align="start" width="md">
114 <div className="space-y-3 text-xs">
115 {machines.map((machine) => (
116 <div key={machine.environmentId} className="space-y-1">
117 <div className="font-medium">{machine.serverLabel}</div>
118 {machine.state.status !== "idle" ? (
119 <ServerUpdateProgress state={machine.state} />
120 ) : !machine.remoteUpdate ? (
121 <>
122 <div className="text-muted-foreground">
123 <T>Manual update required</T>
124 </div>
125 <ServerUpdateAction {...machine} />
126 </>
127 ) : (
128 <div className="text-muted-foreground">
129 {machine.connected
130 ? gt("Ready to update to {version}", { version: machine.targetVersion })
131 : gt("Reconnect this machine to update")}
132 </div>
133 )}
134 </div>
135 ))}
136 </div>
137 </PopoverPopup>
138 </Popover>
139 ),
140 description:
141 manual > 0
142 ? gt(
143 "{manual, plural, one {{manual} needs a manual update} other {{manual} need a manual update}}",
144 { manual },
145 )
146 : undefined,
147 actions:
148 running === 0 && targets.length > 0 ? (
149 <ServerUpdatesAction
150 targets={targets}
151 variant="ghost"
152 label={
153 failed > 0
154 ? gt("Retry", { $context: "verb, retries the server updates" })
155 : targets.length === machines.length
156 ? gt("Update all")
157 : gt(
158 "{count, plural, one {Update {count} machine} other {Update {count} machines}}",
159 { count: targets.length },
160 )
161 }
162 />
163 ) : undefined,
164 dismissLabel: gt("Dismiss update notice"),
165 ...(running
166 ? {}
167 : {
168 onDismiss: () => {
169 const next = new Set(dismissedNotices);
170 for (const { state, dismissKey } of machines) {
171 dismissServerUpdateFailure(state);
172 dismissVersionMismatch(dismissKey);
173 if (dismissKey) next.add(dismissKey);
174 if (state.status === "failed") next.add(state);
175 }
176 setDismissedNotices(next);
177 },
178 }),
179 };
180 }