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

UpdateEverythingDialog.tsx

250 lines9.3 KB

1import { useAtomValue } from "@effect/atom-react";
2import { EnvironmentId } from "@t3tools/contracts";
3import { Atom } from "effect/reactivity";
4import { ChevronRightIcon } from "lucide-react";
5import { Fragment, type RefObject, useRef } from "react";
6​
7import { environmentThreadShells } from "~/state/threads";
8import {
9 AlertDialog,
10 AlertDialogClose,
11 AlertDialogDescription,
12 AlertDialogFooter,
13 AlertDialogHeader,
14 AlertDialogPopup,
15 AlertDialogTitle,
16} from "../ui/alert-dialog";
17import { Button } from "../ui/button";
18import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible";
19import { MiddleTruncate } from "../ui/middle-truncate";
20import { ScrollArea } from "../ui/scroll-area";
21import type { RestartingMachine, UpdateEverythingRow } from "./updateEverything.logic";
22​
23const isRunning = (status: string) =>
24 status === "preparing" || status === "starting" || status === "running";
25​
26const runningThreadCountAtom = Atom.family((environmentIds: string) =>
27 Atom.make((get) =>
28 environmentIds
29 .split("\n")
30 .filter((id) => id !== "")
31 .reduce(
32 (count, id) =>
33 count +
34 get(environmentThreadShells.environmentThreadsAtom(EnvironmentId.make(id))).filter(
35 (thread) => isRunning(thread.status),
36 ).length,
37 0,
38 ),
39 ).pipe(Atom.withLabel(`update-everything-running-threads:${environmentIds}`)),
40);
41​
42interface UpdateEverythingDialogProps {
43 readonly open: boolean;
44 readonly onOpenChange: (open: boolean) => void;
45 readonly version: string | null;
46 readonly providerCount: number;
47 readonly rows: ReadonlyArray<UpdateEverythingRow>;
48 readonly restarts: ReadonlyArray<RestartingMachine>;
49 readonly onConfirm: () => void;
50 /** Overrides the "Update N machines?" title, for an update-track switch. */
51 readonly title?: string;
52 readonly confirmLabel?: string;
53 readonly notes?: ReadonlyArray<string>;
54 /** Shown in place of running-thread counts; disables the confirm button. */
55 readonly disabledReason?: string | null;
56 /** The confirmed work is starting; it cannot be cancelled from here. */
57 readonly busy?: boolean;
58 /** Machines are still looking for provider updates, so more rows can appear. */
59 readonly checking?: boolean;
61​
62type UpdateEverythingDialogContentProps = Omit<
63 UpdateEverythingDialogProps,
64 "open" | "onOpenChange"
65> & {
66 readonly cancelRef: RefObject<HTMLButtonElement | null>;
67};
68​
69export function UpdateEverythingDialog({
70 open,
71 onOpenChange,
72 ...content
73}: UpdateEverythingDialogProps) {
74 const cancelRef = useRef<HTMLButtonElement>(null);
75 return (
76 <AlertDialog open={open} onOpenChange={onOpenChange}>
77 {/* Refocusing the trigger after a pointer close shows its focus ring and opens its tooltip. */}
78 <AlertDialogPopup
79 className="sm:max-w-xl"
80 initialFocus={cancelRef}
81 finalFocus={(closeType) => closeType === "keyboard"}
82 >
83 <UpdateEverythingDialogContent {...content} cancelRef={cancelRef} />
84 </AlertDialogPopup>
85 </AlertDialog>
86 );
88​
89const APP_LABELS = {
90 "this-app": "This app",
91 server: "Server",
92 "desktop-app": "Desktop app",
93} as const;
94​
95function UpdateEverythingDialogContent({
96 version,
97 providerCount,
98 rows,
99 restarts,
100 onConfirm,
101 cancelRef,
102 title,
103 confirmLabel = "Update",
104 notes = [],
105 disabledReason = null,
106 busy = false,
107 checking = false,
108}: UpdateEverythingDialogContentProps) {
109 const stopped = useAtomValue(
110 runningThreadCountAtom(
111 restarts
112 .filter((machine) => !machine.continuesThreads)
113 .map((machine) => machine.environmentId)
114 .join("\n"),
115 ),
116 );
117 const continuing = useAtomValue(
118 runningThreadCountAtom(
119 restarts
120 .filter((machine) => machine.continuesThreads)
121 .map((machine) => machine.environmentId)
122 .join("\n"),
123 ),
124 );
125 const running = stopped + continuing;
126 const threads = `${running} running ${running === 1 ? "thread" : "threads"}`;
127 const restarting = new Set(restarts.map((machine) => machine.environmentId));
128 const updating = rows.filter((row) => row.app !== null || row.providers.length > 0);
129 const skipped = rows.filter((row) => row.skipped !== null);
130​
131 return (
132 <>
133 <AlertDialogHeader className="shrink-0">
134 <AlertDialogTitle>
135 {title ?? `Update ${updating.length} ${updating.length === 1 ? "machine" : "machines"}?`}
136 </AlertDialogTitle>
137 <AlertDialogDescription>
138 {version !== null ? (
139 <>
140 T3 Code <span className="font-mono text-xs">{version}</span>
141 </>
142 ) : null}
143 {version !== null && providerCount > 0 ? " · " : null}
144 {providerCount > 0
145 ? `${providerCount} provider ${providerCount === 1 ? "update" : "updates"}`
146 : null}
147 {checking
148 ? `${version !== null || providerCount > 0 ? " · " : ""}Checking for more…`
149 : null}
150 </AlertDialogDescription>
151 </AlertDialogHeader>
152 <ScrollArea scrollFade>
153 <div className="flex flex-col gap-3 px-6 pb-4">
154 {notes.map((note) => (
155 <p key={note} className="text-muted-foreground text-sm">
156 {note}
157 </p>
158 ))}
159 {/* Machine names truncate first. Provider lists wrap between names, never inside one. */}
160 <div className="grid grid-cols-[minmax(4rem,max-content)_max-content_minmax(min-content,1fr)_max-content] gap-x-4 text-sm">
161 <div className="col-span-full grid grid-cols-subgrid border-b pb-1.5 text-muted-foreground text-xs">
162 <span>Machine</span>
163 <span>T3 Code</span>
164 <span>Providers</span>
165 <span />
166 </div>
167 {updating.map((row) => (
168 <div
169 key={row.environmentId ?? "this-app"}
170 className="col-span-full grid grid-cols-subgrid border-b border-border/50 py-1.5 last:border-b-0"
171 >
172 <span className="max-w-32 min-w-0">
173 <MiddleTruncate value={row.label} />
174 </span>
175 <span>
176 {row.app !== null ? APP_LABELS[row.app] : null}
177 {row.app === null && row.skipped !== null ? (
178 <span className="text-muted-foreground">Skipped</span>
179 ) : null}
180 {row.app === "this-app" ? (
181 <span className="block text-muted-foreground text-xs">restarts last</span>
182 ) : null}
183 </span>
184 <span>
185 {row.providers.map((provider, index) => (
186 <Fragment key={provider}>
187 <span className="whitespace-nowrap">{provider}</span>
188 {index < row.providers.length - 1 ? ", " : null}
189 </Fragment>
190 ))}
191 </span>
192 <span className="text-right">
193 {row.environmentId !== null && restarting.has(row.environmentId) ? (
194 <RunningThreads environmentId={row.environmentId} />
195 ) : null}
196 </span>
197 </div>
198 ))}
199 </div>
200 {skipped.length > 0 ? (
201 <Collapsible>
202 <CollapsibleTrigger className="flex items-center gap-1 text-muted-foreground text-xs hover:text-foreground data-panel-open:[&_svg]:rotate-90">
203 <ChevronRightIcon className="size-3.5 transition-transform" aria-hidden />
204 {skipped.length} skipped
205 </CollapsibleTrigger>
206 <CollapsiblePanel>
207 <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">
208 {skipped.map((row) => (
209 <Fragment key={row.environmentId ?? "this-app"}>
210 <span className="max-w-32 min-w-0">
211 <MiddleTruncate value={row.label} />
212 </span>
213 <span className="wrap-anywhere">{row.skipped}</span>
214 </Fragment>
215 ))}
216 </div>
217 </CollapsiblePanel>
218 </Collapsible>
219 ) : null}
220 </div>
221 </ScrollArea>
222 <AlertDialogFooter>
223 {disabledReason !== null ? (
224 <p className="self-center text-sm max-sm:order-1 max-sm:text-center sm:mr-auto">
225 {disabledReason}
226 </p>
227 ) : running > 0 ? (
228 <p className="self-center text-muted-foreground text-sm max-sm:order-1 max-sm:text-center sm:mr-auto">
229 {stopped === 0
230 ? `${threads} will be interrupted and can resume after the restart.`
231 : continuing === 0
232 ? `${threads} will be interrupted.`
233 : `${threads} will be interrupted. ${continuing} can resume after the restart.`}
234 </p>
235 ) : null}
236 <AlertDialogClose ref={cancelRef} render={<Button variant="outline" disabled={busy} />}>
237 Cancel
238 </AlertDialogClose>
239 <Button disabled={disabledReason !== null || busy} onClick={onConfirm}>
240 {confirmLabel}
241 </Button>
242 </AlertDialogFooter>
243 </>
244 );
246​
247function RunningThreads({ environmentId }: { readonly environmentId: EnvironmentId }) {
248 const count = useAtomValue(runningThreadCountAtom(environmentId));
249 return count > 0 ? <span className="whitespace-nowrap text-warning">{count} running</span> : null;