Skip to content

Commit c07fb36

Browse files
committed
refactor: optimize device settings handlers for better performance
- Refactored the `handleDevChannelChange` and `handleLoopbackOnlyModeChange` functions to use `useCallback` for improved performance and to prevent unnecessary re-renders. - Consolidated the logic for applying loopback-only mode into a separate `applyLoopbackOnlyMode` function, enhancing code clarity and maintainability. - Updated the confirmation flow for enabling loopback-only mode to ensure user warnings are displayed appropriately.
1 parent 7715700 commit c07fb36

File tree

1 file changed

+48
-39
lines changed

1 file changed

+48
-39
lines changed

ui/src/routes/devices.$id.settings.advanced.tsx

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -116,53 +116,62 @@ export default function SettingsAdvancedRoute() {
116116
[send, setDeveloperMode],
117117
);
118118

119-
const handleDevChannelChange = (enabled: boolean) => {
120-
send("setDevChannelState", { enabled }, resp => {
121-
if ("error" in resp) {
122-
notifications.error(
123-
`Failed to set dev channel state: ${resp.error.data || "Unknown error"}`,
124-
);
125-
return;
126-
}
127-
setDevChannel(enabled);
128-
});
129-
};
119+
const handleDevChannelChange = useCallback(
120+
(enabled: boolean) => {
121+
send("setDevChannelState", { enabled }, resp => {
122+
if ("error" in resp) {
123+
notifications.error(
124+
`Failed to set dev channel state: ${resp.error.data || "Unknown error"}`,
125+
);
126+
return;
127+
}
128+
setDevChannel(enabled);
129+
});
130+
},
131+
[send, setDevChannel],
132+
);
130133

131-
const handleLoopbackOnlyModeChange = (enabled: boolean) => {
132-
// If trying to enable loopback-only mode, show warning first
133-
if (enabled) {
134-
setShowLoopbackWarning(true);
135-
} else {
136-
// If disabling, just proceed
137-
applyLoopbackOnlyMode(false);
138-
}
139-
};
134+
const applyLoopbackOnlyMode = useCallback(
135+
(enabled: boolean) => {
136+
send("setLocalLoopbackOnly", { enabled }, resp => {
137+
if ("error" in resp) {
138+
notifications.error(
139+
`Failed to ${enabled ? "enable" : "disable"} loopback-only mode: ${resp.error.data || "Unknown error"}`,
140+
);
141+
return;
142+
}
143+
setLocalLoopbackOnly(enabled);
144+
if (enabled) {
145+
notifications.success(
146+
"Loopback-only mode enabled. Restart your device to apply.",
147+
);
148+
} else {
149+
notifications.success(
150+
"Loopback-only mode disabled. Restart your device to apply.",
151+
);
152+
}
153+
});
154+
},
155+
[send, setLocalLoopbackOnly],
156+
);
140157

141-
const applyLoopbackOnlyMode = (enabled: boolean) => {
142-
send("setLocalLoopbackOnly", { enabled }, resp => {
143-
if ("error" in resp) {
144-
notifications.error(
145-
`Failed to ${enabled ? "enable" : "disable"} loopback-only mode: ${resp.error.data || "Unknown error"}`,
146-
);
147-
return;
148-
}
149-
setLocalLoopbackOnly(enabled);
158+
const handleLoopbackOnlyModeChange = useCallback(
159+
(enabled: boolean) => {
160+
// If trying to enable loopback-only mode, show warning first
150161
if (enabled) {
151-
notifications.success(
152-
"Loopback-only mode enabled. Restart your device to apply.",
153-
);
162+
setShowLoopbackWarning(true);
154163
} else {
155-
notifications.success(
156-
"Loopback-only mode disabled. Restart your device to apply.",
157-
);
164+
// If disabling, just proceed
165+
applyLoopbackOnlyMode(false);
158166
}
159-
});
160-
};
167+
},
168+
[applyLoopbackOnlyMode, setShowLoopbackWarning],
169+
);
161170

162-
const confirmLoopbackModeEnable = () => {
171+
const confirmLoopbackModeEnable = useCallback(() => {
163172
applyLoopbackOnlyMode(true);
164173
setShowLoopbackWarning(false);
165-
};
174+
}, [applyLoopbackOnlyMode, setShowLoopbackWarning]);
166175

167176
return (
168177
<div className="space-y-4">

0 commit comments

Comments
 (0)