Open History to see the last commit
1import type {
2 EnvironmentId,
3 ServerConfig,
4 ServerInstallation,
5 ServerSelfUpdateCapability,
6} from "@t3tools/contracts";
7import type { ServerUpdateState } from "@t3tools/client-runtime/state/server";
8import { compareSemverVersions, parseSemver } from "@t3tools/shared/semver";
9import * as Schema from "effect/Schema";
10
11import { APP_VERSION } from "./branding";
12import { getLocalStorageItem, setLocalStorageItem } from "./hooks/useLocalStorage";
13
14export interface VersionMismatch {
15 readonly clientVersion: string;
16 readonly serverVersion: string;
17 readonly hint: string;
18}
19
20const VERSION_MISMATCH_DISMISSALS_STORAGE_KEY = "t3code:version-mismatch-dismissals:v1";
21
22const dismissedServerUpdateFailures = new WeakSet<ServerUpdateState>();
23
24export function isServerUpdateFailureDismissed(state: ServerUpdateState): boolean {
25 return state.status === "failed" && dismissedServerUpdateFailures.has(state);
26}
27
28export function dismissServerUpdateFailure(state: ServerUpdateState): void {
29 if (state.status === "failed") dismissedServerUpdateFailures.add(state);
30}
31
32const VersionMismatchDismissalsSchema = Schema.Struct({
33 keys: Schema.Array(Schema.String),
34});
35
36type VersionMismatchDismissals = typeof VersionMismatchDismissalsSchema.Type;
37
38function normalizeVersion(version: string | null | undefined): string | null {
39 const trimmed = version?.trim();
40 return trimmed && trimmed.length > 0 ? trimmed : null;
41}
42
43function versionCore(version: string): string {
44 return version.replace(/[-+].*$/, "");
45}
46
47export function resolveVersionMismatch(
48 serverVersion: string | null | undefined,
49 clientVersion: string = APP_VERSION,
50): VersionMismatch | null {
51 const normalizedClientVersion = normalizeVersion(clientVersion);
52 const normalizedServerVersion = normalizeVersion(serverVersion);
53 if (!normalizedClientVersion || !normalizedServerVersion) {
54 return null;
55 }
56
57 const clientCore = versionCore(normalizedClientVersion);
58 const serverCore = versionCore(normalizedServerVersion);
59 const compareNightlyBuilds =
60 parseSemver(normalizedClientVersion)?.prerelease[0] === "nightly" &&
61 parseSemver(normalizedServerVersion)?.prerelease[0] === "nightly";
62 const serverIsBehind =
63 parseSemver(clientCore) && parseSemver(serverCore)
64 ? compareSemverVersions(
65 compareNightlyBuilds ? normalizedServerVersion : serverCore,
66 compareNightlyBuilds ? normalizedClientVersion : clientCore,
67 ) < 0
68 : normalizedServerVersion !== normalizedClientVersion;
69 if (!serverIsBehind) {
70 return null;
71 }
72
73 return {
74 clientVersion: normalizedClientVersion,
75 serverVersion: normalizedServerVersion,
76 hint: "Version mismatch. Try syncing the client and server to the same T3 Code version.",
77 };
78}
79
80export function resolveServerConfigVersionMismatch(
81 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
82): VersionMismatch | null {
83 return resolveVersionMismatch(serverConfig?.environment.serverVersion);
84}
85
86export function resolveServerSelfUpdateCapability(
87 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
88): ServerSelfUpdateCapability | null {
89 return serverConfig?.environment.capabilities.serverSelfUpdate ?? null;
90}
91
92export function supportsDesktopAppUpdate(
93 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
94): boolean {
95 return serverConfig?.environment.capabilities.desktopAppUpdate === true;
96}
97
98export function supportsServerUpdateThreadContinuation(
99 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
100): boolean {
101 return serverConfig?.environment.capabilities.serverUpdateThreadContinuation === true;
102}
103
104export function manualServerUpdateCommand(
105 targetVersion: string,
106 installation?: ServerInstallation,
107): string {
108 if (installation?.kind === "npm-global") {
109 const prefix = `'${installation.prefix.replaceAll("'", "'\\''")}'`;
110 return `npm install --global --prefix ${prefix} t3@${targetVersion}`;
111 }
112 const runner =
113 installation?.kind === "pnpm-dlx" ? "pnpm dlx" : installation?.kind === "bunx" ? "bunx" : "npx";
114 return `${runner} t3@${targetVersion}`;
115}
116
117export function buildVersionMismatchDismissalKey(
118 environmentId: EnvironmentId,
119 mismatch: Pick<VersionMismatch, "clientVersion" | "serverVersion">,
120): string {
121 return `${environmentId}:${mismatch.clientVersion}:${mismatch.serverVersion}`;
122}
123
124function readVersionMismatchDismissals(): VersionMismatchDismissals {
125 try {
126 return (
127 getLocalStorageItem(
128 VERSION_MISMATCH_DISMISSALS_STORAGE_KEY,
129 VersionMismatchDismissalsSchema,
130 ) ?? { keys: [] }
131 );
132 } catch (error) {
133 console.error("Could not read version-mismatch dismissals.", error);
134 return { keys: [] };
135 }
136}
137
138function writeVersionMismatchDismissals(document: VersionMismatchDismissals): void {
139 try {
140 setLocalStorageItem(
141 VERSION_MISMATCH_DISMISSALS_STORAGE_KEY,
142 document,
143 VersionMismatchDismissalsSchema,
144 );
145 } catch (error) {
146 console.error("Could not persist version-mismatch dismissals.", error);
147 }
148}
149
150export function isVersionMismatchDismissed(dismissalKey: string | null | undefined): boolean {
151 if (!dismissalKey) {
152 return false;
153 }
154 return readVersionMismatchDismissals().keys.includes(dismissalKey);
155}
156
157export function dismissVersionMismatch(dismissalKey: string | null | undefined): void {
158 if (!dismissalKey) {
159 return;
160 }
161 const document = readVersionMismatchDismissals();
162 if (document.keys.includes(dismissalKey)) {
163 return;
164 }
165 writeVersionMismatchDismissals({
166 keys: [...document.keys, dismissalKey],
167 });
168}
169
170export function appendVersionMismatchHint(
171 message: string | null | undefined,
172 mismatch: VersionMismatch | null | undefined,
173): string | null {
174 const normalizedMessage = normalizeVersion(message);
175 if (!normalizedMessage) {
176 return mismatch?.hint ?? null;
177 }
178 if (!mismatch) {
179 return normalizedMessage;
180 }
181 return `${normalizedMessage} Hint: ${mismatch.hint}`;
182}