configStore.tsx 1.9 KB

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