Open History to see the last commit
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];
28}
29
30function updateFailureMessage(error: unknown): string {
31 return error instanceof Error ? error.message : "Server update failed.";
32}
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 if (state.status === "pending") {
59 return (
60 <div className="mt-1 flex items-center gap-2 text-xs text-muted-foreground">
61 <span className="size-1.5 shrink-0 rounded-full border border-current" aria-hidden="true" />
62 <span>
63 {state.reason === "active-work"
64 ? "Waiting for active work to finish…"
65 : "Checking activity before updating…"}
66 </span>
67 </div>
68 );
69 }
70 return (
71 <div className="mt-1 flex items-center gap-2 text-xs font-medium text-foreground">
72 <span
73 className="size-1.5 shrink-0 animate-status-pulse rounded-full bg-foreground"
74 aria-hidden="true"
75 />
76 <span>{serverUpdateStageLabel(state.stage)}</span>
77 </div>
78 );
79}
80
81/**
82 * Offers the update path advertised by a version-skewed server. Self-updates
83 * delegate their full lifecycle to client-runtime so this component can
84 * unmount during reconnect without losing operation state.
85 */
86export function ServerUpdateAction({
87 environmentId,
88 serverLabel,
89 selfUpdate,
90 targetVersion,
91 label = "Update",
92}: {
93 readonly environmentId: EnvironmentId;
94 readonly serverLabel: string;
95 readonly selfUpdate: ServerSelfUpdateCapability | null;
96 readonly targetVersion: string;
97 readonly label?: string;
98}) {
99 const updateServer = useAtomCommand(serverEnvironment.updateServer, {
100 reportFailure: false,
101 });
102 const { copyToClipboard } = useCopyToClipboard<{ command: string }>({
103 target: "update command",
104 onCopy: ({ command }) => {
105 toastManager.add({
106 type: "success",
107 title: "Update command copied",
108 description: `Run \`${command}\` on ${serverLabel} to update it.`,
109 });
110 },
111 onError: (error) => {
112 toastManager.add({
113 type: "error",
114 title: "Could not copy update command",
115 description: error.message,
116 });
117 },
118 });
119
120 const handleUpdate = async () => {
121 if (pendingUpdateEnvironmentIds.has(environmentId)) {
122 return;
123 }
124 pendingUpdateEnvironmentIds.add(environmentId);
125 try {
126 const result = await updateServer({
127 environmentId,
128 input: { targetVersion },
129 });
130 if (result._tag === "Failure") {
131 if (isAtomCommandInterrupted(result)) {
132 return;
133 }
134 toastManager.add({
135 type: "error",
136 title: "Server update failed",
137 description: updateFailureMessage(squashAtomCommandFailure(result)),
138 });
139 return;
140 }
141 toastManager.add({
142 type: "success",
143 title: `${serverLabel} updated`,
144 description: `Reconnected on t3@${result.value.targetVersion}.`,
145 });
146 } finally {
147 pendingUpdateEnvironmentIds.delete(environmentId);
148 }
149 };
150
151 if (selfUpdate === "desktop-managed") {
152 return (
153 <span className="text-muted-foreground text-xs">
154 Update the desktop app on that machine to update this server.
155 </span>
156 );
157 }
158
159 if (selfUpdate === null) {
160 const command = manualServerUpdateCommand(targetVersion);
161 return (
162 <Button size="xs" variant="outline" onClick={() => copyToClipboard(command, { command })}>
163 Copy update command
164 </Button>
165 );
166 }
167
168 return (
169 <Button size="xs" onClick={() => void handleUpdate()}>
170 {label}
171 </Button>
172 );
173}