index.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { logError } from "@ente/shared/sentry";
  2. export enum LS_KEYS {
  3. USER = "user",
  4. SESSION = "session",
  5. KEY_ATTRIBUTES = "keyAttributes",
  6. ORIGINAL_KEY_ATTRIBUTES = "originalKeyAttributes",
  7. SUBSCRIPTION = "subscription",
  8. FAMILY_DATA = "familyData",
  9. PLANS = "plans",
  10. IS_FIRST_LOGIN = "isFirstLogin",
  11. JUST_SIGNED_UP = "justSignedUp",
  12. SHOW_BACK_BUTTON = "showBackButton",
  13. EXPORT = "export",
  14. AnonymizedUserID = "anonymizedUserID",
  15. THUMBNAIL_FIX_STATE = "thumbnailFixState",
  16. LIVE_PHOTO_INFO_SHOWN_COUNT = "livePhotoInfoShownCount",
  17. LOGS = "logs",
  18. USER_DETAILS = "userDetails",
  19. COLLECTION_SORT_BY = "collectionSortBy",
  20. THEME = "theme",
  21. WAIT_TIME = "waitTime",
  22. API_ENDPOINT = "apiEndpoint",
  23. // Moved to the new wrapper @/utils/local-storage
  24. // LOCALE = 'locale',
  25. MAP_ENABLED = "mapEnabled",
  26. SRP_SETUP_ATTRIBUTES = "srpSetupAttributes",
  27. SRP_ATTRIBUTES = "srpAttributes",
  28. OPT_OUT_OF_CRASH_REPORTS = "optOutOfCrashReports",
  29. CF_PROXY_DISABLED = "cfProxyDisabled",
  30. REFERRAL_SOURCE = "referralSource",
  31. CLIENT_PACKAGE = "clientPackage",
  32. }
  33. export const setData = (key: LS_KEYS, value: object) => {
  34. if (typeof localStorage === "undefined") {
  35. return null;
  36. }
  37. localStorage.setItem(key, JSON.stringify(value));
  38. };
  39. export const removeData = (key: LS_KEYS) => {
  40. if (typeof localStorage === "undefined") {
  41. return null;
  42. }
  43. localStorage.removeItem(key);
  44. };
  45. export const getData = (key: LS_KEYS) => {
  46. try {
  47. if (
  48. typeof localStorage === "undefined" ||
  49. typeof key === "undefined" ||
  50. typeof localStorage.getItem(key) === "undefined" ||
  51. localStorage.getItem(key) === "undefined"
  52. ) {
  53. return null;
  54. }
  55. const data = localStorage.getItem(key);
  56. return data && JSON.parse(data);
  57. } catch (e) {
  58. logError(e, "Failed to Parse JSON for key " + key);
  59. }
  60. };
  61. export const clearData = () => {
  62. if (typeof localStorage === "undefined") {
  63. return null;
  64. }
  65. localStorage.clear();
  66. };