configStore.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import moment from 'moment-timezone';
  2. import {createStore} from 'reflux';
  3. import {Config} from 'sentry/types';
  4. import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
  5. import {CommonStoreDefinition} from './types';
  6. interface InternalConfigStore {
  7. config: Config;
  8. }
  9. interface ConfigStoreDefinition
  10. extends CommonStoreDefinition<Config>,
  11. InternalConfigStore {
  12. get<K extends keyof Config>(key: K): Config[K];
  13. getConfig(): Config;
  14. init(): void;
  15. loadInitialData(config: Config): void;
  16. set<K extends keyof Config>(key: K, value: Config[K]): void;
  17. updateTheme(theme: 'light' | 'dark'): void;
  18. }
  19. const storeConfig: ConfigStoreDefinition = {
  20. // When the app is booted we will _immediately_ hydrate the config store,
  21. // effecively ensuring this is not empty.
  22. config: {} as Config,
  23. init(): void {
  24. this.config = {} as Config;
  25. },
  26. get(key) {
  27. return this.config[key];
  28. },
  29. set(key, value) {
  30. this.config = {
  31. ...this.config,
  32. [key]: value,
  33. };
  34. this.trigger({[key]: value});
  35. },
  36. /**
  37. * This is only called by media query listener so that we can control
  38. * the auto switching of color schemes without affecting manual toggle
  39. */
  40. updateTheme(theme) {
  41. if (this.config.user?.options.theme !== 'system') {
  42. return;
  43. }
  44. this.set('theme', theme);
  45. },
  46. loadInitialData(config): void {
  47. const shouldUseDarkMode = config.user?.options.theme === 'dark';
  48. this.config = {
  49. ...config,
  50. features: new Set(config.features || []),
  51. theme: shouldUseDarkMode ? 'dark' : 'light',
  52. };
  53. // TODO(dcramer): abstract this out of ConfigStore
  54. if (config.user) {
  55. config.user.permissions = new Set(config.user.permissions);
  56. moment.tz.setDefault(config.user.options.timezone);
  57. }
  58. this.trigger(config);
  59. },
  60. getConfig() {
  61. return this.config;
  62. },
  63. getState() {
  64. return this.config;
  65. },
  66. };
  67. export default createStore(makeSafeRefluxStore(storeConfig));