local-storage: better versioning scheme until modules

This commit is contained in:
Hunter Miller 2021-10-20 18:48:42 -05:00
parent 22eae5ea06
commit a71724371c
6 changed files with 30 additions and 13 deletions

View File

@ -18,6 +18,7 @@
</head>
<body class="text-sm leading-6 font-sans text-gray-900 bg-white antialiased">
<div id="app"></div>
<script type="module" src="/src/storage-wipe.ts"></script>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>

8
pkg/grid/src/env.d.ts vendored Normal file
View File

@ -0,0 +1,8 @@
interface ImportMetaEnv extends Readonly<Record<string, string>> {
readonly VITE_LAST_WIPE: string;
readonly VITE_STORAGE_VERSION: string;
}
interface ImportMeta {
readonly env: ImportMetaEnv;
}

View File

@ -7,7 +7,7 @@ import { persist } from 'zustand/middleware';
import Urbit, { SubscriptionRequestInterface } from '@urbit/http-api';
import { Poke } from '@urbit/api';
import api from './api';
import { useMockData } from './util';
import { createStorageKey, useMockData } from './util';
setAutoFreeze(false);
enablePatches();
@ -73,10 +73,10 @@ export const optReduceState = <S extends Record<string, unknown>, U>(
/* eslint-disable-next-line import/no-mutable-exports */
export let stateStorageKeys: string[] = [];
export const stateStorageKey = (stateName: string) => {
stateName = `${window.ship}-Grid${stateName}State-${import.meta.env.VITE_SHORTHASH as any}`;
stateStorageKeys = [...new Set([...stateStorageKeys, stateName])];
return stateName;
export const stateStorageKey = (stateName: string): string => {
const key = createStorageKey(`${stateName}State`);
stateStorageKeys = [...new Set([...stateStorageKeys, key])];
return key;
};
(window as any).clearStates = () => {
@ -149,7 +149,8 @@ export const createState = <T extends Record<string, unknown>>(
}),
{
blacklist,
name: stateStorageKey(name)
name: stateStorageKey(name),
version: parseInt(import.meta.env.VITE_STORAGE_VERSION, 10)
}
)
);

View File

@ -50,3 +50,7 @@ export function getDarkColor(color: string): string {
const hslaColor = parseToHsla(color);
return hsla(hslaColor[0], hslaColor[1], 1 - hslaColor[2], 1);
}
export function createStorageKey(name: string): string {
return `${window.ship}-${window.desk}-${name}`;
}

View File

@ -0,0 +1,9 @@
import { createStorageKey } from './state/util';
const key = createStorageKey(`storage-wipe-${import.meta.env.VITE_LAST_WIPE}`);
const wiped = localStorage.getItem(key);
if (!wiped) {
localStorage.clear();
localStorage.setItem(key, 'true');
}

View File

@ -7,13 +7,7 @@ import { execSync } from 'child_process';
// https://vitejs.dev/config/
export default ({ mode }) => {
if (mode !== 'mock') {
// using current commit until release
const GIT_DESC = execSync('git rev-parse --short HEAD', { encoding: 'utf8' }).trim();
process.env.VITE_SHORTHASH = GIT_DESC;
} else {
process.env.VITE_SHORTHASH = '1';
}
process.env.VITE_STORAGE_VERSION = Date.now().toString();
Object.assign(process.env, loadEnv(mode, process.cwd()));
const SHIP_URL = process.env.SHIP_URL || process.env.VITE_SHIP_URL || 'http://localhost:8080';