Skip to content

Commit a051e4e

Browse files
RaitP1juliusmarminge
authored andcommitted
fix(web): replace useEffectEvent with useCallback for option selection
- Use useCallback instead of useEffectEvent to select an option. - Add handleOptionSelection to the keydown effect's dependency list.
1 parent 21b6fb5 commit a051e4e

1 file changed

Lines changed: 19 additions & 16 deletions

File tree

‎apps/web/src/components/chat/ComposerPendingUserInputPanel.tsx‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type ApprovalRequestId } from "@t3tools/contracts";
2-
import { memo, useEffect, useEffectEvent, useRef, useState } from "react";
2+
import { memo, useCallback, useEffect, useRef, useState } from "react";
33
import { type PendingUserInput } from "../../session-logic";
44
import {
55
derivePendingUserInputProgress,
@@ -109,21 +109,24 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
109109
};
110110
}, []);
111111

112-
const handleOptionSelection = useEffectEvent((questionId: string, optionLabel: string) => {
113-
if (activeQuestion?.multiSelect) {
112+
const handleOptionSelection = useCallback(
113+
(questionId: string, optionLabel: string) => {
114+
if (activeQuestion?.multiSelect) {
115+
onToggleOption(questionId, optionLabel);
116+
return;
117+
}
118+
setOptimisticSingleSelect({ questionId, optionLabel });
114119
onToggleOption(questionId, optionLabel);
115-
return;
116-
}
117-
setOptimisticSingleSelect({ questionId, optionLabel });
118-
onToggleOption(questionId, optionLabel);
119-
if (autoAdvanceTimerRef.current !== null) {
120-
window.clearTimeout(autoAdvanceTimerRef.current);
121-
}
122-
autoAdvanceTimerRef.current = window.setTimeout(() => {
123-
autoAdvanceTimerRef.current = null;
124-
onAdvanceRef.current();
125-
}, 200);
126-
});
120+
if (autoAdvanceTimerRef.current !== null) {
121+
window.clearTimeout(autoAdvanceTimerRef.current);
122+
}
123+
autoAdvanceTimerRef.current = window.setTimeout(() => {
124+
autoAdvanceTimerRef.current = null;
125+
onAdvanceRef.current();
126+
}, 200);
127+
},
128+
[activeQuestion, onToggleOption],
129+
);
127130

128131
// Keyboard shortcut: number keys 1-9 select corresponding options when focus is
129132
// outside editable fields. Multi-select prompts toggle options in place; single-
@@ -154,7 +157,7 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
154157
};
155158
document.addEventListener("keydown", handler);
156159
return () => document.removeEventListener("keydown", handler);
157-
}, [activeQuestion, isCollapsed, isResponding]);
160+
}, [activeQuestion, handleOptionSelection, isCollapsed, isResponding]);
158161

159162
if (!activeQuestion) {
160163
return null;

0 commit comments

Comments
 (0)