Skip to content

Commit cbfadb0

Browse files
fix: copilot review
1 parent 7f5c82e commit cbfadb0

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/store/reducers/settings/api.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,12 @@ export const settingsApi = api.injectEndpoints({
5353
return;
5454
}
5555

56-
const shouldUseLocalSettings =
57-
!user || !window.api.metaSettings || shouldSyncSettingToLS(name);
56+
const shouldUseLocalSettings = !user || !window.api.metaSettings;
5857

5958
const defaultValue = getSettingDefault(name);
6059

6160
// Preload value from LS or default to store
62-
if (shouldUseLocalSettings) {
61+
if (shouldUseLocalSettings || shouldSyncSettingToLS(name)) {
6362
const savedValue = readSettingValueFromLS(name);
6463
const value = savedValue ?? defaultValue;
6564

@@ -68,6 +67,12 @@ export const settingsApi = api.injectEndpoints({
6867
dispatch(setSettingValue(name, defaultValue));
6968
}
7069

70+
// Do not process query result when only LS is used
71+
// There is not actual api call in this case
72+
if (shouldUseLocalSettings) {
73+
return;
74+
}
75+
7176
try {
7277
const {data} = await queryFulfilled;
7378

@@ -77,8 +82,11 @@ export const settingsApi = api.injectEndpoints({
7782
const parsedValue = parseSettingValue(data?.value);
7883

7984
if (isNil(data?.value)) {
85+
// Pass setting params without value if data is empty
86+
const newValue = data ?? {name, user};
87+
8088
// Try to sync local value if there is no backend value
81-
syncLocalValueToMetaIfNoData({...data}, dispatch);
89+
syncLocalValueToMetaIfNoData(newValue, dispatch);
8290
} else {
8391
dispatch(setSettingValue(name, parsedValue));
8492

@@ -122,15 +130,17 @@ export const settingsApi = api.injectEndpoints({
122130
return;
123131
}
124132

125-
// Extract previous value to revert to it if set is not succesfull
133+
// Extract previous value to revert to it if set is not successful
126134
const previousSettingValue = getSettingValue(getState() as RootState, name);
127135

128136
// Optimistically update store
129137
dispatch(setSettingValue(name, value));
130138

139+
const shouldUseLocalSettings = !user || !window.api.metaSettings;
140+
131141
// If local storage settings should be used
132-
// Update LS and do not do any further code
133-
if (!user || !window.api.metaSettings) {
142+
// Update LS and return early
143+
if (shouldUseLocalSettings) {
134144
setSettingValueToLS(name, value);
135145
return;
136146
}
@@ -167,24 +177,24 @@ export const settingsApi = api.injectEndpoints({
167177
name: settingName,
168178
user,
169179
};
170-
const newValue = {name: settingName, user, value: settingData?.value};
180+
const newSetting = {name: settingName, user, value: settingData?.value};
171181

172182
const patch = dispatch(
173183
settingsApi.util.upsertQueryData(
174184
'getSingleSetting',
175185
cacheEntryParams,
176-
newValue,
186+
newSetting,
177187
),
178188
);
179-
if (isNil(settingData.value)) {
189+
if (isNil(newSetting.value)) {
180190
// Try to sync local value if there is no backend value
181-
syncLocalValueToMetaIfNoData(settingData, dispatch);
191+
syncLocalValueToMetaIfNoData(newSetting, dispatch);
182192
} else {
183-
const parsedValue = parseSettingValue(settingData.value);
193+
const parsedValue = parseSettingValue(newSetting.value);
184194
dispatch(setSettingValue(settingName, parsedValue));
185195

186196
if (shouldSyncSettingToLS(settingName)) {
187-
setSettingValueToLS(settingName, settingData.value);
197+
setSettingValueToLS(settingName, newSetting.value);
188198
}
189199
}
190200

src/store/reducers/settings/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {parseJson} from '../../../utils/utils';
44
import type {SettingKey} from './constants';
55
import {DEFAULT_USER_SETTINGS, SETTINGS_OPTIONS} from './constants';
66

7-
export function stringifySettingValue<T>(value?: T): string {
7+
export function stringifySettingValue(value?: unknown): string {
88
return typeof value === 'string' ? value : JSON.stringify(value);
99
}
1010
export function parseSettingValue<T>(value?: SettingValue) {

0 commit comments

Comments
 (0)