inputWithStorage.tsx 3.3 KB

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