indicatorStore.tsx 3.3 KB

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