Skip to content

Commit 619f8b3

Browse files
committed
fix: Fix auto focus forcing screens to jump
1 parent 0052b3f commit 619f8b3

File tree

6 files changed

+44
-17
lines changed

6 files changed

+44
-17
lines changed

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"files": [
1616
"./dist"
1717
],
18-
"version": "0.3.1",
18+
"version": "0.3.2",
1919
"description": "A generic LLM connector for integrating Large Language Models (LLMs) in React ChatBotify!",
2020
"type": "module",
2121
"main": "./dist/index.cjs",
@@ -48,7 +48,7 @@
4848
"peerDependencies": {
4949
"@mlc-ai/web-llm": "^0.2.78",
5050
"react": ">=16.14.0",
51-
"react-chatbotify": "^2.0.0-beta.38",
51+
"react-chatbotify": "^2.0.0-beta.39",
5252
"react-dom": ">=16.14.0"
5353
},
5454
"devDependencies": {

src/core/useRcbPlugin.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const useRcbPlugin = (pluginConfig?: PluginConfig): ReturnType<Plugin> => {
3838
const { messages, injectMessage, simulateStreamMessage, streamMessage, endStreamMessage } = useMessages();
3939
const { goToPath } = usePaths();
4040
const { toggleTextAreaDisabled, focusTextArea } = useTextArea();
41-
const { toggleIsBotTyping } = useChatWindow();
41+
const { toggleIsBotTyping, getIsChatBotVisible } = useChatWindow();
4242

4343
const mergedPluginConfig = { ...DefaultPluginConfig, ...(pluginConfig ?? {}) };
4444

@@ -81,6 +81,7 @@ const useRcbPlugin = (pluginConfig?: PluginConfig): ReturnType<Plugin> => {
8181
toggleIsBotTyping,
8282
focusTextArea,
8383
goToPath,
84+
getIsChatBotVisible,
8485
};
8586

8687
// handles pre-processing and post-processing of blocks.

src/hooks/useMessageHandler.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,19 @@ const useMessageHandler = (
4242
toggleIsBotTyping: (active?: boolean) => void;
4343
focusTextArea: () => void;
4444
goToPath: (path: string) => void;
45+
getIsChatBotVisible: () => boolean;
4546
}
4647
) => {
4748
const { messagesRef, outputTypeRef, onUserMessageRef, onKeyDownRef, errorMessageRef } = refs;
48-
const { injectMessage, simulateStreamMessage, toggleTextAreaDisabled, toggleIsBotTyping, goToPath, focusTextArea } =
49-
actions;
49+
const {
50+
injectMessage,
51+
simulateStreamMessage,
52+
toggleTextAreaDisabled,
53+
toggleIsBotTyping,
54+
goToPath,
55+
focusTextArea,
56+
getIsChatBotVisible,
57+
} = actions;
5058

5159
// controller to abort streaming responses if required
5260
const abortControllerRef = useRef<AbortController | null>(null);
@@ -93,7 +101,9 @@ const useMessageHandler = (
93101
toggleIsBotTyping(false);
94102
toggleTextAreaDisabled(false);
95103
setTimeout(() => {
96-
focusTextArea();
104+
if (getIsChatBotVisible()) {
105+
focusTextArea();
106+
}
97107
});
98108
console.error('LLM prompt failed', err);
99109
if (outputTypeRef.current === 'full') {

src/hooks/useProcessBlock.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,18 @@ const useProcessBlock = (
3131
toggleIsBotTyping: (active?: boolean) => void;
3232
focusTextArea: () => void;
3333
goToPath: (path: string) => void;
34+
getIsChatBotVisible: () => boolean;
3435
}
3536
) => {
3637
const { outputTypeRef } = refs;
37-
const { toggleTextAreaDisabled, toggleIsBotTyping, focusTextArea, injectMessage, simulateStreamMessage } = actions;
38+
const {
39+
toggleTextAreaDisabled,
40+
toggleIsBotTyping,
41+
focusTextArea,
42+
injectMessage,
43+
simulateStreamMessage,
44+
getIsChatBotVisible,
45+
} = actions;
3846

3947
/**
4048
* Handles blocking of pre-processing and post-processing of block for full custom control.
@@ -67,11 +75,13 @@ const useProcessBlock = (
6775
toggleIsBotTyping(false);
6876
toggleTextAreaDisabled(false);
6977
setTimeout(() => {
70-
focusTextArea();
78+
if (getIsChatBotVisible()) {
79+
focusTextArea();
80+
}
7181
});
7282
}
7383
},
74-
[toggleIsBotTyping, toggleTextAreaDisabled, focusTextArea]
84+
[toggleIsBotTyping, toggleTextAreaDisabled, focusTextArea, getIsChatBotVisible]
7585
);
7686

7787
// adds required events for block processing

src/utils/promptHandler.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const handlePrompt = async (
4646
toggleIsBotTyping: (active?: boolean) => void;
4747
focusTextArea: () => void;
4848
goToPath: (path: string) => void;
49+
getIsChatBotVisible: () => boolean;
4950
},
5051
opts: { signal?: AbortSignal } = {}
5152
): Promise<void> => {
@@ -61,6 +62,7 @@ const handlePrompt = async (
6162
injectMessage,
6263
streamMessage,
6364
endStreamMessage,
65+
getIsChatBotVisible,
6466
} = actions;
6567

6668
const rawStream = refs.providerRef.current.sendMessages(messages);
@@ -80,7 +82,9 @@ const handlePrompt = async (
8082
injectMessage(outputContent);
8183
setTimeout(() => {
8284
toggleTextAreaDisabled(false);
83-
focusTextArea();
85+
if (getIsChatBotVisible()) {
86+
focusTextArea();
87+
}
8488
});
8589
} else {
8690
const formattedStream = formatStream(speakAndForward(rawStream, speakAudio), outputType, outputSpeed);
@@ -103,7 +107,9 @@ const handlePrompt = async (
103107
endStreamMessage();
104108
setTimeout(() => {
105109
toggleTextAreaDisabled(false);
106-
focusTextArea();
110+
if (getIsChatBotVisible()) {
111+
focusTextArea();
112+
}
107113
});
108114
}
109115
};

0 commit comments

Comments
 (0)