configStore.tsx 1.6 KB

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