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

UpdateEverythingDialog.tsx

258 lines9.7 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
161 role="table"
162 className="grid grid-cols-[minmax(4rem,max-content)_max-content_minmax(min-content,1fr)_max-content] gap-x-4 text-sm"
163 >
164 <div
165 role="row"
166 className="col-span-full grid grid-cols-subgrid border-b pb-1.5 text-muted-foreground text-xs"
167 >
168 <span role="columnheader">Machine</span>
169 <span role="columnheader">T3 Code</span>
170 <span role="columnheader">Providers</span>
171 <span role="columnheader" />
172 </div>
173 {updating.map((row) => (
174 <div
175 key={row.environmentId ?? "this-app"}
176 role="row"
177 className="col-span-full grid grid-cols-subgrid border-b border-border/50 py-1.5 last:border-b-0"
178 >
179 <span role="cell" className="max-w-32 min-w-0">
180 <span className="sr-only">{row.label}</span>
181 <MiddleTruncate value={row.label} aria-hidden />
182 </span>
183 <span role="cell">
184 {row.app !== null ? APP_LABELS[row.app] : null}
185 {row.app === null && row.skipped !== null ? (
186 <span className="text-muted-foreground">Skipped</span>
187 ) : null}
188 {row.app === "this-app" ? (
189 <span className="block text-muted-foreground text-xs">restarts last</span>
190 ) : null}
191 </span>
192 <span role="cell">
193 {row.providers.map((provider, index) => (
194 <Fragment key={provider}>
195 <span className="whitespace-nowrap">{provider}</span>
196 {index < row.providers.length - 1 ? ", " : null}
197 </Fragment>
198 ))}
199 </span>
200 <span role="cell" className="text-right">
201 {row.environmentId !== null && restarting.has(row.environmentId) ? (
202 <RunningThreads environmentId={row.environmentId} />
203 ) : null}
204 </span>
205 </div>
206 ))}
207 </div>
208 {skipped.length > 0 ? (
209 <Collapsible>
210 <CollapsibleTrigger className="flex items-center gap-1 text-muted-foreground text-xs hover:text-foreground data-panel-open:[&_svg]:rotate-90">
211 <ChevronRightIcon className="size-3.5 transition-transform" aria-hidden />
212 {skipped.length} skipped
213 </CollapsibleTrigger>
214 <CollapsiblePanel>
215 <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">
216 {skipped.map((row) => (
217 <Fragment key={row.environmentId ?? "this-app"}>
218 <span className="max-w-32 min-w-0">
219 <MiddleTruncate value={row.label} />
220 </span>
221 <span className="wrap-anywhere">{row.skipped}</span>
222 </Fragment>
223 ))}
224 </div>
225 </CollapsiblePanel>
226 </Collapsible>
227 ) : null}
228 </div>
229 </ScrollArea>
230 <AlertDialogFooter>
231 {disabledReason !== null ? (
232 <p className="self-center text-sm max-sm:order-1 max-sm:text-center sm:mr-auto">
233 {disabledReason}
234 </p>
235 ) : running > 0 ? (
236 <p className="self-center text-muted-foreground text-sm max-sm:order-1 max-sm:text-center sm:mr-auto">
237 {stopped === 0
238 ? `${threads} will be interrupted and can resume after the restart.`
239 : continuing === 0
240 ? `${threads} will be interrupted.`
241 : `${threads} will be interrupted. ${continuing} can resume after the restart.`}
242 </p>
243 ) : null}
244 <AlertDialogClose ref={cancelRef} render={<Button variant="outline" disabled={busy} />}>
245 Cancel
246 </AlertDialogClose>
247 <Button disabled={disabledReason !== null || busy} onClick={onConfirm}>
248 {confirmLabel}
249 </Button>
250 </AlertDialogFooter>
251 </>
252 );
254​
255function RunningThreads({ environmentId }: { readonly environmentId: EnvironmentId }) {
256 const count = useAtomValue(runningThreadCountAtom(environmentId));
257 return count > 0 ? <span className="whitespace-nowrap text-warning">{count} running</span> : null;