Add a basic local storage wrapper
This commit is contained in:
parent
4b38703750
commit
9c58731403
2 changed files with 34 additions and 1 deletions
|
@ -25,7 +25,7 @@ export const supportedLocales = [
|
|||
"es-ES" /* Spanish */,
|
||||
] as const;
|
||||
|
||||
/** The type of {@link supportedLocale}s. */
|
||||
/** The type of {@link supportedLocales}. */
|
||||
export type SupportedLocale = (typeof supportedLocales)[number];
|
||||
|
||||
/**
|
||||
|
|
33
packages/utils/local-storage.ts
Normal file
33
packages/utils/local-storage.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
/**
|
||||
* Keys corresponding to the values 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 value from local storage
|
||||
*/
|
||||
export const getLSString = (key: LSKey) => {
|
||||
const value = localStorage.getItem(key);
|
||||
if (value === null) return undefined;
|
||||
return value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Save a string value in local storage
|
||||
*/
|
||||
export const setLSString = (key: LSKey, value: string) => {
|
||||
localStorage.setItem(key, value);
|
||||
};
|
Loading…
Reference in a new issue