Skip to content

Commit ff4dc2c

Browse files
gantoineclaude
andcommitted
fix(discord-presence): apply optimistic toggle state before getState resolves
Merge optimistic toggle updates onto a default state when the panel state is still null, so the Switch reflects the change immediately instead of appearing stuck until the query/subscription updates. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent d2ca638 commit ff4dc2c

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

apps/code/src/renderer/features/settings/components/sections/DiscordSettings.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ import { useSubscription } from "@trpc/tanstack-react-query";
99
import { track } from "@utils/analytics";
1010
import { useEffect, useState } from "react";
1111

12+
// Fallback used for optimistic toggle updates that fire before the initial
13+
// getState query resolves, so the Switch reflects the change immediately
14+
// instead of appearing stuck at its default. The query/subscription reconciles
15+
// the remaining fields (connected, configured) right after.
16+
const DEFAULT_STATE: DiscordPresenceState = {
17+
enabled: false,
18+
connected: false,
19+
configured: false,
20+
showTaskTitle: false,
21+
showRepoName: false,
22+
};
23+
1224
export function DiscordSettings() {
1325
const trpcReact = useTRPC();
1426
const { data } = useQuery(trpcReact.discordPresence.getState.queryOptions());
@@ -46,7 +58,7 @@ export function DiscordSettings() {
4658
new_value: checked,
4759
old_value: enabled,
4860
});
49-
setState((prev) => (prev ? { ...prev, enabled: checked } : prev));
61+
setState((prev) => ({ ...(prev ?? DEFAULT_STATE), enabled: checked }));
5062
setEnabled.mutate({ enabled: checked });
5163
};
5264

@@ -56,7 +68,10 @@ export function DiscordSettings() {
5668
new_value: checked,
5769
old_value: state?.showTaskTitle ?? false,
5870
});
59-
setState((prev) => (prev ? { ...prev, showTaskTitle: checked } : prev));
71+
setState((prev) => ({
72+
...(prev ?? DEFAULT_STATE),
73+
showTaskTitle: checked,
74+
}));
6075
setShowTaskTitle.mutate({ value: checked });
6176
};
6277

@@ -66,7 +81,7 @@ export function DiscordSettings() {
6681
new_value: checked,
6782
old_value: state?.showRepoName ?? false,
6883
});
69-
setState((prev) => (prev ? { ...prev, showRepoName: checked } : prev));
84+
setState((prev) => ({ ...(prev ?? DEFAULT_STATE), showRepoName: checked }));
7085
setShowRepoName.mutate({ value: checked });
7186
};
7287

0 commit comments

Comments
 (0)