inputWithStorage.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import {useCallback, useMemo} from 'react';
  2. import * as Sentry from '@sentry/react';
  3. import debounce from 'lodash/debounce';
  4. import NoteInput from 'sentry/components/activity/note/input';
  5. import {MentionChangeEvent} from 'sentry/components/activity/note/types';
  6. import {NoteType} from 'sentry/types/alerts';
  7. import localStorage from 'sentry/utils/localStorage';
  8. type InputProps = React.ComponentProps<typeof NoteInput>;
  9. type Props = {
  10. itemKey: string;
  11. storageKey: string;
  12. onLoad?: (data: string) => string;
  13. onSave?: (data: string) => string;
  14. text?: string;
  15. } & InputProps;
  16. function fetchFromStorage(storageKey: string) {
  17. const storage = localStorage.getItem(storageKey);
  18. if (!storage) {
  19. return null;
  20. }
  21. try {
  22. return JSON.parse(storage);
  23. } catch (err) {
  24. Sentry.withScope(scope => {
  25. scope.setExtra('storage', storage);
  26. Sentry.captureException(err);
  27. });
  28. return null;
  29. }
  30. }
  31. function saveToStorage(storageKey: string, obj: Record<string, any>) {
  32. try {
  33. localStorage.setItem(storageKey, JSON.stringify(obj));
  34. } catch (err) {
  35. Sentry.captureException(err);
  36. Sentry.withScope(scope => {
  37. scope.setExtra('storage', obj);
  38. Sentry.captureException(err);
  39. });
  40. }
  41. }
  42. function NoteInputWithStorage({
  43. itemKey,
  44. storageKey,
  45. onChange,
  46. onCreate,
  47. onLoad,
  48. onSave,
  49. text,
  50. ...props
  51. }: Props) {
  52. const value = useMemo(() => {
  53. if (text) {
  54. return text;
  55. }
  56. const storageObj = fetchFromStorage(storageKey);
  57. if (!storageObj) {
  58. return '';
  59. }
  60. if (!storageObj.hasOwnProperty(itemKey)) {
  61. return '';
  62. }
  63. if (!onLoad) {
  64. return storageObj[itemKey];
  65. }
  66. return onLoad(storageObj[itemKey]);
  67. }, [itemKey, onLoad, storageKey, text]);
  68. const save = useMemo(
  69. () =>
  70. debounce((newValue: string) => {
  71. const currentObj = fetchFromStorage(storageKey) ?? {};
  72. const newObject = {
  73. ...currentObj,
  74. [itemKey]: onSave?.(newValue) ?? newValue,
  75. };
  76. saveToStorage(storageKey, newObject);
  77. }, 150),
  78. [itemKey, onSave, storageKey]
  79. );
  80. const handleChange = useCallback(
  81. (e: MentionChangeEvent, options: {updating?: boolean} = {}) => {
  82. onChange?.(e, options);
  83. if (options.updating) {
  84. return;
  85. }
  86. save(e.target.value);
  87. },
  88. [onChange, save]
  89. );
  90. /**
  91. * Handler when note is created.
  92. *
  93. * Remove in progress item from local storage if it exists
  94. */
  95. const handleCreate = useCallback(
  96. (data: NoteType) => {
  97. onCreate?.(data);
  98. // Remove from local storage
  99. const storageObj = fetchFromStorage(storageKey) ?? {};
  100. // Nothing from this `itemKey` is saved to storage, do nothing
  101. if (!storageObj.hasOwnProperty(itemKey)) {
  102. return;
  103. }
  104. // Remove `itemKey` from stored object and save to storage
  105. // eslint-disable-next-line no-unused-vars
  106. const {[itemKey]: _oldItem, ...newStorageObj} = storageObj;
  107. saveToStorage(storageKey, newStorageObj);
  108. },
  109. [itemKey, onCreate, storageKey]
  110. );
  111. // Make sure `this.props` does not override `onChange` and `onCreate`
  112. return (
  113. <NoteInput {...props} text={value} onCreate={handleCreate} onChange={handleChange} />
  114. );
  115. }
  116. export {NoteInputWithStorage};