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

useAutoBalanceUpdateBanner.tsx

196 lines7.1 KB

1import { T, useGT, useMessages, plural, part } from "gt-react";
2import { useAtomValue } from "@effect/atom-react";
3import type { ServerUpdateState } from "@t3tools/client-runtime/state/server";
4import { useState } from "react";
5​
6import { environmentIdsKey } from "~/state/environmentAtomList";
7import type { EnvironmentPresentation } from "~/state/environments";
8import { environmentUpdateStatesAtom } from "~/state/server";
9import {
10 buildVersionMismatchDismissalKey,
11 dismissServerUpdateFailure,
12 dismissVersionMismatch,
13 isServerUpdateFailureDismissed,
14 isVersionMismatchDismissed,
15 resolveServerConfigVersionMismatch,
16 resolveServerSelfUpdateCapability,
17 supportsDesktopAppUpdate,
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 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`${plural(
88 { count },
89 {
90 one: part`Updating ${{ count }} machine`,
91 other: part`Updating ${{ count }} machines`,
92 },
93 )}`
94 : failed
95 ? gt`${plural(
96 { count },
97 {
98 one: part`Could not update ${{ count }} machine`,
99 other: part`Could not update ${{ count }} machines`,
100 },
101 )}`
102 : gt`${plural(
103 { count },
104 {
105 one: part`Update available for ${{ count }} machine`,
106 other: part`Update available for ${{ count }} machines`,
107 },
108 )}`;
109 return {
110 id: `auto-balance-server-updates-${dismissedNotices.size}`,
111 variant: failed ? "error" : "default",
112 group: "environment",
113 icon: <ComposerServerUpdateIcon status={status} />,
114 title: (
115 <Popover>
116 <PopoverTrigger
117 render={<InlineButton />}
118 className="max-w-full"
119 aria-label={gt`${{ title }}. View machines`}
120 >
121 <span className="min-w-0 truncate">{title}</span>
122 </PopoverTrigger>
123 <PopoverPopup side="top" align="start" width="md">
124 <div className="space-y-3 text-xs">
125 {machines.map((machine) => (
126 <div key={machine.environmentId} className="space-y-1">
127 <div className="font-medium">{machine.serverLabel}</div>
128 {machine.state.status !== "idle" ? (
129 <ServerUpdateProgress state={machine.state} />
130 ) : !machine.remoteUpdate ? (
131 <>
132 <div className="text-muted-foreground">
133 <T>Manual update required</T>
134 </div>
135 <ServerUpdateAction {...machine} />
136 </>
137 ) : (
138 <div className="text-muted-foreground">
139 {machine.connected
140 ? gt`Ready to update to ${{ version: machine.targetVersion }}`
141 : gt`Reconnect this machine to update`}
142 </div>
143 )}
144 </div>
145 ))}
146 </div>
147 </PopoverPopup>
148 </Popover>
149 ),
150 description:
151 manual > 0
152 ? gt`${plural(
153 { manual },
154 {
155 one: part`${{ manual }} needs a manual update`,
156 other: part`${{ manual }} need a manual update`,
157 },
158 )}`
159 : undefined,
160 actions:
161 running === 0 && targets.length > 0 ? (
162 <ServerUpdatesAction
163 targets={targets}
164 variant="ghost"
165 label={
166 failed > 0
167 ? gt({ context: "verb, retries the server updates" })`Retry`
168 : targets.length === machines.length
169 ? gt`Update all`
170 : gt`${plural(
171 { count: targets.length },
172 {
173 one: part`Update ${{ count: targets.length }} machine`,
174 other: part`Update ${{ count: targets.length }} machines`,
175 },
176 )}`
177 }
178 />
179 ) : undefined,
180 dismissLabel: gt`Dismiss update notice`,
181 ...(running
182 ? {}
183 : {
184 onDismiss: () => {
185 const next = new Set(dismissedNotices);
186 for (const { state, dismissKey } of machines) {
187 dismissServerUpdateFailure(state);
188 dismissVersionMismatch(dismissKey);
189 if (dismissKey) next.add(dismissKey);
190 if (state.status === "failed") next.add(state);
191 }
192 setDismissedNotices(next);
193 },
194 }),
195 };