configStore.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import moment from 'moment-timezone';
  2. import {createStore} from 'reflux';
  3. import type {Config} from 'sentry/types/system';
  4. import type {StrictStoreDefinition} from './types';
  5. interface ConfigStoreDefinition extends StrictStoreDefinition<Config> {
  6. get<K extends keyof Config>(key: K): Config[K];
  7. loadInitialData(config: Config): void;
  8. set<K extends keyof Config>(key: K, value: Config[K]): void;
  9. }
  10. const storeConfig: ConfigStoreDefinition = {
  11. // When the app is booted we will _immediately_ hydrate the config store,
  12. // effecively ensuring this is not empty.
  13. state: {} as Config,
  14. init(): void {
  15. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  16. // listeners due to their leaky nature in tests.
  17. this.state = {} as Config;
  18. },
  19. get(key) {
  20. return this.state[key];
  21. },
  22. set(key, value) {
  23. this.state = {...this.state, [key]: value};
  24. this.trigger({[key]: value});
  25. },
  26. loadInitialData(config): void {
  27. const shouldUseDarkMode = config.user?.options.theme === 'dark';
  28. this.state = {
  29. ...config,
  30. features: new Set(config.features || []),
  31. theme: shouldUseDarkMode ? 'dark' : 'light',
  32. };
  33. // TODO(dcramer): abstract this out of ConfigStore
  34. if (config.user) {
  35. config.user.permissions = new Set(config.user.permissions);
  36. moment.tz.setDefault(config.user.options.timezone);
  37. }
  38. this.trigger(config);
  39. },
  40. getState() {
  41. return this.state;
  42. },
  43. };
  44. const ConfigStore = createStore(storeConfig);
  45. export default ConfigStore;