index.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import log from "@/next/log";
  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. IS_FIRST_LOGIN = "isFirstLogin",
  10. JUST_SIGNED_UP = "justSignedUp",
  11. SHOW_BACK_BUTTON = "showBackButton",
  12. EXPORT = "export",
  13. THUMBNAIL_FIX_STATE = "thumbnailFixState",
  14. LIVE_PHOTO_INFO_SHOWN_COUNT = "livePhotoInfoShownCount",
  15. // LOGS = "logs",
  16. USER_DETAILS = "userDetails",
  17. COLLECTION_SORT_BY = "collectionSortBy",
  18. THEME = "theme",
  19. WAIT_TIME = "waitTime",
  20. API_ENDPOINT = "apiEndpoint",
  21. // Moved to the new wrapper @/next/local-storage
  22. // LOCALE = 'locale',
  23. MAP_ENABLED = "mapEnabled",
  24. SRP_SETUP_ATTRIBUTES = "srpSetupAttributes",
  25. SRP_ATTRIBUTES = "srpAttributes",
  26. CF_PROXY_DISABLED = "cfProxyDisabled",
  27. REFERRAL_SOURCE = "referralSource",
  28. CLIENT_PACKAGE = "clientPackage",
  29. }
  30. export const setData = (key: LS_KEYS, value: object) => {
  31. if (typeof localStorage === "undefined") {
  32. return null;
  33. }
  34. localStorage.setItem(key, JSON.stringify(value));
  35. };
  36. export const removeData = (key: LS_KEYS) => {
  37. if (typeof localStorage === "undefined") {
  38. return null;
  39. }
  40. localStorage.removeItem(key);
  41. };
  42. export const getData = (key: LS_KEYS) => {
  43. try {
  44. if (
  45. typeof localStorage === "undefined" ||
  46. typeof key === "undefined" ||
  47. typeof localStorage.getItem(key) === "undefined" ||
  48. localStorage.getItem(key) === "undefined"
  49. ) {
  50. return null;
  51. }
  52. const data = localStorage.getItem(key);
  53. return data && JSON.parse(data);
  54. } catch (e) {
  55. log.error(`Failed to Parse JSON for key ${key}`, e);
  56. }
  57. };
  58. export const clearData = () => {
  59. if (typeof localStorage === "undefined") {
  60. return null;
  61. }
  62. localStorage.clear();
  63. };