localStorage.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import * as Sentry from '@sentry/react';
  2. import localStorage from 'sentry/utils/localStorage';
  3. import type {SourceSuggestion} from '../types';
  4. const ADVANCED_DATA_SCRUBBING_LOCALSTORAGE_KEY = 'advanced-data-scrubbing';
  5. type StorageValue = {
  6. eventId: string;
  7. sourceSuggestions: Array<SourceSuggestion>;
  8. };
  9. // TODO(Priscila): add the method below in app/utils
  10. function fetchFromStorage(): StorageValue | undefined {
  11. const storage = localStorage.getItem(ADVANCED_DATA_SCRUBBING_LOCALSTORAGE_KEY);
  12. if (!storage) {
  13. return undefined;
  14. }
  15. try {
  16. return JSON.parse(storage);
  17. } catch (err) {
  18. Sentry.withScope(scope => {
  19. scope.setExtra('storage', storage);
  20. Sentry.captureException(err);
  21. });
  22. return undefined;
  23. }
  24. }
  25. function saveToStorage(obj: StorageValue) {
  26. try {
  27. localStorage.setItem(ADVANCED_DATA_SCRUBBING_LOCALSTORAGE_KEY, JSON.stringify(obj));
  28. } catch (err) {
  29. Sentry.captureException(err);
  30. Sentry.withScope(scope => {
  31. scope.setExtra('storage', obj);
  32. Sentry.captureException(err);
  33. });
  34. }
  35. }
  36. export {fetchFromStorage, saveToStorage};