modalStore.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import {createStore, StoreDefinition} from 'reflux';
  2. import {ModalOptions, ModalRenderProps} from 'sentry/actionCreators/modal';
  3. import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
  4. type Renderer = (renderProps: ModalRenderProps) => React.ReactNode;
  5. type ModalStoreState = {
  6. options: ModalOptions;
  7. renderer: Renderer | null;
  8. };
  9. interface ModalStoreDefinition extends StoreDefinition {
  10. closeModal(): void;
  11. get(): ModalStoreState;
  12. getState(): ModalStoreState;
  13. init(): void;
  14. openModal(renderer: Renderer, options: ModalOptions): void;
  15. reset(): void;
  16. }
  17. const storeConfig: ModalStoreDefinition = {
  18. unsubscribeListeners: [],
  19. init() {
  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(makeSafeRefluxStore(storeConfig));
  44. export default ModalStore;