modalStore.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import {createStore} from 'reflux';
  2. import type {ModalOptions, ModalRenderProps} from 'sentry/actionCreators/modal';
  3. import type {StrictStoreDefinition} from './types';
  4. type Renderer = (renderProps: ModalRenderProps) => React.ReactNode;
  5. type State = {
  6. options: ModalOptions;
  7. renderer: Renderer | null;
  8. };
  9. interface ModalStoreDefinition extends StrictStoreDefinition<State> {
  10. closeModal(): void;
  11. init(): void;
  12. openModal(renderer: Renderer, options: ModalOptions): void;
  13. reset(): void;
  14. }
  15. const storeConfig: ModalStoreDefinition = {
  16. state: {renderer: null, options: {}},
  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. };
  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;