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