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

UpdateEverythingDialog.tsx

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