You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

values.ts 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { keyBy, omit } from 'lodash';
  21. import { combineReducers } from 'redux';
  22. import { Action as AppStateAction, Actions as AppStateActions } from '../../../store/appState';
  23. import { ActionType } from '../../../store/utils/actions';
  24. import { SettingValue } from '../../../types/settings';
  25. enum Actions {
  26. receiveValues = 'RECEIVE_VALUES'
  27. }
  28. type Action = ActionType<typeof receiveValues, Actions.receiveValues>;
  29. type SettingsState = T.Dict<SettingValue>;
  30. export interface State {
  31. components: T.Dict<SettingsState>;
  32. global: SettingsState;
  33. }
  34. export function receiveValues(
  35. updateKeys: string[],
  36. settings: Array<{ key: string; value?: string }>,
  37. component?: string
  38. ) {
  39. return { type: Actions.receiveValues, updateKeys, settings, component };
  40. }
  41. function components(state: State['components'] = {}, action: Action) {
  42. const { component: key } = action;
  43. if (!key) {
  44. return state;
  45. }
  46. if (action.type === Actions.receiveValues) {
  47. const settingsByKey = keyBy(action.settings, 'key');
  48. return { ...state, [key]: { ...omit(state[key] || {}, action.updateKeys), ...settingsByKey } };
  49. }
  50. return state;
  51. }
  52. function global(state: State['components'] = {}, action: Action | AppStateAction) {
  53. if (action.type === Actions.receiveValues) {
  54. if (action.component) {
  55. return state;
  56. }
  57. const settingsByKey = keyBy(action.settings, 'key');
  58. return { ...omit(state, action.updateKeys), ...settingsByKey };
  59. }
  60. if (action.type === AppStateActions.SetAppState) {
  61. const settingsByKey: SettingsState = {};
  62. Object.keys(action.appState.settings).forEach(
  63. key => (settingsByKey[key] = { key, value: action.appState.settings[key] })
  64. );
  65. return { ...state, ...settingsByKey };
  66. }
  67. return state;
  68. }
  69. export default combineReducers({ components, global });
  70. export function getValue(state: State, key: string, component?: string): SettingValue | undefined {
  71. if (component) {
  72. return state.components[component] && state.components[component][key];
  73. }
  74. return state.global[key];
  75. }