local-storage.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * Keys corresponding to the items that we save in local storage.
  3. *
  4. * The type of each of the these keys is {@link LSKey}.
  5. *
  6. * Note: [Local Storage]
  7. *
  8. * Data in the local storage is persisted even after the user closes the tab (or
  9. * the browser itself). This is in contrast with session storage, where the data
  10. * is cleared when the browser tab is closed.
  11. *
  12. * The data in local storage is tied to the Document's origin (scheme + host).
  13. */
  14. export const lsKeys = ["locale"] as const;
  15. /** The type of {@link lsKeys}. */
  16. export type LSKey = (typeof lsKeys)[number];
  17. /**
  18. * Read a previously saved string from local storage
  19. */
  20. export const getLSString = (key: LSKey) => {
  21. const item = localStorage.getItem(key);
  22. if (item === null) return undefined;
  23. return item;
  24. };
  25. /**
  26. * Save a string in local storage
  27. */
  28. export const setLSString = (key: LSKey, value: string) => {
  29. localStorage.setItem(key, value);
  30. };
  31. /**
  32. * Remove an string from local storage.
  33. */
  34. export const removeLSString = (key: LSKey) => {
  35. localStorage.removeItem(key);
  36. };