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

SidebarUpdateCapsule.tsx

181 lines6.8 KB

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