Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kobakazu0429 committed Dec 6, 2023
1 parent 0468c27 commit 4b99db7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/components/SettingModal/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/editor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ export const recoveryCode = (): RecoveryCode => {
if (decompressedLz !== "") {
const recovered = JSON.parse(decompressedLz) as Partial<RecoveryCode>;
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 };
};
Expand Down
22 changes: 16 additions & 6 deletions src/localStorage/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -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 = () => {
Expand All @@ -66,6 +75,7 @@ export const getUser = (): User => {
};

export const updateUser = (user: Partial<User>) => {
const prev = getUser();
store.local.set(USER_KEY, { ...prev, ...user });
const prevUser = getUser();
const newUser: User = { ...prevUser, ...user };
store.local.set(USER_KEY, newUser);
};
2 changes: 1 addition & 1 deletion src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Settings = { config: Config; env: Env; argvs: Argvs };
export const settings = writable<Settings>({
config: [
{ key: "timeout [ms]", value: "3000" },
{ key: "User ID", value: "" }
{ key: "User ID", value: "__IGNORE_ME__" }
// { key: "use File System", value: false },
],

Expand Down

0 comments on commit 4b99db7

Please sign in to comment.