indicatorStore.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import {createStore} from 'reflux';
  2. import {Indicator} from 'sentry/actionCreators/indicator';
  3. import {t} from 'sentry/locale';
  4. import {CommonStoreDefinition} from './types';
  5. interface InternalDefinition {
  6. items: any[];
  7. lastId: number;
  8. }
  9. interface IndicatorStoreDefinition
  10. extends CommonStoreDefinition<Indicator[]>,
  11. InternalDefinition {
  12. /**
  13. * When this method is called directly via older parts of the application,
  14. * we want to maintain the old behavior in that it is replaced (and not queued up)
  15. *
  16. * @param message Toast message to be displayed
  17. * @param type One of ['error', 'success', '']
  18. * @param options Options object
  19. */
  20. add(
  21. message: React.ReactNode,
  22. type?: Indicator['type'],
  23. options?: Indicator['options']
  24. ): Indicator;
  25. addError(message?: string): Indicator;
  26. /**
  27. * Alias for add()
  28. */
  29. addMessage(
  30. message: React.ReactNode,
  31. type: Indicator['type'],
  32. options?: Indicator['options']
  33. ): Indicator;
  34. addSuccess(message: string): Indicator;
  35. /**
  36. * Appends a message to be displayed in list of indicators
  37. *
  38. * @param message Toast message to be displayed
  39. * @param type One of ['error', 'success', '']
  40. * @param options Options object
  41. */
  42. append(
  43. message: React.ReactNode,
  44. type: Indicator['type'],
  45. options?: Indicator['options']
  46. ): Indicator;
  47. /**
  48. * Remove all current indicators.
  49. */
  50. clear(): void;
  51. init(): void;
  52. /**
  53. * Remove an indicator
  54. */
  55. remove(indicator: Indicator): void;
  56. }
  57. const storeConfig: IndicatorStoreDefinition = {
  58. items: [],
  59. lastId: 0,
  60. init() {
  61. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  62. // listeners due to their leaky nature in tests.
  63. this.items = [];
  64. this.lastId = 0;
  65. },
  66. addSuccess(message) {
  67. return this.add(message, 'success', {duration: 2000});
  68. },
  69. addError(message = t('An error occurred')) {
  70. return this.add(message, 'error', {duration: 2000});
  71. },
  72. addMessage(message, type, {append, ...options} = {}) {
  73. const indicator: Indicator = {
  74. id: this.lastId++,
  75. message,
  76. type,
  77. options,
  78. clearId: null,
  79. };
  80. if (options.duration) {
  81. indicator.clearId = window.setTimeout(() => {
  82. this.remove(indicator);
  83. }, options.duration);
  84. }
  85. const newItems = append ? [...this.items, indicator] : [indicator];
  86. this.items = newItems;
  87. this.trigger(this.items);
  88. return indicator;
  89. },
  90. append(message, type, options) {
  91. return this.addMessage(message, type, {
  92. ...options,
  93. append: true,
  94. });
  95. },
  96. add(message, type = 'loading', options = {}) {
  97. return this.addMessage(message, type, {
  98. ...options,
  99. append: false,
  100. });
  101. },
  102. clear() {
  103. this.items = [];
  104. this.trigger(this.items);
  105. },
  106. remove(indicator) {
  107. if (!indicator) {
  108. return;
  109. }
  110. this.items = this.items.filter(item => item !== indicator);
  111. if (indicator.clearId) {
  112. window.clearTimeout(indicator.clearId);
  113. indicator.clearId = null;
  114. }
  115. this.trigger(this.items);
  116. },
  117. getState() {
  118. return this.items;
  119. },
  120. };
  121. const IndicatorStore = createStore(storeConfig);
  122. export default IndicatorStore;