import React, { useRef, useTransition } from 'react'; import { createContext, ContextSelector, useContextSelector } from '@fluentui/react-context-selector'; import type { BusterChat } from '@/api/asset_interfaces'; import type { IBusterChat, IBusterChatMessage } from '../interfaces'; import { useChatSubscriptions } from './useChatSubscriptions'; import { useChatAssosciations } from './useChatAssosciations'; import { useChatSelectors } from './useChatSelectors'; import { useChatUpdate } from './useChatUpdate'; export const useBusterChat = () => { const [isPending, startTransition] = useTransition(); const chatsRef = useRef>({}); const chatsMessagesRef = useRef>({}); const chatSubscriptions = useChatSubscriptions({ chatsRef, chatsMessagesRef, startTransition }); const chatAssociations = useChatAssosciations(); const chatSelectors = useChatSelectors({ chatsRef, chatsMessagesRef, isPending }); const chatUpdate = useChatUpdate({ chatsRef, chatsMessagesRef, startTransition }); return { chats: chatsRef.current, chatsMessages: chatsMessagesRef.current, ...chatSubscriptions, ...chatAssociations, ...chatSelectors, ...chatUpdate }; }; const BusterChat = createContext>( {} as ReturnType ); export const ChatProvider: React.FC<{ children: React.ReactNode; }> = ({ children }) => { const value = useBusterChat(); return {children}; }; export const useBusterChatContextSelector = ( selector: ContextSelector, T> ) => useContextSelector(BusterChat, selector);