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.

settings.ts 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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 { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
  21. import { addGlobalSuccessMessage } from 'design-system';
  22. import { getValue, getValues, resetSettingValue, setSettingValue } from '../api/settings';
  23. import { translate } from '../helpers/l10n';
  24. import { ExtendedSettingDefinition } from '../types/settings';
  25. type SettingValue = string | boolean | string[];
  26. export function useGetValuesQuery(keys: string[]) {
  27. return useQuery({
  28. queryKey: ['settings', 'values', keys] as const,
  29. queryFn: ({ queryKey: [_a, _b, keys] }) => {
  30. return getValues({ keys });
  31. },
  32. });
  33. }
  34. export function useGetValueQuery(key: string, component?: string) {
  35. return useQuery({
  36. queryKey: ['settings', 'details', key] as const,
  37. queryFn: ({ queryKey: [_a, _b, key] }) => {
  38. return getValue({ key, component }).then((v) => v ?? null);
  39. },
  40. });
  41. }
  42. export function useResetSettingsMutation() {
  43. const queryClient = useQueryClient();
  44. return useMutation({
  45. mutationFn: ({ keys, component }: { keys: string[]; component?: string }) =>
  46. resetSettingValue({ keys: keys.join(','), component }),
  47. onSuccess: (_, { keys }) => {
  48. keys.forEach((key) => {
  49. queryClient.invalidateQueries({ queryKey: ['settings', 'details', key] });
  50. });
  51. queryClient.invalidateQueries({ queryKey: ['settings', 'values'] });
  52. },
  53. });
  54. }
  55. export function useSaveValuesMutation() {
  56. const queryClient = useQueryClient();
  57. return useMutation({
  58. mutationFn: (
  59. values: {
  60. newValue?: SettingValue;
  61. definition: ExtendedSettingDefinition;
  62. }[],
  63. ) => {
  64. return Promise.all(
  65. values
  66. .filter((v) => v.newValue !== undefined)
  67. .map(async ({ newValue, definition }) => {
  68. try {
  69. if (isDefaultValue(newValue as string | boolean | string[], definition)) {
  70. await resetSettingValue({ keys: definition.key });
  71. } else {
  72. await setSettingValue(definition, newValue);
  73. }
  74. return { key: definition.key, success: true };
  75. } catch (error) {
  76. return { key: definition.key, success: false };
  77. }
  78. }),
  79. );
  80. },
  81. onSuccess: (data) => {
  82. if (data.length > 0) {
  83. data.forEach(({ key }) => {
  84. queryClient.invalidateQueries({ queryKey: ['settings', 'details', key] });
  85. });
  86. queryClient.invalidateQueries({ queryKey: ['settings', 'values'] });
  87. addGlobalSuccessMessage(translate('settings.authentication.form.settings.save_success'));
  88. }
  89. },
  90. });
  91. }
  92. export function useSaveValueMutation() {
  93. const queryClient = useQueryClient();
  94. return useMutation({
  95. mutationFn: ({
  96. newValue,
  97. definition,
  98. component,
  99. }: {
  100. newValue: SettingValue;
  101. definition: ExtendedSettingDefinition;
  102. component?: string;
  103. }) => {
  104. if (isDefaultValue(newValue, definition)) {
  105. return resetSettingValue({ keys: definition.key, component });
  106. }
  107. return setSettingValue(definition, newValue, component);
  108. },
  109. onSuccess: (_, { definition }) => {
  110. queryClient.invalidateQueries({ queryKey: ['settings', 'details', definition.key] });
  111. queryClient.invalidateQueries({ queryKey: ['settings', 'values'] });
  112. addGlobalSuccessMessage(translate('settings.authentication.form.settings.save_success'));
  113. },
  114. });
  115. }
  116. function isDefaultValue(value: SettingValue, definition: ExtendedSettingDefinition) {
  117. const defaultValue = definition.defaultValue ?? '';
  118. if (definition.multiValues) {
  119. return defaultValue === (value as string[]).join(',');
  120. }
  121. return defaultValue === String(value);
  122. }