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

UpdateEverythingDialog.tsx

350 lines13.2 KB

1import { useAtomValue } from "@effect/atom-react";
2import { EnvironmentId } from "@t3tools/contracts";
3import { Atom } from "effect/reactivity";
4import { ChevronRightIcon } from "lucide-react";
5import { msg, T, useGT, useMessages, plural, part, Var } from "gt-react";
6import { Fragment, type RefObject, useRef } from "react";
7​
8import { environmentThreadShells } from "~/state/threads";
9import {
10 AlertDialog,
11 AlertDialogClose,
12 AlertDialogDescription,
13 AlertDialogFooter,
14 AlertDialogHeader,
15 AlertDialogPopup,
16 AlertDialogTitle,
17} from "../ui/alert-dialog";
18import { Button } from "../ui/button";
19import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible";
20import { MiddleTruncate } from "../ui/middle-truncate";
21import { ScrollArea } from "../ui/scroll-area";
22import type { RestartingMachine, UpdateEverythingRow } from "./updateEverything.logic";
23​
24const isRunning = (status: string) =>
25 status === "preparing" || status === "starting" || status === "running";
26​
27const runningThreadCountAtom = Atom.family((environmentIds: string) =>
28 Atom.make((get) =>
29 environmentIds
30 .split("\n")
31 .filter((id) => id !== "")
32 .reduce(
33 (count, id) =>
34 count +
35 get(environmentThreadShells.environmentThreadsAtom(EnvironmentId.make(id))).filter(
36 (thread) => isRunning(thread.status),
37 ).length,
38 0,
39 ),
40 ).pipe(Atom.withLabel(`update-everything-running-threads:${environmentIds}`)),
41);
42​
43interface UpdateEverythingDialogProps {
44 readonly open: boolean;
45 readonly onOpenChange: (open: boolean) => void;
46 readonly version: string | null;
47 /** `version` is where servers move to; this app has no update in this run. */
48 readonly serversOnly?: boolean;
49 readonly providerCount: number;
50 readonly rows: ReadonlyArray<UpdateEverythingRow>;
51 readonly restarts: ReadonlyArray<RestartingMachine>;
52 readonly onConfirm: () => void;
53 /** Overrides the "Update N machines?" title, for an update-track switch. */
54 readonly title?: string;
55 readonly confirmLabel?: string;
56 readonly notes?: ReadonlyArray<string>;
57 /** Shown in place of running-thread counts; disables the confirm button. */
58 readonly disabledReason?: string | null;
59 /** The confirmed work is starting; it cannot be cancelled from here. */
60 readonly busy?: boolean;
61 /** Machines are still looking for provider updates, so more rows can appear. */
62 readonly checking?: boolean;
64​
65type UpdateEverythingDialogContentProps = Omit<
66 UpdateEverythingDialogProps,
67 "open" | "onOpenChange"
68> & {
69 readonly cancelRef: RefObject<HTMLButtonElement | null>;
70};
71​
72export function UpdateEverythingDialog({
73 open,
74 onOpenChange,
75 ...content
76}: UpdateEverythingDialogProps) {
77 const cancelRef = useRef<HTMLButtonElement>(null);
78 return (
79 <AlertDialog open={open} onOpenChange={onOpenChange}>
80 {/* Refocusing the trigger after a pointer close shows its focus ring and opens its tooltip. */}
81 <AlertDialogPopup
82 className="sm:max-w-xl"
83 initialFocus={cancelRef}
84 finalFocus={(closeType) => closeType === "keyboard"}
85 >
86 <UpdateEverythingDialogContent {...content} cancelRef={cancelRef} />
87 </AlertDialogPopup>
88 </AlertDialog>
89 );
91​
92const APP_LABELS = {
93 "this-app": msg("This app"),
94 server: msg("Server"),
95 "desktop-app": msg("Desktop app"),
96} as const;
97​
98function UpdateEverythingDialogContent({
99 version,
100 serversOnly = false,
101 providerCount,
102 rows,
103 restarts,
104 onConfirm,
105 cancelRef,
106 title,
107 confirmLabel,
108 notes = [],
109 disabledReason = null,
110 busy = false,
111 checking = false,
112}: UpdateEverythingDialogContentProps) {
113 const gt = useGT();
114 const m = useMessages();
115 const stopped = useAtomValue(
116 runningThreadCountAtom(
117 restarts
118 .filter((machine) => !machine.continuesThreads)
119 .map((machine) => machine.environmentId)
120 .join("\n"),
121 ),
122 );
123 const continuing = useAtomValue(
124 runningThreadCountAtom(
125 restarts
126 .filter((machine) => machine.continuesThreads)
127 .map((machine) => machine.environmentId)
128 .join("\n"),
129 ),
130 );
131 const running = stopped + continuing;
132 const restarting = new Set(restarts.map((machine) => machine.environmentId));
133 const updating = rows.filter((row) => row.app !== null || row.providers.length > 0);
134 const skipped = rows.filter((row) => row.skipped !== null);
135 const hasNothingToUpdate = title === undefined && updating.length === 0;
136 const hasBody = notes.length > 0 || updating.length > 0 || skipped.length > 0;
137​
138 return (
139 <>
140 <AlertDialogHeader className="shrink-0">
141 <AlertDialogTitle>
142 {title ??
143 (updating.length === 0
144 ? checking
145 ? gt`Checking for updates…`
146 : skipped.length > 0
147 ? gt`Nothing to update`
148 : gt`Up to date`
149 : gt`${plural(
150 { count: updating.length },
151 {
152 one: part`Update ${{ count: updating.length }} machine?`,
153 other: part`Update ${{ count: updating.length }} machines?`,
154 },
155 )}`)}
156 </AlertDialogTitle>
157 {!hasNothingToUpdate ? (
158 <AlertDialogDescription>
159 {version === null ? null : serversOnly ? (
160 // Servers move to this app's version, which is not always the newest release.
161 <T>
162 Servers to{" "}
163 <span className="font-mono text-xs">
164 <Var>{version}</Var>
165 </span>
166 </T>
167 ) : (
168 <>
169 T3 Code <span className="font-mono text-xs">{version}</span>
170 </>
171 )}
172 {version !== null && providerCount > 0 ? " · " : null}
173 {providerCount > 0
174 ? gt`${plural(
175 { count: providerCount },
176 {
177 one: part`${{ count: providerCount }} provider update`,
178 other: part`${{ count: providerCount }} provider updates`,
179 },
180 )}`
181 : null}
182 {checking
183 ? `${version !== null || providerCount > 0 ? " · " : ""}${gt`Checking for more…`}`
184 : null}
185 </AlertDialogDescription>
186 ) : null}
187 </AlertDialogHeader>
188 {hasBody ? (
189 <ScrollArea scrollFade>
190 <div className="flex flex-col gap-3 px-6 pb-4">
191 {notes.map((note) => (
192 <p key={note} className="text-muted-foreground text-sm">
193 {note}
194 </p>
195 ))}
196 {/* Machine names truncate first. Provider lists wrap between names, never inside one. */}
197 {updating.length > 0 ? (
198 <div
199 role="table"
200 className="grid grid-cols-[minmax(4rem,max-content)_max-content_minmax(min-content,1fr)_max-content] gap-x-4 text-sm"
201 >
202 <div
203 role="row"
204 className="col-span-full grid grid-cols-subgrid border-b pb-1.5 text-muted-foreground text-xs"
205 >
206 <span role="columnheader">
207 <T>Machine</T>
208 </span>
209 <span role="columnheader">T3 Code</span>
210 <span role="columnheader">
211 <T>Providers</T>
212 </span>
213 <span role="columnheader" />
214 </div>
215 {updating.map((row) => (
216 <div
217 key={row.environmentId ?? "this-app"}
218 role="row"
219 className="col-span-full grid grid-cols-subgrid border-b border-border/50 py-1.5 last:border-b-0"
220 >
221 <span role="cell" className="max-w-32 min-w-0">
222 <span className="sr-only">{row.label}</span>
223 <MiddleTruncate value={row.label} aria-hidden />
224 </span>
225 <span role="cell">
226 {row.app !== null ? m(APP_LABELS[row.app]) : null}
227 {row.app === null && row.skipped !== null ? (
228 <span className="text-muted-foreground">
229 <T>Skipped</T>
230 </span>
231 ) : null}
232 {row.app === "this-app" ? (
233 <span className="block text-muted-foreground text-xs">
234 <T>restarts last</T>
235 </span>
236 ) : null}
237 </span>
238 <span role="cell">
239 {row.providers.map((provider, index) => (
240 <Fragment key={provider}>
241 <span className="whitespace-nowrap">{provider}</span>
242 {index < row.providers.length - 1 ? ", " : null}
243 </Fragment>
244 ))}
245 </span>
246 <span role="cell" className="text-right">
247 {row.environmentId !== null && restarting.has(row.environmentId) ? (
248 <RunningThreads environmentId={row.environmentId} />
249 ) : null}
250 </span>
251 </div>
252 ))}
253 </div>
254 ) : null}
255 {skipped.length > 0 ? (
256 <Collapsible>
257 <CollapsibleTrigger className="flex items-center gap-1 text-muted-foreground text-xs hover:text-foreground data-panel-open:[&_svg]:rotate-90">
258 <ChevronRightIcon className="size-3.5 transition-transform" aria-hidden />
259 {gt`${plural(
260 { count: skipped.length },
261 {
262 one: part`${{ count: skipped.length }} skipped`,
263 other: part`${{ count: skipped.length }} skipped`,
264 },
265 )}`}
266 </CollapsibleTrigger>
267 <CollapsiblePanel>
268 <div className="grid grid-cols-[minmax(0,max-content)_1fr] gap-x-3 gap-y-0.5 pt-1.5 ps-4.5 text-muted-foreground text-xs">
269 {skipped.map((row) => (
270 <Fragment key={row.environmentId ?? "this-app"}>
271 <span className="max-w-32 min-w-0">
272 <MiddleTruncate value={row.label} />
273 </span>
274 <span className="wrap-anywhere">{row.skipped}</span>
275 </Fragment>
276 ))}
277 </div>
278 </CollapsiblePanel>
279 </Collapsible>
280 ) : null}
281 </div>
282 </ScrollArea>
283 ) : null}
284 <AlertDialogFooter>
285 {disabledReason !== null ? (
286 <p className="self-center text-sm max-sm:order-1 max-sm:text-center sm:flex-1">
287 {disabledReason}
288 </p>
289 ) : running > 0 ? (
290 <p className="self-center text-muted-foreground text-sm max-sm:order-1 max-sm:text-center sm:flex-1">
291 {stopped === 0
292 ? gt`${plural(
293 { count: running },
294 {
295 one: part`${{ count: running }} running thread will be interrupted and can resume after the restart.`,
296 other: part`${{ count: running }} running threads will be interrupted and can resume after the restart.`,
297 },
298 )}`
299 : continuing === 0
300 ? gt`${plural(
301 { count: running },
302 {
303 one: part`${{ count: running }} running thread will be interrupted.`,
304 other: part`${{ count: running }} running threads will be interrupted.`,
305 },
306 )}`
307 : `${gt`${plural(
308 { count: running },
309 {
310 one: part`${{ count: running }} running thread will be interrupted.`,
311 other: part`${{ count: running }} running threads will be interrupted.`,
312 },
313 )}`} ${gt`${plural(
314 { count: continuing },
315 {
316 one: part`${{ count: continuing }} can resume after the restart.`,
317 other: part`${{ count: continuing }} can resume after the restart.`,
318 },
319 )}`}`}
320 </p>
321 ) : null}
322 <AlertDialogClose ref={cancelRef} render={<Button variant="outline" disabled={busy} />}>
323 <T>Cancel</T>
324 </AlertDialogClose>
325 <Button
326 disabled={disabledReason !== null || busy || hasNothingToUpdate}
327 onClick={onConfirm}
328 >
329 {confirmLabel ?? gt({ context: "verb, confirms the update" })`Update`}
330 </Button>
331 </AlertDialogFooter>
332 </>
333 );
335​
336function RunningThreads({ environmentId }: { readonly environmentId: EnvironmentId }) {
337 const count = useAtomValue(runningThreadCountAtom(environmentId));
338 const gt = useGT();
339 return count > 0 ? (
340 <span className="whitespace-nowrap text-warning">
341 {gt`${plural(
342 { count: count },
343 {
344 one: part`${{ count }} running`,
345 other: part`${{ count }} running`,
346 },
347 )}`}
348 </span>
349 ) : null;