2025-02-09 13:41:08 +08:00
|
|
|
import { MutableRefObject, useCallback } from 'react';
|
|
|
|
import { IBusterChat, IBusterChatMessage } from '../interfaces';
|
2025-02-11 07:43:38 +08:00
|
|
|
import { useMemoizedFn } from 'ahooks';
|
2025-02-09 13:41:08 +08:00
|
|
|
|
|
|
|
export const useChatSelectors = ({
|
|
|
|
isPending,
|
|
|
|
chatsRef,
|
|
|
|
chatsMessagesRef
|
|
|
|
}: {
|
|
|
|
isPending: boolean;
|
|
|
|
chatsRef: MutableRefObject<Record<string, IBusterChat>>;
|
|
|
|
chatsMessagesRef: MutableRefObject<Record<string, IBusterChatMessage>>;
|
|
|
|
}) => {
|
2025-02-11 07:43:38 +08:00
|
|
|
const getChatMemoized = useMemoizedFn((chatId: string) => {
|
|
|
|
return chatsRef.current[chatId];
|
|
|
|
});
|
|
|
|
|
2025-02-09 13:41:08 +08:00
|
|
|
const getChatMessages = useCallback(
|
|
|
|
(chatId: string): IBusterChatMessage[] => {
|
|
|
|
const chatMessageIds = chatsRef.current[chatId].messages || [];
|
|
|
|
return chatMessageIds.map((messageId) => chatsMessagesRef.current[messageId]);
|
|
|
|
},
|
|
|
|
[chatsMessagesRef, isPending, chatsRef]
|
|
|
|
);
|
|
|
|
|
|
|
|
const getChatMessage = useCallback(
|
|
|
|
(messageId: string): IBusterChatMessage => {
|
|
|
|
return chatsMessagesRef.current[messageId];
|
|
|
|
},
|
|
|
|
[chatsMessagesRef, isPending]
|
|
|
|
);
|
|
|
|
|
2025-02-11 07:43:38 +08:00
|
|
|
const getChatMessagesMemoized = useMemoizedFn((chatId: string) => {
|
|
|
|
return getChatMessages(chatId);
|
|
|
|
});
|
|
|
|
|
|
|
|
const getChatMessageMemoized = useMemoizedFn((messageId: string) => {
|
|
|
|
return getChatMessage(messageId);
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
getChatMemoized,
|
|
|
|
getChatMessages,
|
|
|
|
getChatMessage,
|
|
|
|
getChatMessagesMemoized,
|
|
|
|
getChatMessageMemoized
|
|
|
|
};
|
2025-02-09 13:41:08 +08:00
|
|
|
};
|