1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
/*
* SonarQube
* Copyright (C) 2009-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { queryOptions, useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { addGlobalSuccessMessage } from 'design-system';
import {
getValue,
getValues,
resetSettingValue,
setSettingValue,
setSimpleSettingValue,
} from '../api/settings';
import { translate } from '../helpers/l10n';
import { ExtendedSettingDefinition, SettingsKey } from '../types/settings';
import { createQueryHook } from './common';
import { invalidateAllMeasures } from './measures';
const SETTINGS_SAVE_SUCCESS_MESSAGE = translate(
'settings.authentication.form.settings.save_success',
);
type SettingValue = string | boolean | string[];
export function useGetValuesQuery(keys: string[]) {
return useQuery({
queryKey: ['settings', 'values', keys] as const,
queryFn: ({ queryKey: [_a, _b, keys] }) => {
return getValues({ keys });
},
});
}
export const useGetValueQuery = createQueryHook(
({ key, component }: { component?: string; key: string }) => {
return queryOptions({
queryKey: ['settings', 'details', key] as const,
queryFn: ({ queryKey: [_a, _b, key] }) => {
return getValue({ key, component }).then((v) => v ?? null);
},
});
},
);
export const useIsLegacyCCTMode = () => {
return useGetValueQuery(
{ key: SettingsKey.LegacyMode },
{ staleTime: Infinity, select: (data) => data?.value === 'true' },
);
};
export function useResetSettingsMutation() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ keys, component }: { component?: string; keys: string[] }) =>
resetSettingValue({ keys: keys.join(','), component }),
onSuccess: (_, { keys }) => {
keys.forEach((key) => {
queryClient.invalidateQueries({ queryKey: ['settings', 'details', key] });
});
queryClient.invalidateQueries({ queryKey: ['settings', 'values'] });
},
});
}
export function useSaveValuesMutation() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (
values: {
definition: ExtendedSettingDefinition;
newValue?: SettingValue;
}[],
) => {
return Promise.all(
values
.filter((v) => v.newValue !== undefined)
.map(async ({ newValue, definition }) => {
try {
if (isDefaultValue(newValue as string | boolean | string[], definition)) {
await resetSettingValue({ keys: definition.key });
} else {
await setSettingValue(definition, newValue);
}
return { key: definition.key, success: true };
} catch (error) {
return { key: definition.key, success: false };
}
}),
);
},
onSuccess: (data) => {
if (data.length > 0) {
data.forEach(({ key }) => {
queryClient.invalidateQueries({ queryKey: ['settings', 'details', key] });
});
queryClient.invalidateQueries({ queryKey: ['settings', 'values'] });
addGlobalSuccessMessage(SETTINGS_SAVE_SUCCESS_MESSAGE);
}
},
});
}
export function useSaveValueMutation() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({
newValue,
definition,
component,
}: {
component?: string;
definition: ExtendedSettingDefinition;
newValue: SettingValue;
}) => {
if (isDefaultValue(newValue, definition)) {
return resetSettingValue({ keys: definition.key, component });
}
return setSettingValue(definition, newValue, component);
},
onSuccess: (_, { definition }) => {
queryClient.invalidateQueries({ queryKey: ['settings', 'details', definition.key] });
queryClient.invalidateQueries({ queryKey: ['settings', 'values'] });
invalidateAllMeasures(queryClient);
addGlobalSuccessMessage(SETTINGS_SAVE_SUCCESS_MESSAGE);
},
});
}
export function useSaveSimpleValueMutation() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ key, value }: { key: string; value: string }) => {
return setSimpleSettingValue({ key, value });
},
onSuccess: (_, { key }) => {
queryClient.invalidateQueries({ queryKey: ['settings', 'details', key] });
queryClient.invalidateQueries({ queryKey: ['settings', 'values', [key]] });
addGlobalSuccessMessage(SETTINGS_SAVE_SUCCESS_MESSAGE);
},
});
}
function isDefaultValue(value: SettingValue, definition: ExtendedSettingDefinition) {
const defaultValue = definition.defaultValue ?? '';
if (definition.multiValues) {
return defaultValue === (value as string[]).join(',');
}
return defaultValue === String(value);
}
|