({
isUserTyping,
setUserTyping,
userTyping,
currentUser,
}: {
isUserTyping: boolean;
setUserTyping: React.Dispatch<React.SetStateAction<boolean>>;
userTyping: ({
typing,
username,
}: {
typing: boolean;
username: string;
}) => void;
currentUser: SerializedUser | null;
})
| 35 | } |
| 36 | |
| 37 | export function TypingFunctions({ |
| 38 | isUserTyping, |
| 39 | setUserTyping, |
| 40 | userTyping, |
| 41 | currentUser, |
| 42 | }: { |
| 43 | isUserTyping: boolean; |
| 44 | setUserTyping: React.Dispatch<React.SetStateAction<boolean>>; |
| 45 | userTyping: ({ |
| 46 | typing, |
| 47 | username, |
| 48 | }: { |
| 49 | typing: boolean; |
| 50 | username: string; |
| 51 | }) => void; |
| 52 | currentUser: SerializedUser | null; |
| 53 | }) { |
| 54 | let typingTimer: NodeJS.Timeout; |
| 55 | |
| 56 | const userStartsTyping = function () { |
| 57 | if (isUserTyping) { |
| 58 | return; |
| 59 | } |
| 60 | setUserTyping(true); |
| 61 | userTyping({ typing: true, username: currentUser?.displayName! }); |
| 62 | }; |
| 63 | |
| 64 | const userStopsTyping = function () { |
| 65 | clearTimeout(typingTimer); |
| 66 | setUserTyping(false); |
| 67 | userTyping({ typing: false, username: currentUser?.displayName! }); |
| 68 | }; |
| 69 | |
| 70 | const onKeyDown = () => { |
| 71 | userStartsTyping(); |
| 72 | clearTimeout(typingTimer); |
| 73 | }; |
| 74 | |
| 75 | const onKeyUp = () => { |
| 76 | clearTimeout(typingTimer); |
| 77 | typingTimer = setTimeout(userStopsTyping, typingTimeout); |
| 78 | }; |
| 79 | |
| 80 | return { onKeyDown, onKeyUp }; |
| 81 | } |
nothing calls this directly
no outgoing calls
no test coverage detected