Open History to see the last commit
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}
59
60type UpdateEverythingDialogContentProps = Omit<
61 UpdateEverythingDialogProps,
62 "open" | "onOpenChange"
63> & {
64 readonly cancelRef: RefObject<HTMLButtonElement | null>;
65};
66
67export function UpdateEverythingDialog({
68 open,
69 onOpenChange,
70 ...content
71}: UpdateEverythingDialogProps) {
72 const cancelRef = useRef<HTMLButtonElement>(null);
73 return (
74 <AlertDialog open={open} onOpenChange={onOpenChange}>
75 {/* Refocusing the trigger after a pointer close shows its focus ring and opens its tooltip. */}
76 <AlertDialogPopup
77 className="sm:max-w-xl"
78 initialFocus={cancelRef}
79 finalFocus={(closeType) => closeType === "keyboard"}
80 >
81 <UpdateEverythingDialogContent {...content} cancelRef={cancelRef} />
82 </AlertDialogPopup>
83 </AlertDialog>
84 );
85}
86
87const APP_LABELS = {
88 "this-app": "This app",
89 server: "Server",
90 "desktop-app": "Desktop app",
91} as const;
92
93function UpdateEverythingDialogContent({
94 version,
95 providerCount,
96 rows,
97 restarts,
98 onConfirm,
99 cancelRef,
100 title,
101 confirmLabel = "Update",
102 notes = [],
103 disabledReason = null,
104 busy = false,
105}: UpdateEverythingDialogContentProps) {
106 const stopped = useAtomValue(
107 runningThreadCountAtom(
108 restarts
109 .filter((machine) => !machine.continuesThreads)
110 .map((machine) => machine.environmentId)
111 .join("\n"),
112 ),
113 );
114 const continuing = useAtomValue(
115 runningThreadCountAtom(
116 restarts
117 .filter((machine) => machine.continuesThreads)
118 .map((machine) => machine.environmentId)
119 .join("\n"),
120 ),
121 );
122 const running = stopped + continuing;
123 const threads = `${running} running ${running === 1 ? "thread" : "threads"}`;
124 const restarting = new Set(restarts.map((machine) => machine.environmentId));
125 const updating = rows.filter((row) => row.app !== null || row.providers.length > 0);
126 const skipped = rows.filter((row) => row.skipped !== null);
127
128 return (
129 <>
130 <AlertDialogHeader className="shrink-0">
131 <AlertDialogTitle>
132 {title ?? `Update ${updating.length} ${updating.length === 1 ? "machine" : "machines"}?`}
133 </AlertDialogTitle>
134 <AlertDialogDescription>
135 {version !== null ? (
136 <>
137 T3 Code <span className="font-mono text-xs">{version}</span>
138 </>
139 ) : null}
140 {version !== null && providerCount > 0 ? " · " : null}
141 {providerCount > 0
142 ? `${providerCount} provider ${providerCount === 1 ? "update" : "updates"}`
143 : null}
144 </AlertDialogDescription>
145 </AlertDialogHeader>
146 <ScrollArea scrollFade>
147 <div className="flex flex-col gap-3 px-6 pb-4">
148 {notes.map((note) => (
149 <p key={note} className="text-muted-foreground text-sm">
150 {note}
151 </p>
152 ))}
153 {/* Machine names truncate first. Provider lists wrap between names, never inside one. */}
154 <div className="grid grid-cols-[minmax(4rem,max-content)_max-content_minmax(min-content,1fr)_max-content] gap-x-4 text-sm">
155 <div className="col-span-full grid grid-cols-subgrid border-b pb-1.5 text-muted-foreground text-xs">
156 <span>Machine</span>
157 <span>T3 Code</span>
158 <span>Providers</span>
159 <span />
160 </div>
161 {updating.map((row) => (
162 <div
163 key={row.environmentId ?? "this-app"}
164 className="col-span-full grid grid-cols-subgrid border-b border-border/50 py-1.5 last:border-b-0"
165 >
166 <span className="max-w-32 min-w-0">
167 <MiddleTruncate value={row.label} />
168 </span>
169 <span>
170 {row.app !== null ? APP_LABELS[row.app] : null}
171 {row.app === null && row.skipped !== null ? (
172 <span className="text-muted-foreground">Skipped</span>
173 ) : null}
174 {row.app === "this-app" ? (
175 <span className="block text-muted-foreground text-xs">restarts last</span>
176 ) : null}
177 </span>
178 <span>
179 {row.providers.map((provider, index) => (
180 <Fragment key={provider}>
181 <span className="whitespace-nowrap">{provider}</span>
182 {index < row.providers.length - 1 ? ", " : null}
183 </Fragment>
184 ))}
185 </span>
186 <span className="text-right">
187 {row.environmentId !== null && restarting.has(row.environmentId) ? (
188 <RunningThreads environmentId={row.environmentId} />
189 ) : null}
190 </span>
191 </div>
192 ))}
193 </div>
194 {skipped.length > 0 ? (
195 <Collapsible>
196 <CollapsibleTrigger className="flex items-center gap-1 text-muted-foreground text-xs hover:text-foreground data-panel-open:[&_svg]:rotate-90">
197 <ChevronRightIcon className="size-3.5 transition-transform" aria-hidden />
198 {skipped.length} skipped
199 </CollapsibleTrigger>
200 <CollapsiblePanel>
201 <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">
202 {skipped.map((row) => (
203 <Fragment key={row.environmentId ?? "this-app"}>
204 <span className="max-w-32 min-w-0">
205 <MiddleTruncate value={row.label} />
206 </span>
207 <span className="wrap-anywhere">{row.skipped}</span>
208 </Fragment>
209 ))}
210 </div>
211 </CollapsiblePanel>
212 </Collapsible>
213 ) : null}
214 </div>
215 </ScrollArea>
216 <AlertDialogFooter>
217 {disabledReason !== null ? (
218 <p className="self-center text-sm max-sm:order-1 max-sm:text-center sm:mr-auto">
219 {disabledReason}
220 </p>
221 ) : running > 0 ? (
222 <p className="self-center text-muted-foreground text-sm max-sm:order-1 max-sm:text-center sm:mr-auto">
223 {stopped === 0
224 ? `${threads} will be interrupted and can resume after the restart.`
225 : continuing === 0
226 ? `${threads} will be interrupted.`
227 : `${threads} will be interrupted. ${continuing} can resume after the restart.`}
228 </p>
229 ) : null}
230 <AlertDialogClose ref={cancelRef} render={<Button variant="outline" disabled={busy} />}>
231 Cancel
232 </AlertDialogClose>
233 <Button disabled={disabledReason !== null || busy} onClick={onConfirm}>
234 {confirmLabel}
235 </Button>
236 </AlertDialogFooter>
237 </>
238 );
239}
240
241function RunningThreads({ environmentId }: { readonly environmentId: EnvironmentId }) {
242 const count = useAtomValue(runningThreadCountAtom(environmentId));
243 return count > 0 ? <span className="whitespace-nowrap text-warning">{count} running</span> : null;
244}