Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

versionSkew.ts

117 lines3.4 KB

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;
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;
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 };
45​
46export function resolveServerConfigVersionMismatch(
47 serverConfig: Pick<ServerConfig, "environment"> | null | undefined,
48): VersionMismatch | null {
49 return resolveVersionMismatch(serverConfig?.environment.serverVersion);
51​
52export function buildVersionMismatchDismissalKey(
53 environmentId: EnvironmentId,
54 mismatch: Pick<VersionMismatch, "clientVersion" | "serverVersion">,
55): string {
56 return `${environmentId}:${mismatch.clientVersion}:${mismatch.serverVersion}`;
58​
59function readVersionMismatchDismissals(): VersionMismatchDismissals {
60 try {
61 return (
62 getLocalStorageItem(
63 VERSION_MISMATCH_DISMISSALS_STORAGE_KEY,
64 VersionMismatchDismissalsSchema,
65 ) ?? { keys: [] }
66 );
67 } catch (error) {
68 console.error("Could not read version-mismatch dismissals.", error);
69 return { keys: [] };
70 }
72​
73function writeVersionMismatchDismissals(document: VersionMismatchDismissals): void {
74 try {
75 setLocalStorageItem(
76 VERSION_MISMATCH_DISMISSALS_STORAGE_KEY,
77 document,
78 VersionMismatchDismissalsSchema,
79 );
80 } catch (error) {
81 console.error("Could not persist version-mismatch dismissals.", error);
82 }
84​
85export function isVersionMismatchDismissed(dismissalKey: string | null | undefined): boolean {
86 if (!dismissalKey) {
87 return false;
88 }
89 return readVersionMismatchDismissals().keys.includes(dismissalKey);
91​
92export function dismissVersionMismatch(dismissalKey: string | null | undefined): void {
93 if (!dismissalKey) {
94 return;
95 }
96 const document = readVersionMismatchDismissals();
97 if (document.keys.includes(dismissalKey)) {
98 return;
99 }
100 writeVersionMismatchDismissals({
101 keys: [...document.keys, dismissalKey],
102 });
104​
105export function appendVersionMismatchHint(
106 message: string | null | undefined,
107 mismatch: VersionMismatch | null | undefined,
108): string | null {
109 const normalizedMessage = normalizeVersion(message);
110 if (!normalizedMessage) {
111 return mismatch?.hint ?? null;
112 }
113 if (!mismatch) {
114 return normalizedMessage;
115 }
116 return `${normalizedMessage} Hint: ${mismatch.hint}`;