Skip to content

Commit 8c56c9c

Browse files
committed
refactor: consolidate voice input useEffects
Merge command palette toggle and global recording keybinds into single useEffect with shared cleanup.
1 parent fe15e4d commit 8c56c9c

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

src/browser/components/ChatInput/index.tsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -480,11 +480,11 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
480480
window.removeEventListener(CUSTOM_EVENTS.THINKING_LEVEL_TOAST, handler as EventListener);
481481
}, [variant, props, setToast]);
482482

483-
// Listen for voice input toggle from command palette
483+
// Voice input: command palette toggle + global recording keybinds
484484
useEffect(() => {
485485
if (!voiceInput.shouldShowUI) return;
486486

487-
const handler = () => {
487+
const handleToggle = () => {
488488
if (!voiceInput.isApiKeySet) {
489489
setToast({
490490
id: Date.now().toString(),
@@ -495,16 +495,10 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
495495
}
496496
voiceInput.toggle();
497497
};
498-
window.addEventListener(CUSTOM_EVENTS.TOGGLE_VOICE_INPUT, handler as EventListener);
499-
return () =>
500-
window.removeEventListener(CUSTOM_EVENTS.TOGGLE_VOICE_INPUT, handler as EventListener);
501-
}, [voiceInput, setToast]);
502-
503-
// Global keybinds during recording (work regardless of focus)
504-
useEffect(() => {
505-
if (voiceInput.state !== "recording") return;
506498

507-
const handler = (e: KeyboardEvent) => {
499+
// Global keybinds only active during recording
500+
const handleKeyDown = (e: KeyboardEvent) => {
501+
if (voiceInput.state !== "recording") return;
508502
if (e.key === " ") {
509503
e.preventDefault();
510504
voiceInput.stop({ send: true });
@@ -513,9 +507,14 @@ export const ChatInput: React.FC<ChatInputProps> = (props) => {
513507
voiceInput.cancel();
514508
}
515509
};
516-
window.addEventListener("keydown", handler);
517-
return () => window.removeEventListener("keydown", handler);
518-
}, [voiceInput]);
510+
511+
window.addEventListener(CUSTOM_EVENTS.TOGGLE_VOICE_INPUT, handleToggle as EventListener);
512+
window.addEventListener("keydown", handleKeyDown);
513+
return () => {
514+
window.removeEventListener(CUSTOM_EVENTS.TOGGLE_VOICE_INPUT, handleToggle as EventListener);
515+
window.removeEventListener("keydown", handleKeyDown);
516+
};
517+
}, [voiceInput, setToast]);
519518

520519
// Auto-focus chat input when workspace changes (workspace only)
521520
const workspaceIdForFocus = variant === "workspace" ? props.workspaceId : null;

0 commit comments

Comments
 (0)