modalStore.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {createStore} from 'reflux';
  2. import {ModalOptions, ModalRenderProps} from 'sentry/actionCreators/modal';
  3. import {CommonStoreDefinition} from './types';
  4. type Renderer = (renderProps: ModalRenderProps) => React.ReactNode;
  5. type State = {
  6. options: ModalOptions;
  7. renderer: Renderer | null;
  8. };
  9. interface ModalStoreDefinition extends CommonStoreDefinition<State> {
  10. closeModal(): void;
  11. getState(): State;
  12. init(): void;
  13. openModal(renderer: Renderer, options: ModalOptions): void;
  14. reset(): void;
  15. }
  16. const storeConfig: ModalStoreDefinition = {
  17. init() {
  18. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  19. // listeners due to their leaky nature in tests.
  20. this.reset();
  21. },
  22. get() {
  23. return this.state;
  24. },
  25. getState() {
  26. return this.state;
  27. },
  28. reset() {
  29. this.state = {
  30. renderer: null,
  31. options: {},
  32. } as State;
  33. },
  34. closeModal() {
  35. this.reset();
  36. this.trigger(this.state);
  37. },
  38. openModal(renderer: Renderer, options: ModalOptions) {
  39. this.state = {renderer, options};
  40. this.trigger(this.state);
  41. },
  42. };
  43. const ModalStore = createStore(storeConfig);
  44. export default ModalStore;