Open History to see the last commit
1import type { EnvironmentId, ServerConfig, ServerSelfUpdateCapability } from "@t3tools/contracts";
2import * as Schema from "effect/Schema";
3
4import { APP_VERSION } from "./branding";
5import { getLocalStorageItem, setLocalStorageItem } from "./hooks/useLocalStorage";
6
7export interface VersionMismatch {
8 readonly clientVersion: string;
9 readonly serverVersion: string;
10 readonly hint: string;
11}
12
13export const VERSION_MISMATCH_DISMISSALS_STORAGE_KEY = "t3code:version-mismatch-dismissals:v1";
14
15const VersionMismatchDismissalsSchema = Schema.Struct({
16 keys: Schema.Array(Schema.String),
17});
18
19type VersionMismatchDismissals = typeof VersionMismatchDismissalsSchema.Type;
20
21function normalizeVersion(version: string | null | undefined): string | null {
22 const trimmed = version?.trim();
23 return trimmed && trimmed.length > 0 ? trimmed : null;
24}
25
26export function resolveVersionMismatch(
27 serverVersion: string | null | undefined,
28): VersionMismatch | null {
29 const normalizedClientVersion = normalizeVersion(APP_VERSION);
30 const normalizedServerVersion = normalizeVersion(serverVersion);
31 if (
32 !normalizedClientVersion ||
33 !normalizedServerVersion ||
34 normalizedClientVersion === normalizedServerVersion
35 ) {
36 return null;
37 }
38
39 return {
40 clientVersion: normalizedClientVersion,
41 serverVersion: normalizedServerVersion,
42 hint: "Version mismatch. Try syncing the client and server to the same T3 Code version.",
43 };
44}
45
46export function resolveServerConfigVersionMismatch(
47 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
48): VersionMismatch | null {
49 return resolveVersionMismatch(serverConfig?.environment.serverVersion);
50}
51
52/** The update path the connected server offers, or null when it only
53 supports a manual relaunch (older servers, dev checkouts, Windows). */
54export function resolveServerSelfUpdateCapability(
55 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
56): ServerSelfUpdateCapability | null {
57 return serverConfig?.environment.capabilities.serverSelfUpdate ?? null;
58}
59
60/** The command to hand users whose server cannot update itself. */
61export function manualServerUpdateCommand(targetVersion: string): string {
62 return `npx t3@${targetVersion}`;
63}
64
65/** One sentence telling the user how to resolve version skew for a server,
66 matched to the update path it offers. */
67export function serverUpdateGuidance(
68 capability: ServerSelfUpdateCapability | null,
69 serverLabel: string,
70): string {
71 switch (capability) {
72 case "boot-service":
73 case "respawn":
74 return `Update the ${serverLabel} so they stay in sync.`;
75 case "desktop-managed":
76 return `Update the desktop app that runs the ${serverLabel}.`;
77 default:
78 return `Relaunch the ${serverLabel} with the copied command to sync them.`;
79 }
80}
81
82export function buildVersionMismatchDismissalKey(
83 environmentId: EnvironmentId,
84 mismatch: Pick<VersionMismatch, "clientVersion" | "serverVersion">,
85): string {
86 return `${environmentId}:${mismatch.clientVersion}:${mismatch.serverVersion}`;
87}
88
89function readVersionMismatchDismissals(): VersionMismatchDismissals {
90 try {
91 return (
92 getLocalStorageItem(
93 VERSION_MISMATCH_DISMISSALS_STORAGE_KEY,
94 VersionMismatchDismissalsSchema,
95 ) ?? { keys: [] }
96 );
97 } catch (error) {
98 console.error("Could not read version-mismatch dismissals.", error);
99 return { keys: [] };
100 }
101}
102
103function writeVersionMismatchDismissals(document: VersionMismatchDismissals): void {
104 try {
105 setLocalStorageItem(
106 VERSION_MISMATCH_DISMISSALS_STORAGE_KEY,
107 document,
108 VersionMismatchDismissalsSchema,
109 );
110 } catch (error) {
111 console.error("Could not persist version-mismatch dismissals.", error);
112 }
113}
114
115export function isVersionMismatchDismissed(dismissalKey: string | null | undefined): boolean {
116 if (!dismissalKey) {
117 return false;
118 }
119 return readVersionMismatchDismissals().keys.includes(dismissalKey);
120}
121
122export function dismissVersionMismatch(dismissalKey: string | null | undefined): void {
123 if (!dismissalKey) {
124 return;
125 }
126 const document = readVersionMismatchDismissals();
127 if (document.keys.includes(dismissalKey)) {
128 return;
129 }
130 writeVersionMismatchDismissals({
131 keys: [...document.keys, dismissalKey],
132 });
133}
134
135export function appendVersionMismatchHint(
136 message: string | null | undefined,
137 mismatch: VersionMismatch | null | undefined,
138): string | null {
139 const normalizedMessage = normalizeVersion(message);
140 if (!normalizedMessage) {
141 return mismatch?.hint ?? null;
142 }
143 if (!mismatch) {
144 return normalizedMessage;
145 }
146 return `${normalizedMessage} Hint: ${mismatch.hint}`;
147}