modalStore.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import {createStore, StoreDefinition} from 'reflux';
  2. import {ModalOptions, ModalRenderProps} from 'sentry/actionCreators/modal';
  3. type Renderer = (renderProps: ModalRenderProps) => React.ReactNode;
  4. type ModalStoreState = {
  5. options: ModalOptions;
  6. renderer: Renderer | null;
  7. };
  8. interface ModalStoreDefinition extends StoreDefinition {
  9. closeModal(): void;
  10. get(): ModalStoreState;
  11. getState(): ModalStoreState;
  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 ModalStoreState;
  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;