indicatorStore.tsx 3.5 KB

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