Skip to content

Commit 90eb50a

Browse files
committed
fix(settings): send only the changed settings in v2 settings PUTs (TM-974)
1 parent bcf0560 commit 90eb50a

15 files changed

Lines changed: 130 additions & 98 deletions

‎src/App.vue‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -576,13 +576,9 @@
576576
577577
if (workspace.is_local) {
578578
// local/install.ts activates it and keeps the server default untouched.
579-
const updatedSettings = settings.map((setting) => ({
580-
id: setting.id,
581-
value:
582-
setting.key === 'current_workspace'
583-
? workspace.id
584-
: setting.value,
585-
}));
579+
const updatedSettings = settings
580+
.filter((setting) => setting.key === 'current_workspace')
581+
.map((setting) => ({ id: setting.id, value: workspace.id }));
586582
const updatedUser = await updateUserSettingsV2(updatedSettings);
587583
this.$store.commit('setUser', updatedUser);
588584
} else {

‎src/actions/tmgr/__tests__/agentUsage.test.ts‎

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import {
4141
listAgentUsage,
4242
setAgentUsageSync,
4343
uploadRows,
44-
withCurrentAgentUsageSync,
4544
} from '../agentUsage';
4645

4746
const user = (value: string | null) => ({
@@ -72,12 +71,11 @@ describe('agent usage sync setting', () => {
7271
expect(isAgentUsageSyncOn()).toBe(false);
7372
});
7473

75-
it('puts every setting with the new value for this one', async () => {
74+
it('puts only the agent usage sync value', async () => {
7675
state.user = user('0');
7776
put.mockResolvedValue({ data: { data: user('1') } });
7877
await setAgentUsageSync(true);
7978
expect(put).toHaveBeenCalledWith('v2/user/settings', [
80-
{ id: 5, value: 56 },
8179
{ id: 9, value: '1' },
8280
]);
8381
expect(isAgentUsageSyncOn()).toBe(true);
@@ -166,24 +164,3 @@ describe('uploads', () => {
166164
expect(rows[1]).toMatchObject({ task: '#2', cost: null });
167165
});
168166
});
169-
170-
describe('withCurrentAgentUsageSync', () => {
171-
it('replaces a stale cached value with the current one', () => {
172-
state.user = user('1');
173-
const stale = [
174-
{ id: 5, value: 56 },
175-
{ id: 9, value: '0' },
176-
];
177-
expect(withCurrentAgentUsageSync(stale)).toEqual([
178-
{ id: 5, value: 56 },
179-
{ id: 9, value: '1' },
180-
]);
181-
expect(stale[1].value).toBe('0');
182-
});
183-
184-
it('leaves the list alone without the setting', () => {
185-
state.user = user(null);
186-
const list = [{ id: 5, value: 56 }];
187-
expect(withCurrentAgentUsageSync(list)).toBe(list);
188-
});
189-
});

‎src/actions/tmgr/__tests__/commentAiReply.test.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ test('reads the stored value of the user setting', () => {
5757
expect(isCommentAiReplyOn()).toBe(false);
5858
});
5959

60-
test('saves the full settings list with only the AI reply value changed', async () => {
60+
test('saves only the AI reply value', async () => {
6161
(store.state as any).user = userWith([
6262
{ id: 6, key: 'dark_mode', value: '1' },
6363
{ id: 20, key: 'comment_ai_reply', value: '0' },
@@ -71,7 +71,6 @@ test('saves the full settings list with only the AI reply value changed', async
7171
expect(isCommentAiReplyOn()).toBe(true);
7272
await pending;
7373
expect($axios.put).toHaveBeenCalledWith('v2/user/settings', [
74-
{ id: 6, value: '1' },
7574
{ id: 20, value: '1' },
7675
]);
7776
expect((store.state as any).user).toEqual(saved);

‎src/actions/tmgr/__tests__/persistThemeSettings.test.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ beforeEach(() => {
2020
($axios.put as jest.Mock).mockResolvedValue({ data: { data: {} } });
2121
});
2222

23-
test('never sends a settings item without a numeric id', async () => {
23+
test('sends only the theme and color scheme, never other keys or items without an id', async () => {
2424
(store.state as any).user.settings = [
2525
{ id: 6, key: 'theme', value: 'default' },
2626
{ id: 7, key: 'colorScheme', value: 'system' },
@@ -31,6 +31,5 @@ test('never sends a settings item without a numeric id', async () => {
3131
expect($axios.put).toHaveBeenCalledWith('v2/user/settings', [
3232
{ id: 6, value: 'ocean' },
3333
{ id: 7, value: 'dark' },
34-
{ id: 9, value: 1 },
3534
]);
3635
});

‎src/actions/tmgr/agentUsage.ts‎

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,14 @@ export const isAgentUsageSyncAvailable = (): boolean =>
3939
export const isAgentUsageSyncOn = (): boolean =>
4040
isOn(store.getters.userSettingByKey(AGENT_USAGE_SYNC_KEY)?.value);
4141

42-
export const withCurrentAgentUsageSync = <
43-
T extends { id: number; value?: unknown },
44-
>(
45-
settings: T[],
46-
): T[] => {
47-
const current = store.getters.userSettingByKey(AGENT_USAGE_SYNC_KEY);
48-
if (!current) return settings;
49-
return settings.map((s) =>
50-
s.id === current.id ? { ...s, value: current.value } : s,
51-
);
52-
};
53-
5442
export const setAgentUsageSync = async (on: boolean): Promise<void> => {
5543
const user = (store.state as any).user;
5644
const setting = store.getters.userSettingByKey(AGENT_USAGE_SYNC_KEY);
5745
if (!setting || !Array.isArray(user?.settings)) {
5846
throw new Error('Agent usage sync is not available on this server yet.');
5947
}
6048
const value = on ? '1' : '0';
61-
const payload = user.settings.map((s: any) => ({
62-
id: s.id,
63-
value: s.key === AGENT_USAGE_SYNC_KEY ? value : s.value,
64-
}));
49+
const payload = [{ id: setting.id, value }];
6550
store.commit('setUser', {
6651
...user,
6752
settings: withValue(user.settings, value),

‎src/actions/tmgr/commentAiReply.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ export const setCommentAiReply = async (on: boolean): Promise<void> => {
2727
return;
2828
}
2929
const value = on ? '1' : '0';
30-
const payload = user.settings.map((s: any) => ({
31-
id: s.id,
32-
value: s.key === COMMENT_AI_REPLY_KEY ? value : s.value,
33-
}));
30+
const payload = [{ id: setting.id, value }];
3431
store.commit('setUser', {
3532
...user,
3633
settings: withValue(user.settings, value),

‎src/actions/tmgr/user.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,10 @@ export const persistThemeSettings = async (
216216
: [];
217217
const payload = current
218218
.filter((s: any) => typeof s.id === 'number')
219-
.map((s: any) => {
220-
if (s.key === 'theme') return { id: s.id, value: theme };
221-
if (s.key === 'colorScheme') return { id: s.id, value: colorScheme };
222-
return { id: s.id, value: s.value };
219+
.flatMap((s: any) => {
220+
if (s.key === 'theme') return [{ id: s.id, value: theme }];
221+
if (s.key === 'colorScheme') return [{ id: s.id, value: colorScheme }];
222+
return [];
223223
});
224224
const updatedUser = await updateUserSettingsV2(payload);
225225
store.commit('setUser', updatedUser);

‎src/components/general/AccountDropdown.vue‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,12 @@
145145
});
146146
147147
async function onSelectChange(workspaceId: number) {
148-
const settingsWithUpdatedWorkspace = user.value.settings.map((setting) => {
149-
if (setting.key === 'current_workspace') {
148+
const settingsWithUpdatedWorkspace = user.value.settings
149+
.filter((setting) => setting.key === 'current_workspace')
150+
.map((setting) => {
150151
setting.value = workspaceId;
151-
}
152-
153-
return {
154-
id: setting.id,
155-
value: setting.value,
156-
};
157-
});
152+
return { id: setting.id, value: setting.value };
153+
});
158154
159155
const updatedUser = await updateUserSettingsV2(
160156
settingsWithUpdatedWorkspace,

‎src/components/general/CustomSidebar.vue‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -450,15 +450,9 @@
450450
451451
if (workspace.is_local) {
452452
// local/install.ts activates it and keeps the server default untouched.
453-
const settingsWithUpdatedWorkspace = user.value?.settings.map(
454-
(setting) => ({
455-
id: setting.id,
456-
value:
457-
setting.key === 'current_workspace'
458-
? workspace.id
459-
: setting.value,
460-
}),
461-
);
453+
const settingsWithUpdatedWorkspace = user.value?.settings
454+
.filter((setting) => setting.key === 'current_workspace')
455+
.map((setting) => ({ id: setting.id, value: workspace.id }));
462456
const updatedUser = await updateUserSettingsV2(
463457
settingsWithUpdatedWorkspace,
464458
);

‎src/components/general/WorkspaceSelect.vue‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,12 @@
2828
// Methods
2929
const handleWorkspaceChange = async (workspaceId: number) => {
3030
try {
31-
const settingsWithUpdatedWorkspace = user.value.settings.map(
32-
(setting) => {
33-
if (setting.key === 'current_workspace') {
34-
setting.value = workspaceId;
35-
}
36-
return {
37-
id: setting.id,
38-
value: setting.value,
39-
};
40-
},
41-
);
31+
const settingsWithUpdatedWorkspace = user.value.settings
32+
.filter((setting) => setting.key === 'current_workspace')
33+
.map((setting) => {
34+
setting.value = workspaceId;
35+
return { id: setting.id, value: setting.value };
36+
});
4237
4338
const updatedUser = await updateUserSettingsV2(
4439
settingsWithUpdatedWorkspace,

0 commit comments

Comments
 (0)