Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useAutoBalanceUpdateBanner.tsx

158 lines6.2 KB

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