modalStore.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. getState() {
  23. return this.state;
  24. },
  25. reset() {
  26. this.state = {
  27. renderer: null,
  28. options: {},
  29. } as State;
  30. },
  31. closeModal() {
  32. this.reset();
  33. this.trigger(this.state);
  34. },
  35. openModal(renderer: Renderer, options: ModalOptions) {
  36. this.state = {renderer, options};
  37. this.trigger(this.state);
  38. },
  39. };
  40. const ModalStore = createStore(storeConfig);
  41. export default ModalStore;