Open History to see the last commit
1import type { EnvironmentId, ServerConfig, ServerSelfUpdateCapability } from "@t3tools/contracts";
2import { compareSemverVersions, parseSemver } from "@t3tools/shared/semver";
3import * as Schema from "effect/Schema";
4
5import { APP_VERSION } from "./branding";
6import { getLocalStorageItem, setLocalStorageItem } from "./hooks/useLocalStorage";
7
8export interface VersionMismatch {
9 readonly clientVersion: string;
10 readonly serverVersion: string;
11 readonly hint: string;
12}
13
14export const VERSION_MISMATCH_DISMISSALS_STORAGE_KEY = "t3code:version-mismatch-dismissals:v1";
15
16const VersionMismatchDismissalsSchema = Schema.Struct({
17 keys: Schema.Array(Schema.String),
18});
19
20type VersionMismatchDismissals = typeof VersionMismatchDismissalsSchema.Type;
21
22function normalizeVersion(version: string | null | undefined): string | null {
23 const trimmed = version?.trim();
24 return trimmed && trimmed.length > 0 ? trimmed : null;
25}
26
27/** Core `major.minor.patch`, dropping any prerelease or build suffix. */
28function versionCore(version: string): string {
29 return version.replace(/[-+].*$/, "");
30}
31
32/**
33 * The skew a user can act on: the connected server runs an older T3 Code than
34 * this client, so the server is the side that needs updating.
35 *
36 * Versions compare as semver on their core `major.minor.patch` only. Nightlies
37 * are `<core>-nightly.<date>.<run>` builds of the release they precede, so a
38 * stable client on a nightly server (or the reverse, at the same core) shares a
39 * wire contract and is not skew. A server *ahead* of the client is not skew
40 * either: the client is the stale side, and every consumer of this result tells
41 * the user to update their server. Versions that do not parse as semver fall
42 * back to plain string inequality.
43 */
44export function resolveVersionMismatch(
45 serverVersion: string | null | undefined,
46): VersionMismatch | null {
47 const normalizedClientVersion = normalizeVersion(APP_VERSION);
48 const normalizedServerVersion = normalizeVersion(serverVersion);
49 if (!normalizedClientVersion || !normalizedServerVersion) {
50 return null;
51 }
52
53 const clientCore = versionCore(normalizedClientVersion);
54 const serverCore = versionCore(normalizedServerVersion);
55 const serverIsBehind =
56 parseSemver(clientCore) && parseSemver(serverCore)
57 ? compareSemverVersions(serverCore, clientCore) < 0
58 : normalizedServerVersion !== normalizedClientVersion;
59 if (!serverIsBehind) {
60 return null;
61 }
62
63 return {
64 clientVersion: normalizedClientVersion,
65 serverVersion: normalizedServerVersion,
66 hint: "Version mismatch. Try syncing the client and server to the same T3 Code version.",
67 };
68}
69
70export function resolveServerConfigVersionMismatch(
71 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
72): VersionMismatch | null {
73 return resolveVersionMismatch(serverConfig?.environment.serverVersion);
74}
75
76/** The update path the connected server offers, or null when it only
77 supports a manual relaunch (older servers, dev checkouts, Windows). */
78export function resolveServerSelfUpdateCapability(
79 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
80): ServerSelfUpdateCapability | null {
81 return serverConfig?.environment.capabilities.serverSelfUpdate ?? null;
82}
83
84/** The command to hand users whose server cannot update itself. */
85export function manualServerUpdateCommand(targetVersion: string): string {
86 return `npx t3@${targetVersion}`;
87}
88
89/** One sentence telling the user how to resolve version skew for a server,
90 matched to the update path it offers. */
91export function serverUpdateGuidance(
92 capability: ServerSelfUpdateCapability | null,
93 serverLabel: string,
94): string {
95 switch (capability) {
96 case "boot-service":
97 case "respawn":
98 return `Update the ${serverLabel} so they stay in sync.`;
99 case "desktop-managed":
100 return `Update the desktop app that runs the ${serverLabel}.`;
101 default:
102 return `Relaunch the ${serverLabel} with the copied command to sync them.`;
103 }
104}
105
106export function buildVersionMismatchDismissalKey(
107 environmentId: EnvironmentId,
108 mismatch: Pick<VersionMismatch, "clientVersion" | "serverVersion">,
109): string {
110 return `${environmentId}:${mismatch.clientVersion}:${mismatch.serverVersion}`;
111}
112
113function readVersionMismatchDismissals(): VersionMismatchDismissals {
114 try {
115 return (
116 getLocalStorageItem(
117 VERSION_MISMATCH_DISMISSALS_STORAGE_KEY,
118 VersionMismatchDismissalsSchema,
119 ) ?? { keys: [] }
120 );
121 } catch (error) {
122 console.error("Could not read version-mismatch dismissals.", error);
123 return { keys: [] };
124 }
125}
126
127function writeVersionMismatchDismissals(document: VersionMismatchDismissals): void {
128 try {
129 setLocalStorageItem(
130 VERSION_MISMATCH_DISMISSALS_STORAGE_KEY,
131 document,
132 VersionMismatchDismissalsSchema,
133 );
134 } catch (error) {
135 console.error("Could not persist version-mismatch dismissals.", error);
136 }
137}
138
139export function isVersionMismatchDismissed(dismissalKey: string | null | undefined): boolean {
140 if (!dismissalKey) {
141 return false;
142 }
143 return readVersionMismatchDismissals().keys.includes(dismissalKey);
144}
145
146export function dismissVersionMismatch(dismissalKey: string | null | undefined): void {
147 if (!dismissalKey) {
148 return;
149 }
150 const document = readVersionMismatchDismissals();
151 if (document.keys.includes(dismissalKey)) {
152 return;
153 }
154 writeVersionMismatchDismissals({
155 keys: [...document.keys, dismissalKey],
156 });
157}
158
159export function appendVersionMismatchHint(
160 message: string | null | undefined,
161 mismatch: VersionMismatch | null | undefined,
162): string | null {
163 const normalizedMessage = normalizeVersion(message);
164 if (!normalizedMessage) {
165 return mismatch?.hint ?? null;
166 }
167 if (!mismatch) {
168 return normalizedMessage;
169 }
170 return `${normalizedMessage} Hint: ${mismatch.hint}`;
171}