Skip to content

Commit

Permalink
runfix: Avoid sending typing stop when user has not typed at all (#15985
Browse files Browse the repository at this point in the history
)
  • Loading branch information
atomrc authored Oct 12, 2023
1 parent 8f49789 commit 3f8fbd1
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/script/components/InputBar/hooks/useTypingIndicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
*/

import {useEffect, useRef} from 'react';
import {useCallback, useEffect, useRef} from 'react';

import {TYPING_TIMEOUT} from '../components/TypingIndicator';

Expand All @@ -29,6 +29,17 @@ type TypingIndicatorProps = {

export function useTypingIndicator({text, isEnabled, onTypingChange}: TypingIndicatorProps) {
const hasHitKeyboard = useRef(false);
const isTypingRef = useRef(false);

const setTyping = useCallback(
(isTyping: boolean) => {
if (isTyping !== isTypingRef.current) {
isTypingRef.current = isTyping;
onTypingChange(isTyping);
}
},
[onTypingChange],
);

useEffect(() => {
if (!hasHitKeyboard.current && isEnabled) {
Expand All @@ -50,14 +61,14 @@ export function useTypingIndicator({text, isEnabled, onTypingChange}: TypingIndi
}

if (text.length > 0) {
onTypingChange(true);
timerId = window.setTimeout(() => onTypingChange(false), TYPING_TIMEOUT);
setTyping(true);
timerId = window.setTimeout(() => setTyping(false), TYPING_TIMEOUT);
} else {
onTypingChange(false);
setTyping(false);
}

return () => window.clearTimeout(timerId);
}, [text, onTypingChange]);
}, [text, setTyping]);

useEffect(() => () => onTypingChange(false), [onTypingChange]);
useEffect(() => () => setTyping(false), [setTyping]);
}

0 comments on commit 3f8fbd1

Please sign in to comment.