modalStore.tsx 1.0 KB

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