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

ServerUpdateAction.tsx

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