From f10f751a2f03a5a3078f1f6d016f5c025e841d7a Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Wed, 3 Apr 2024 13:50:17 +0530 Subject: [PATCH] Inline local storage calls The methods are trivial, and we cannot centralize the keys since they will be different for different apps. So an abstraction for this is not beneficial. Also move the next specific dev build check to @/next --- web/docs/storage.md | 16 +++++++++++ web/packages/{utils => next}/env.ts | 0 web/packages/next/hello.ts | 4 --- web/packages/next/i18n.ts | 19 +++++-------- web/packages/shared/logging/index.ts | 2 +- web/packages/shared/logging/web.ts | 2 +- web/packages/utils/local-storage.ts | 40 ---------------------------- 7 files changed, 25 insertions(+), 58 deletions(-) create mode 100644 web/docs/storage.md rename web/packages/{utils => next}/env.ts (100%) delete mode 100644 web/packages/next/hello.ts delete mode 100644 web/packages/utils/local-storage.ts diff --git a/web/docs/storage.md b/web/docs/storage.md new file mode 100644 index 0000000000..8f072684bf --- /dev/null +++ b/web/docs/storage.md @@ -0,0 +1,16 @@ +# Storage + +## Local Storage + +Data in the local storage is persisted even after the user closes the tab (or +the browser itself). This is in contrast with session storage, where the data is +cleared when the browser tab is closed. + +The data in local storage is tied to the Document's origin (scheme + host). + +## Session Storage + +## Indexed DB + +We use the LocalForage library for storing things in Indexed DB. This library +falls back to localStorage in case Indexed DB storage is not available. diff --git a/web/packages/utils/env.ts b/web/packages/next/env.ts similarity index 100% rename from web/packages/utils/env.ts rename to web/packages/next/env.ts diff --git a/web/packages/next/hello.ts b/web/packages/next/hello.ts deleted file mode 100644 index 6e26a0ea88..0000000000 --- a/web/packages/next/hello.ts +++ /dev/null @@ -1,4 +0,0 @@ -/** Howdy! */ -export const sayNamaste = () => { - console.log("Namaste, world"); -}; diff --git a/web/packages/next/i18n.ts b/web/packages/next/i18n.ts index 9f0f2a3970..3f51d5c101 100644 --- a/web/packages/next/i18n.ts +++ b/web/packages/next/i18n.ts @@ -1,9 +1,4 @@ -import { isDevBuild } from "@/utils/env"; -import { - getLSString, - removeLSString, - setLSString, -} from "@/utils/local-storage"; +import { isDevBuild } from "@/next/env"; import { logError } from "@/utils/logging"; import { includes } from "@/utils/type-guards"; import { getUserLocales } from "get-user-locale"; @@ -119,8 +114,8 @@ export const setupI18n = async () => { * If it finds a locale stored in the old format, it also updates the saved * value and returns it in the new format. */ -const savedLocaleStringMigratingIfNeeded = () => { - const ls = getLSString("locale"); +const savedLocaleStringMigratingIfNeeded = (): SupportedLocale | undefined => { + const ls = localStorage.getItem("locale"); // An older version of our code had stored only the language code, not the // full locale. Migrate these to the new locale format. Luckily, all such @@ -133,7 +128,7 @@ const savedLocaleStringMigratingIfNeeded = () => { if (!ls) { // Nothing found - return ls; + return undefined; } if (includes(supportedLocales, ls)) { @@ -150,12 +145,12 @@ const savedLocaleStringMigratingIfNeeded = () => { // have happened, we're the only one setting it. logError("Failed to parse locale obtained from local storage", e); // Also remove the old key, it is not parseable by us anymore. - removeLSString("locale"); + localStorage.removeItem("locale"); return undefined; } const newValue = mapOldValue(value); - if (newValue) setLSString("locale", newValue); + if (newValue) localStorage.setItem("locale", newValue); return newValue; }; @@ -249,6 +244,6 @@ export const getLocaleInUse = (): SupportedLocale => { * preference that is stored in local storage. */ export const setLocaleInUse = async (locale: SupportedLocale) => { - setLSString("locale", locale); + localStorage.setItem("locale", locale); return i18n.changeLanguage(locale); }; diff --git a/web/packages/shared/logging/index.ts b/web/packages/shared/logging/index.ts index c7c793dbfe..12ff5bf6ea 100644 --- a/web/packages/shared/logging/index.ts +++ b/web/packages/shared/logging/index.ts @@ -1,4 +1,4 @@ -import { isDevBuild } from "@/utils/env"; +import { isDevBuild } from "@/next/env"; import { logError } from "@ente/shared/sentry"; import isElectron from "is-electron"; import { WorkerSafeElectronService } from "../electron/service"; diff --git a/web/packages/shared/logging/web.ts b/web/packages/shared/logging/web.ts index 7c9e7e2ed9..7b15851b86 100644 --- a/web/packages/shared/logging/web.ts +++ b/web/packages/shared/logging/web.ts @@ -1,4 +1,4 @@ -import { isDevBuild } from "@/utils/env"; +import { isDevBuild } from "@/next/env"; import { logError } from "@ente/shared/sentry"; import { getData, diff --git a/web/packages/utils/local-storage.ts b/web/packages/utils/local-storage.ts deleted file mode 100644 index 7da0b150c0..0000000000 --- a/web/packages/utils/local-storage.ts +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Keys corresponding to the items that we save in local storage. - * - * The type of each of the these keys is {@link LSKey}. - * - * Note: [Local Storage] - * - * Data in the local storage is persisted even after the user closes the tab (or - * the browser itself). This is in contrast with session storage, where the data - * is cleared when the browser tab is closed. - * - * The data in local storage is tied to the Document's origin (scheme + host). - */ -export const lsKeys = ["locale"] as const; - -/** The type of {@link lsKeys}. */ -export type LSKey = (typeof lsKeys)[number]; - -/** - * Read a previously saved string from local storage - */ -export const getLSString = (key: LSKey) => { - const item = localStorage.getItem(key); - if (item === null) return undefined; - return item; -}; - -/** - * Save a string in local storage - */ -export const setLSString = (key: LSKey, value: string) => { - localStorage.setItem(key, value); -}; - -/** - * Remove an string from local storage. - */ -export const removeLSString = (key: LSKey) => { - localStorage.removeItem(key); -};