Open History to see the last commit
1import type { EnvironmentId, ServerConfig } 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
52export function buildVersionMismatchDismissalKey(
53 environmentId: EnvironmentId,
54 mismatch: Pick<VersionMismatch, "clientVersion" | "serverVersion">,
55): string {
56 return `${environmentId}:${mismatch.clientVersion}:${mismatch.serverVersion}`;
57}
58
59function readVersionMismatchDismissals(): VersionMismatchDismissals {
60 try {
61 return (
62 getLocalStorageItem(
63 VERSION_MISMATCH_DISMISSALS_STORAGE_KEY,
64 VersionMismatchDismissalsSchema,
65 ) ?? { keys: [] }
66 );
67 } catch {
68 return { keys: [] };
69 }
70}
71
72function writeVersionMismatchDismissals(document: VersionMismatchDismissals): void {
73 try {
74 setLocalStorageItem(
75 VERSION_MISMATCH_DISMISSALS_STORAGE_KEY,
76 document,
77 VersionMismatchDismissalsSchema,
78 );
79 } catch {
80 // Dismissal state is best-effort UI state; a storage failure should not block the banner.
81 }
82}
83
84export function isVersionMismatchDismissed(dismissalKey: string | null | undefined): boolean {
85 if (!dismissalKey) {
86 return false;
87 }
88 return readVersionMismatchDismissals().keys.includes(dismissalKey);
89}
90
91export function dismissVersionMismatch(dismissalKey: string | null | undefined): void {
92 if (!dismissalKey) {
93 return;
94 }
95 const document = readVersionMismatchDismissals();
96 if (document.keys.includes(dismissalKey)) {
97 return;
98 }
99 writeVersionMismatchDismissals({
100 keys: [...document.keys, dismissalKey],
101 });
102}
103
104export function appendVersionMismatchHint(
105 message: string | null | undefined,
106 mismatch: VersionMismatch | null | undefined,
107): string | null {
108 const normalizedMessage = normalizeVersion(message);
109 if (!normalizedMessage) {
110 return mismatch?.hint ?? null;
111 }
112 if (!mismatch) {
113 return normalizedMessage;
114 }
115 return `${normalizedMessage} Hint: ${mismatch.hint}`;
116}