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

ServerUpdateAction.tsx

204 lines6.5 KB

1import type { EnvironmentId, ServerSelfUpdateCapability } from "@t3tools/contracts";
2import type { ServerUpdateState } from "@t3tools/client-runtime/state/server";
3import {
4 isAtomCommandInterrupted,
5 squashAtomCommandFailure,
6} from "@t3tools/client-runtime/state/runtime";
7import { CheckIcon } from "lucide-react";
8​
9import { useCopyToClipboard } from "~/hooks/useCopyToClipboard";
10import { cn } from "~/lib/utils";
11import { serverEnvironment } from "~/state/server";
12import { useAtomCommand } from "~/state/use-atom-command";
13import { manualServerUpdateCommand } from "~/versionSkew";
14import { Button } from "./ui/button";
15import { toastManager } from "./ui/toast";
16​
17const UPDATE_STEPS = [
18 { stage: "downloading", label: "Download", activeLabel: "Downloading" },
19 { stage: "installing", label: "Install", activeLabel: "Installing" },
20 { stage: "resuming", label: "Resume", activeLabel: "Resuming" },
21] as const;
22const pendingUpdateEnvironmentIds = new Set<EnvironmentId>();
23​
24function updateFailureMessage(error: unknown): string {
25 return error instanceof Error ? error.message : "Server update failed.";
27​
28/**
29 * Monochrome step rail for an in-flight server update. The update is a
30 * wait, not a warning: done steps recede to gray, the active step is the
31 * only bright element and pulses. Failure keeps the rail but surfaces the
32 * error below it.
33 */
34export function ServerUpdateProgress({
35 fromVersion,
36 state,
37}: {
38 readonly fromVersion: string;
39 readonly state: Exclude<ServerUpdateState, { status: "idle" }>;
40}) {
41 const currentIndex = UPDATE_STEPS.findIndex(({ stage }) => stage === state.stage);
42​
43 return (
44 <div className="mt-1 min-w-0 space-y-2.5">
45 <p className="text-xs text-muted-foreground/70">
46 {fromVersion} <span aria-hidden="true">→</span> {state.targetVersion}
47 </p>
48 <ol className="flex min-w-0 items-center" aria-label="Server update progress">
49 {UPDATE_STEPS.map((step, index) => {
50 const complete = index < currentIndex;
51 const current = index === currentIndex;
52 const failed = current && state.status === "failed";
53 const running = current && state.status === "running";
54 return (
55 <li
56 key={step.stage}
57 className={cn(
58 "flex min-w-0 items-center text-xs font-medium",
59 index < UPDATE_STEPS.length - 1 ? "flex-1" : null,
60 complete
61 ? "text-muted-foreground"
62 : failed
63 ? "text-destructive"
64 : running
65 ? "animate-status-pulse text-foreground"
66 : "text-muted-foreground/50",
67 )}
68 aria-current={running ? "step" : undefined}
69 >
70 {complete ? (
71 <CheckIcon
72 className="size-3.5 shrink-0 opacity-70"
73 strokeWidth={2.5}
74 aria-hidden="true"
75 />
76 ) : (
77 <span
78 className={cn(
79 "size-1.5 shrink-0 rounded-full",
80 failed
81 ? "bg-destructive"
82 : running
83 ? "bg-foreground"
84 : "border border-muted-foreground/40",
85 )}
86 aria-hidden="true"
87 />
88 )}
89 <span className="ml-1.5 truncate">{running ? step.activeLabel : step.label}</span>
90 {index < UPDATE_STEPS.length - 1 ? (
91 <span
92 className={cn(
93 "mx-2 h-px min-w-3 flex-1",
94 complete ? "bg-muted-foreground/40" : "bg-border",
95 )}
96 aria-hidden="true"
97 />
98 ) : null}
99 </li>
100 );
101 })}
102 </ol>
103 {state.status === "failed" ? (
104 <p className="text-xs text-destructive" role="alert">
105 {state.message}
106 </p>
107 ) : null}
108 </div>
109 );
111​
112/**
113 * Offers the update path advertised by a version-skewed server. Self-updates
114 * delegate their full lifecycle to client-runtime so this component can
115 * unmount during reconnect without losing operation state.
116 */
117export function ServerUpdateAction({
118 environmentId,
119 serverLabel,
120 selfUpdate,
121 targetVersion,
122 label = "Update server",
123}: {
124 readonly environmentId: EnvironmentId;
125 readonly serverLabel: string;
126 readonly selfUpdate: ServerSelfUpdateCapability | null;
127 readonly targetVersion: string;
128 readonly label?: string;
129}) {
130 const updateServer = useAtomCommand(serverEnvironment.updateServer, {
131 reportFailure: false,
132 });
133 const { copyToClipboard } = useCopyToClipboard<{ command: string }>({
134 target: "update command",
135 onCopy: ({ command }) => {
136 toastManager.add({
137 type: "success",
138 title: "Update command copied",
139 description: `Run \`${command}\` on ${serverLabel} to update it.`,
140 });
141 },
142 onError: (error) => {
143 toastManager.add({
144 type: "error",
145 title: "Could not copy update command",
146 description: error.message,
147 });
148 },
149 });
150​
151 const handleUpdate = async () => {
152 if (pendingUpdateEnvironmentIds.has(environmentId)) {
153 return;
154 }
155 pendingUpdateEnvironmentIds.add(environmentId);
156 try {
157 const result = await updateServer({
158 environmentId,
159 input: { targetVersion },
160 });
161 if (result._tag === "Failure") {
162 if (isAtomCommandInterrupted(result)) {
163 return;
164 }
165 toastManager.add({
166 type: "error",
167 title: "Server update failed",
168 description: updateFailureMessage(squashAtomCommandFailure(result)),
169 });
170 return;
171 }
172 toastManager.add({
173 type: "success",
174 title: `${serverLabel} updated`,
175 description: `Reconnected on t3@${result.value.targetVersion}.`,
176 });
177 } finally {
178 pendingUpdateEnvironmentIds.delete(environmentId);
179 }
180 };
181​
182 if (selfUpdate === "desktop-managed") {
183 return (
184 <span className="text-muted-foreground text-xs">
185 Update the desktop app on that machine to update this server.
186 </span>
187 );
188 }
189​
190 if (selfUpdate === null) {
191 const command = manualServerUpdateCommand(targetVersion);
192 return (
193 <Button size="xs" variant="outline" onClick={() => copyToClipboard(command, { command })}>
194 Copy update command
195 </Button>
196 );
197 }
198​
199 return (
200 <Button size="xs" onClick={() => void handleUpdate()}>
201 {label}
202 </Button>
203 );