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