configStore.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. this.config = {} as Config;
  22. },
  23. get(key) {
  24. return this.config[key];
  25. },
  26. set(key, value) {
  27. this.config = {...this.config, [key]: value};
  28. this.trigger({[key]: value});
  29. },
  30. loadInitialData(config): void {
  31. const shouldUseDarkMode = config.user?.options.theme === 'dark';
  32. this.config = {
  33. ...config,
  34. features: new Set(config.features || []),
  35. theme: shouldUseDarkMode ? 'dark' : 'light',
  36. };
  37. // TODO(dcramer): abstract this out of ConfigStore
  38. if (config.user) {
  39. config.user.permissions = new Set(config.user.permissions);
  40. moment.tz.setDefault(config.user.options.timezone);
  41. }
  42. this.trigger(config);
  43. },
  44. getState() {
  45. return this.config;
  46. },
  47. };
  48. const ConfigStore = createStore(storeConfig);
  49. export default ConfigStore;