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

ServerUpdateAction.tsx

157 lines4.8 KB

1import type { EnvironmentId, ServerSelfUpdateCapability } from "@t3tools/contracts";
2import type { ServerUpdateStage, ServerUpdateState } from "@t3tools/client-runtime/state/server";
3import {
4 isAtomCommandInterrupted,
5 squashAtomCommandFailure,
6} from "@t3tools/client-runtime/state/runtime";
7​
8import { useCopyToClipboard } from "~/hooks/useCopyToClipboard";
9import { serverEnvironment } from "~/state/server";
10import { useAtomCommand } from "~/state/use-atom-command";
11import { manualServerUpdateCommand } from "~/versionSkew";
12import { Button } from "./ui/button";
13import { toastManager } from "./ui/toast";
14​
15// The wire "installing" stage is a sub-second launcher handoff, so the UI
16// folds it into the download phase; everything after the handoff is the
17// restart the user is actually waiting through.
18const UPDATE_STAGE_LABELS: Record<ServerUpdateStage, string> = {
19 downloading: "Downloading…",
20 installing: "Downloading…",
21 resuming: "Restarting…",
22};
23const pendingUpdateEnvironmentIds = new Set<EnvironmentId>();
24​
25export function serverUpdateStageLabel(stage: ServerUpdateStage): string {
26 return UPDATE_STAGE_LABELS[stage];
28​
29function updateFailureMessage(error: unknown): string {
30 return error instanceof Error ? error.message : "Server update failed.";
32​
33/**
34 * One-row status for an in-flight server update: "Downloading…" then
35 * "Restarting…". The update is a wait, not a warning: a single pulsing dot
36 * and label, no step rail, no versions. Failure turns the row red with the
37 * rollback reason.
38 */
39export function ServerUpdateProgress({
40 state,
41}: {
42 readonly state: Exclude<ServerUpdateState, { status: "idle" }>;
43}) {
44 if (state.status === "failed") {
45 return (
46 <div className="mt-1 flex min-w-0 items-center gap-2 text-xs text-destructive" role="alert">
47 <span className="size-1.5 shrink-0 rounded-full bg-destructive" aria-hidden="true" />
48 <span className="min-w-0 truncate" title={state.message}>
49 {state.message}
50 </span>
51 </div>
52 );
53 }
54 return (
55 <div className="mt-1 flex items-center gap-2 text-xs font-medium text-foreground">
56 <span
57 className="size-1.5 shrink-0 animate-status-pulse rounded-full bg-foreground"
58 aria-hidden="true"
59 />
60 <span>{serverUpdateStageLabel(state.stage)}</span>
61 </div>
62 );
64​
65/**
66 * Offers the update path advertised by a version-skewed server. Self-updates
67 * delegate their full lifecycle to client-runtime so this component can
68 * unmount during reconnect without losing operation state.
69 */
70export function ServerUpdateAction({
71 environmentId,
72 serverLabel,
73 selfUpdate,
74 targetVersion,
75 label = "Update",
76}: {
77 readonly environmentId: EnvironmentId;
78 readonly serverLabel: string;
79 readonly selfUpdate: ServerSelfUpdateCapability | null;
80 readonly targetVersion: string;
81 readonly label?: string;
82}) {
83 const updateServer = useAtomCommand(serverEnvironment.updateServer, {
84 reportFailure: false,
85 });
86 const { copyToClipboard } = useCopyToClipboard<{ command: string }>({
87 target: "update command",
88 onCopy: ({ command }) => {
89 toastManager.add({
90 type: "success",
91 title: "Update command copied",
92 description: `Run \`${command}\` on ${serverLabel} to update it.`,
93 });
94 },
95 onError: (error) => {
96 toastManager.add({
97 type: "error",
98 title: "Could not copy update command",
99 description: error.message,
100 });
101 },
102 });
103​
104 const handleUpdate = async () => {
105 if (pendingUpdateEnvironmentIds.has(environmentId)) {
106 return;
107 }
108 pendingUpdateEnvironmentIds.add(environmentId);
109 try {
110 const result = await updateServer({
111 environmentId,
112 input: { targetVersion },
113 });
114 if (result._tag === "Failure") {
115 if (isAtomCommandInterrupted(result)) {
116 return;
117 }
118 toastManager.add({
119 type: "error",
120 title: "Server update failed",
121 description: updateFailureMessage(squashAtomCommandFailure(result)),
122 });
123 return;
124 }
125 toastManager.add({
126 type: "success",
127 title: `${serverLabel} updated`,
128 description: `Reconnected on t3@${result.value.targetVersion}.`,
129 });
130 } finally {
131 pendingUpdateEnvironmentIds.delete(environmentId);
132 }
133 };
134​
135 if (selfUpdate === "desktop-managed") {
136 return (
137 <span className="text-muted-foreground text-xs">
138 Update the desktop app on that machine to update this server.
139 </span>
140 );
141 }
142​
143 if (selfUpdate === null) {
144 const command = manualServerUpdateCommand(targetVersion);
145 return (
146 <Button size="xs" variant="outline" onClick={() => copyToClipboard(command, { command })}>
147 Copy update command
148 </Button>
149 );
150 }
151​
152 return (
153 <Button size="xs" onClick={() => void handleUpdate()}>
154 {label}
155 </Button>
156 );