global update and global fix
This commit is contained in:
44
apps/frontend/hooks/use-chat-typing.ts
Normal file
44
apps/frontend/hooks/use-chat-typing.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
'use client';
|
||||
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
|
||||
type Typer = { userId: string; userName: string; expiresAt: number };
|
||||
|
||||
export function useChatTyping(currentUserId?: string) {
|
||||
const [typers, setTypers] = useState<Typer[]>([]);
|
||||
const timerRef = useRef<number | null>(null);
|
||||
|
||||
const pruneTypers = useCallback(() => {
|
||||
const now = Date.now();
|
||||
setTypers((current) => current.filter((item) => item.expiresAt > now));
|
||||
}, []);
|
||||
|
||||
const registerTyper = useCallback(
|
||||
(userId: string, userName: string) => {
|
||||
if (!userId || userId === currentUserId) return;
|
||||
const expiresAt = Date.now() + 3000;
|
||||
setTypers((current) => {
|
||||
const without = current.filter((item) => item.userId !== userId);
|
||||
return [...without, { userId, userName, expiresAt }];
|
||||
});
|
||||
},
|
||||
[currentUserId]
|
||||
);
|
||||
|
||||
const clearTypers = useCallback(() => {
|
||||
setTypers([]);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
timerRef.current = window.setInterval(pruneTypers, 500);
|
||||
return () => {
|
||||
if (timerRef.current) window.clearInterval(timerRef.current);
|
||||
};
|
||||
}, [pruneTypers]);
|
||||
|
||||
return {
|
||||
typers: typers.map(({ userId, userName }) => ({ userId, userName })),
|
||||
registerTyper,
|
||||
clearTypers
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user