From 4b99db7ad2f6f36899e6000e3a55fea828ce08da Mon Sep 17 00:00:00 2001 From: kobakazu0429 Date: Wed, 6 Dec 2023 13:09:06 +0900 Subject: [PATCH] fix --- src/components/SettingModal/index.svelte | 2 +- src/editor/utils.ts | 4 ++-- src/localStorage/index.ts | 22 ++++++++++++++++------ src/store.ts | 2 +- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/components/SettingModal/index.svelte b/src/components/SettingModal/index.svelte index 34ebbb3..40d1719 100644 --- a/src/components/SettingModal/index.svelte +++ b/src/components/SettingModal/index.svelte @@ -39,7 +39,7 @@ settings.subscribe((s) => { const userId = s.config.find((c) => c.key === "User ID"); - if (userId && typeof userId.value === "string" && userId.value.length > 0) { + if (userId && typeof userId.value === "string" && userId.value !== "__IGNORE_ME__") { updateUser({ id: userId.value }); } }); diff --git a/src/editor/utils.ts b/src/editor/utils.ts index 2bbbe68..50b1773 100644 --- a/src/editor/utils.ts +++ b/src/editor/utils.ts @@ -74,13 +74,13 @@ export const recoveryCode = (): RecoveryCode => { if (decompressedLz !== "") { const recovered = JSON.parse(decompressedLz) as Partial; recovered.code = recovered.code ? recovered.code.replaceAll(/\r(\r)+/g, "\r") : ""; - recovered.filename ??= `${user.id}-${ulid()}.c`; + recovered.filename ??= `${user ? user.id + "-" : ""}${ulid()}.c`; return recovered as RecoveryCode; } } const previousCode = getPreviousCode(); const code = previousCode?.code ?? ""; - const filename = previousCode?.filename ?? `${user.id}-${ulid()}.c`; + const filename = previousCode?.filename ?? `${user ? user.id + "-" : ""}${ulid()}.c`; const tests = previousCode?.tests; return { code, filename, tests }; }; diff --git a/src/localStorage/index.ts b/src/localStorage/index.ts index f2ba7d2..c1cf40b 100644 --- a/src/localStorage/index.ts +++ b/src/localStorage/index.ts @@ -1,6 +1,7 @@ import store from "store2"; import { ulid } from "ulid"; import type { Test } from "../test"; +import { settings } from "../store"; const joinKey = (keys: string | string[]) => { const PREFIX = "wasm_c_web"; @@ -10,7 +11,7 @@ const joinKey = (keys: string | string[]) => { const CODE_KEY = joinKey("auto_saved"); const VISITED_KEY = joinKey("visited"); const USER_KEY = joinKey("user"); -const USER_VERSION = 1; +const LATEST_USER_VERSION = 1; type UserV1 = { version: 1; id: string; @@ -45,15 +46,23 @@ export const visited = () => { store.local.set(VISITED_KEY, true); if (store.local.has(USER_KEY)) { const user = store.local.get(USER_KEY) as User; - if (user?.version !== USER_VERSION) { - store.local.set(USER_KEY, { ...user, version: USER_VERSION }); + switch (user.version) { + case 1: + store.local.set(USER_KEY, { ...user, version: LATEST_USER_VERSION }); } } else { store.local.set(USER_KEY, { - version: USER_VERSION, + version: LATEST_USER_VERSION, id: import.meta.env.DEV ? "__I_AM_DEV_USER__" : ulid() }); } + + const user = getUser(); + settings.update((self) => { + const config = self.config.find((c) => c.key === "User ID"); + if (config) config.value = user.id; + return self; + }); }; export const isVisited = () => { @@ -66,6 +75,7 @@ export const getUser = (): User => { }; export const updateUser = (user: Partial) => { - const prev = getUser(); - store.local.set(USER_KEY, { ...prev, ...user }); + const prevUser = getUser(); + const newUser: User = { ...prevUser, ...user }; + store.local.set(USER_KEY, newUser); }; diff --git a/src/store.ts b/src/store.ts index 508f838..a588e06 100644 --- a/src/store.ts +++ b/src/store.ts @@ -10,7 +10,7 @@ type Settings = { config: Config; env: Env; argvs: Argvs }; export const settings = writable({ config: [ { key: "timeout [ms]", value: "3000" }, - { key: "User ID", value: "" } + { key: "User ID", value: "__IGNORE_ME__" } // { key: "use File System", value: false }, ],