localStorage.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { logError } from 'utils/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. LOCALE = 'locale',
  24. MAP_ENABLED = 'mapEnabled',
  25. SRP_SETUP_ATTRIBUTES = 'srpSetupAttributes',
  26. SRP_ATTRIBUTES = 'srpAttributes',
  27. OPT_OUT_OF_CRASH_REPORTS = 'optOutOfCrashReports',
  28. }
  29. export const setData = (key: LS_KEYS, value: object) => {
  30. if (typeof localStorage === 'undefined') {
  31. return null;
  32. }
  33. localStorage.setItem(key, JSON.stringify(value));
  34. };
  35. export const removeData = (key: LS_KEYS) => {
  36. if (typeof localStorage === 'undefined') {
  37. return null;
  38. }
  39. localStorage.removeItem(key);
  40. };
  41. export const getData = (key: LS_KEYS) => {
  42. try {
  43. if (
  44. typeof localStorage === 'undefined' ||
  45. typeof key === 'undefined' ||
  46. typeof localStorage.getItem(key) === 'undefined' ||
  47. localStorage.getItem(key) === 'undefined'
  48. ) {
  49. return null;
  50. }
  51. const data = localStorage.getItem(key);
  52. return data && JSON.parse(data);
  53. } catch (e) {
  54. logError(e, 'Failed to Parse JSON for key ' + key);
  55. }
  56. };
  57. export const clearData = () => {
  58. if (typeof localStorage === 'undefined') {
  59. return null;
  60. }
  61. localStorage.clear();
  62. };