@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

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