mirror of
https://github.com/array-in-a-matrix/xinny.git
synced 2024-06-11 17:07:12 -04:00
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
|
import { MatrixClient } from 'matrix-js-sdk';
|
||
|
import { useMemo, useRef } from 'react';
|
||
|
|
||
|
type TypingStatusUpdater = (typing: boolean) => void;
|
||
|
|
||
|
const TYPING_TIMEOUT_MS = 5000; // 5 seconds
|
||
|
|
||
|
export const useTypingStatusUpdater = (mx: MatrixClient, roomId: string): TypingStatusUpdater => {
|
||
|
const statusSentTsRef = useRef<number>(0);
|
||
|
|
||
|
const sendTypingStatus: TypingStatusUpdater = useMemo(() => {
|
||
|
statusSentTsRef.current = 0;
|
||
|
return (typing) => {
|
||
|
if (typing) {
|
||
|
if (Date.now() - statusSentTsRef.current < TYPING_TIMEOUT_MS) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
mx.sendTyping(roomId, true, TYPING_TIMEOUT_MS);
|
||
|
const sentTs = Date.now();
|
||
|
statusSentTsRef.current = sentTs;
|
||
|
|
||
|
// Don't believe server will timeout typing status;
|
||
|
// Clear typing status after timeout if already not;
|
||
|
setTimeout(() => {
|
||
|
if (statusSentTsRef.current === sentTs) {
|
||
|
mx.sendTyping(roomId, false, TYPING_TIMEOUT_MS);
|
||
|
statusSentTsRef.current = 0;
|
||
|
}
|
||
|
}, TYPING_TIMEOUT_MS);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (Date.now() - statusSentTsRef.current < TYPING_TIMEOUT_MS) {
|
||
|
mx.sendTyping(roomId, false, TYPING_TIMEOUT_MS);
|
||
|
}
|
||
|
statusSentTsRef.current = 0;
|
||
|
};
|
||
|
}, [mx, roomId]);
|
||
|
|
||
|
return sendTypingStatus;
|
||
|
};
|