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