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
This commit is contained in:
Manav Rathi 2024-04-03 13:50:17 +05:30
parent d28daece8a
commit f10f751a2f
No known key found for this signature in database
7 changed files with 25 additions and 58 deletions

16
web/docs/storage.md Normal file
View file

@ -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.

View file

@ -1,4 +0,0 @@
/** Howdy! */
export const sayNamaste = () => {
console.log("Namaste, world");
};

View file

@ -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);
};

View file

@ -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";

View file

@ -1,4 +1,4 @@
import { isDevBuild } from "@/utils/env";
import { isDevBuild } from "@/next/env";
import { logError } from "@ente/shared/sentry";
import {
getData,

View file

@ -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);
};