2024-05-31 19:49:46 +05:30
|
|
|
import { useAtomValue } from 'jotai';
|
2024-07-08 16:57:10 +05:30
|
|
|
import { selectAtom } from 'jotai/utils';
|
|
|
|
|
import { useCallback } from 'react';
|
|
|
|
|
import {
|
|
|
|
|
IRoomIdToTypingMembers,
|
|
|
|
|
TypingReceipt,
|
|
|
|
|
roomIdToTypingMembersAtom,
|
|
|
|
|
} from '../state/typingMembers';
|
|
|
|
|
|
|
|
|
|
const typingReceiptEqual = (a: TypingReceipt, b: TypingReceipt): boolean =>
|
|
|
|
|
a.userId === b.userId && a.ts === b.ts;
|
|
|
|
|
|
|
|
|
|
const equalTypingMembers = (x: TypingReceipt[], y: TypingReceipt[]): boolean => {
|
|
|
|
|
if (x.length !== y.length) return false;
|
|
|
|
|
return x.every((a, i) => typingReceiptEqual(a, y[i]));
|
|
|
|
|
};
|
2024-05-31 19:49:46 +05:30
|
|
|
|
|
|
|
|
export const useRoomTypingMember = (roomId: string) => {
|
2024-07-08 16:57:10 +05:30
|
|
|
const selector = useCallback(
|
|
|
|
|
(roomToTyping: IRoomIdToTypingMembers) => roomToTyping.get(roomId) ?? [],
|
|
|
|
|
[roomId]
|
2024-05-31 19:49:46 +05:30
|
|
|
);
|
2024-07-08 16:57:10 +05:30
|
|
|
|
|
|
|
|
const typing = useAtomValue(selectAtom(roomIdToTypingMembersAtom, selector, equalTypingMembers));
|
2024-05-31 19:49:46 +05:30
|
|
|
return typing;
|
|
|
|
|
};
|