Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { EnvironmentId } from "@t3tools/contracts";
3import { Atom } from "effect/reactivity";
4
5import { environmentThreadShells } from "~/state/threads";
6import {
7 AlertDialog,
8 AlertDialogClose,
9 AlertDialogDescription,
10 AlertDialogFooter,
11 AlertDialogHeader,
12 AlertDialogPopup,
13 AlertDialogTitle,
14} from "../ui/alert-dialog";
15import { Button } from "../ui/button";
16import type { RestartingMachine, SkippedMachine } from "./updateEverything.logic";
17
18const isRunning = (status: string) =>
19 status === "preparing" || status === "starting" || status === "running";
20
21const runningThreadCountAtom = Atom.family((environmentIds: string) =>
22 Atom.make((get) =>
23 environmentIds
24 .split("\n")
25 .filter((id) => id !== "")
26 .reduce(
27 (count, id) =>
28 count +
29 get(environmentThreadShells.environmentThreadsAtom(EnvironmentId.make(id))).filter(
30 (thread) => isRunning(thread.status),
31 ).length,
32 0,
33 ),
34 ).pipe(Atom.withLabel(`update-everything-running-threads:${environmentIds}`)),
35);
36
37interface UpdateEverythingDialogProps {
38 readonly open: boolean;
39 readonly onOpenChange: (open: boolean) => void;
40 readonly summary: string;
41 readonly lines: ReadonlyArray<string>;
42 readonly restarts: ReadonlyArray<RestartingMachine>;
43 readonly skipped: ReadonlyArray<SkippedMachine>;
44 readonly onConfirm: () => void;
45}
46
47export function UpdateEverythingDialog({
48 open,
49 onOpenChange,
50 ...content
51}: UpdateEverythingDialogProps) {
52 return (
53 <AlertDialog open={open} onOpenChange={onOpenChange}>
54 <AlertDialogPopup>
55 {}
56 <UpdateEverythingDialogContent {...content} />
57 </AlertDialogPopup>
58 </AlertDialog>
59 );
60}
61
62function UpdateEverythingDialogContent({
63 summary,
64 lines,
65 restarts,
66 skipped,
67 onConfirm,
68}: Omit<UpdateEverythingDialogProps, "open" | "onOpenChange">) {
69 const stopped = useAtomValue(
70 runningThreadCountAtom(
71 restarts
72 .filter((machine) => !machine.continuesThreads)
73 .map((machine) => machine.environmentId)
74 .join("\n"),
75 ),
76 );
77 const continuing = useAtomValue(
78 runningThreadCountAtom(
79 restarts
80 .filter((machine) => machine.continuesThreads)
81 .map((machine) => machine.environmentId)
82 .join("\n"),
83 ),
84 );
85 const running = stopped + continuing;
86 const threads = `${running} running ${running === 1 ? "thread" : "threads"}`;
87
88 return (
89 <>
90 <AlertDialogHeader>
91 <AlertDialogTitle>{summary}?</AlertDialogTitle>
92 <AlertDialogDescription className="whitespace-pre-line">
93 {lines.join("\n")}
94 </AlertDialogDescription>
95 {running > 0 ? (
96 <p className="pt-2 text-sm">
97 {stopped === 0
98 ? `${threads} will be interrupted and can resume after the restart.`
99 : continuing === 0
100 ? `${threads} will be interrupted.`
101 : `${threads} will be interrupted. ${continuing} can resume after the restart.`}
102 </p>
103 ) : null}
104 {skipped.length > 0 ? (
105 <p className="whitespace-pre-line pt-2 text-sm">
106 {skipped.map((machine) => `Skips ${machine.label}: ${machine.reason}`).join("\n")}
107 </p>
108 ) : null}
109 </AlertDialogHeader>
110 <AlertDialogFooter>
111 <AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>
112 <Button onClick={onConfirm}>Update</Button>
113 </AlertDialogFooter>
114 </>
115 );
116}