60c2c97ba6
A11y: - Add aria-label Close to RoomTopicViewer, ImagePackView close buttons - Add aria-label Cancel to LeaveRoomPrompt cancel button - Add aria-label to Send, Reply, Thread, Edit, React, Search, Mute, Download buttons - Fix aria-pressed -> aria-expanded + aria-haspopup on menu anchor triggers - Add aria-label to username/password auth inputs BUG-21: Add useAlive unmount guard to handleGifSelect error path in RoomInput BUG-22: Fix typing status timer accumulation with typingTimerRef Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import { MatrixClient } from 'matrix-js-sdk';
|
|
import { useMemo, useRef } from 'react';
|
|
import { TYPING_TIMEOUT_MS } from '../state/typingMembers';
|
|
|
|
type TypingStatusUpdater = (typing: boolean) => void;
|
|
|
|
export const useTypingStatusUpdater = (mx: MatrixClient, roomId: string): TypingStatusUpdater => {
|
|
const statusSentTsRef = useRef<number>(0);
|
|
const typingTimerRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
|
|
|
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;
|
|
|
|
// Cancel any previous pending timeout before scheduling a new one
|
|
if (typingTimerRef.current !== undefined) clearTimeout(typingTimerRef.current);
|
|
typingTimerRef.current = setTimeout(() => {
|
|
typingTimerRef.current = undefined;
|
|
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;
|
|
};
|