Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { Atom } from "effect/reactivity";
3import { type ReactNode, useCallback, useMemo, useSyncExternalStore } from "react";
4
5import { cn } from "~/lib/utils";
6import { serverEnvironment } from "~/state/server";
7import { useServerUpdate } from "../ServerUpdateAction";
8import { Button } from "../ui/button";
9import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
10import {
11 describeUpdateCapsule,
12 formatUpdateElapsed,
13 RUN_STATUS_LABELS,
14 resolveUpdateRunMachineStatus,
15 type UpdateEverythingRun,
16 type UpdateRunLocalState,
17 type UpdateRunMachineView,
18} from "./updateEverything.logic";
19import {
20 dismissSettledUpdateRunMachines,
21 postponeUpdateRestart,
22 retryFailedUpdateRunServers,
23} from "./updateEverythingRun";
24
25function useNow(intervalMs: number | null): number {
26 const step = intervalMs ?? 60_000;
27 const subscribe = useCallback(
28 (onChange: () => void) => {
29 if (intervalMs === null) return () => {};
30 const timer = setInterval(onChange, intervalMs);
31 return () => clearInterval(timer);
32 },
33 [intervalMs],
34 );
35 return useSyncExternalStore(subscribe, () => Math.floor(Date.now() / step) * step);
36}
37
38function useUpdateRunMachineViews(run: UpdateEverythingRun): ReadonlyArray<UpdateRunMachineView> {
39 const viewsAtom = useMemo(
40 () =>
41 Atom.make((get) =>
42 run.machines.map((machine) => ({
43 machine,
44 status: resolveUpdateRunMachineStatus(
45 machine,
46 run.phase,
47 get(serverEnvironment.updateStateAtom(machine.environmentId)),
48 ),
49 })),
50 ),
51 [run],
52 );
53 return useAtomValue(viewsAtom);
54}
55
56function localStatusLabel(run: UpdateEverythingRun, local: UpdateRunLocalState, now: number) {
57 if (run.phase === "countdown" && run.restartAt !== null) {
58 return `restarts in ${Math.max(0, Math.ceil((run.restartAt - now) / 1000))}s`;
59 }
60 if (run.phase === "installing") return "restarting";
61 if (local?.status === "downloading") {
62 return `downloading${local.percent === null ? "" : ` ${Math.floor(local.percent)}%`}`;
63 }
64 return "restarts after the servers";
65}
66
67export function SidebarUpdateCapsule({
68 run,
69 local,
70 icon,
71 onRestart,
72}: {
73 readonly run: UpdateEverythingRun;
74 readonly local: UpdateRunLocalState;
75 readonly icon: ReactNode;
76 readonly onRestart?: () => Promise<void>;
77}) {
78 const machines = useUpdateRunMachineViews(run);
79 const hasActiveMachine = machines.some(
80 ({ status }) => status !== "done" && status !== "failed" && status !== "waiting",
81 );
82 const now = useNow(run.phase === "countdown" ? 1_000 : hasActiveMachine ? 30_000 : null);
83 const view = describeUpdateCapsule(run, machines, local, now);
84 const updateServer = useServerUpdate();
85 const failed = machines.some(({ status }) => status === "failed");
86 const settled = !hasActiveMachine && run.localVersion === null;
87
88 const actionLabel =
89 view.action === "later" ? "Later" : view.action === "retry" ? "Retry" : "Dismiss";
90 const runAction = () => {
91 if (view.action === "later") postponeUpdateRestart();
92 else if (view.action === "retry") retryFailedUpdateRunServers(updateServer);
93 else dismissSettledUpdateRunMachines();
94 };
95
96 return (
97 <Popover>
98 <div
99 className={cn(
100 "relative flex h-8 min-w-0 flex-1 items-center rounded-full bg-sidebar-control-surface text-sidebar-foreground",
101 view.tone === "slow" && "ring-1 ring-warning/40 ring-inset",
102 view.tone === "failed" && "ring-1 ring-destructive/40 ring-inset",
103 )}
104 >
105 <PopoverTrigger
106 render={
107 <button
108 type="button"
109 aria-label={
110 view.slow ? `${view.label} ${view.slow.status} ${view.slow.elapsed}` : view.label
111 }
112 className="absolute inset-0 cursor-pointer rounded-full outline-hidden ring-ring hover:bg-sidebar-row-hover focus-visible:ring-2 focus-visible:ring-inset"
113 />
114 }
115 />
116 <span className="pointer-events-none relative min-w-0 flex-1 truncate pl-3 text-xs">
117 <span className={cn(view.tone === "failed" && "text-destructive")}>{view.label}</span>
118 {view.slow ? (
119 <span className="text-warning tabular-nums"> {view.slow.elapsed}</span>
120 ) : null}
121 </span>
122 {view.action !== null ? (
123 <button
124 type="button"
125 className="relative shrink-0 cursor-pointer pr-3 pl-1.5 text-xs underline decoration-muted-foreground/60 underline-offset-3 hover:decoration-current"
126 onClick={runAction}
127 >
128 {actionLabel}
129 </button>
130 ) : (
131 <span className="pointer-events-none relative grid size-8 shrink-0 place-items-center">
132 {icon}
133 </span>
134 )}
135 </div>
136 <PopoverPopup side="top" align="end" padding="compact" width="sm">
137 <div className="flex flex-col gap-1 text-xs">
138 {run.localVersion !== null ? (
139 <div className="flex items-center justify-between gap-3">
140 <span>This app</span>
141 <span className="text-muted-foreground tabular-nums">
142 {localStatusLabel(run, local, now)}
143 </span>
144 </div>
145 ) : onRestart ? (
146 <div className="flex items-center justify-between gap-3">
147 <span>This app</span>
148 <Button size="xs" variant="outline" onClick={() => void onRestart()}>
149 Restart to update
150 </Button>
151 </div>
152 ) : null}
153 {machines.map(({ machine, status }) => (
154 <div key={machine.environmentId} className="flex min-w-0 flex-col">
155 <div className="flex items-center justify-between gap-3">
156 <span className="truncate">{machine.label}</span>
157 <span
158 className={cn(
159 "shrink-0 tabular-nums",
160 status === "failed" ? "text-destructive" : "text-muted-foreground",
161 )}
162 >
163 {RUN_STATUS_LABELS[status]}
164 {status !== "done" && status !== "failed" && status !== "waiting"
165 ? ` ${formatUpdateElapsed(now - machine.startedAt)}`
166 : ""}
167 </span>
168 </div>
169 {machine.failure !== null ? (
170 <span className="break-words text-muted-foreground">{machine.failure}</span>
171 ) : null}
172 </div>
173 ))}
174 {settled && failed ? (
175 <div className="mt-1 flex justify-end gap-1">
176 {view.action === "retry" ? (
177 <Button size="xs" variant="outline" onClick={runAction}>
178 Retry
179 </Button>
180 ) : null}
181 <Button size="xs" variant="ghost" onClick={dismissSettledUpdateRunMachines}>
182 Dismiss
183 </Button>
184 </div>
185 ) : null}
186 </div>
187 </PopoverPopup>
188 </Popover>
189 );
190}