import React, { PropsWithChildren, useContext } from 'react'; import { getDisplayName } from '../utils/getDisplayName'; import type { Channel, Mute, StreamChat } from 'stream-chat'; import type { DefaultAttachmentType, DefaultChannelType, DefaultCommandType, DefaultEventType, DefaultMessageType, DefaultReactionType, DefaultUserType, UnknownType, } from '../../types/types'; export type ChatContextValue< At extends UnknownType = DefaultAttachmentType, Ch extends UnknownType = DefaultChannelType, Co extends string = DefaultCommandType, Ev extends UnknownType = DefaultEventType, Me extends UnknownType = DefaultMessageType, Re extends UnknownType = DefaultReactionType, Us extends UnknownType = DefaultUserType, > = { /** * The StreamChat client object * * ``` * import { StreamChat } from 'stream-chat'; * import { Chat } from 'stream-chat-react-native'; * * const client = StreamChat.getInstance('api_key); * await client.connectUser('user_id', 'userToken'); * * * * ``` * * @overrideType StreamChat * */ client: StreamChat; connectionRecovering: boolean; isOnline: boolean; mutedUsers: Mute[]; /** * @param newChannel Channel to set as active. * * @overrideType Function */ setActiveChannel: (newChannel?: Channel) => void; /** * Instance of channel object from stream-chat package. * * Please check the docs around how to create or query channel - https://getstream.io/chat/docs/javascript/creating_channels/?language=javascript * * ``` * import { StreamChat, Channel } from 'stream-chat'; * import { Chat, Channel} from 'stream-chat-react-native'; * * const client = StreamChat.getInstance('api_key'); * await client.connectUser('user_id', 'user_token'); * const channel = client.channel('messaging', 'channel_id'); * await channel.watch(); * ``` * * @overrideType Channel */ channel?: Channel; }; export const ChatContext = React.createContext({} as ChatContextValue); export const ChatProvider = < At extends UnknownType = DefaultAttachmentType, Ch extends UnknownType = DefaultChannelType, Co extends string = DefaultCommandType, Ev extends UnknownType = DefaultEventType, Me extends UnknownType = DefaultMessageType, Re extends UnknownType = DefaultReactionType, Us extends UnknownType = DefaultUserType, >({ children, value, }: PropsWithChildren<{ value: ChatContextValue; }>) => ( {children} ); export const useChatContext = < At extends UnknownType = DefaultAttachmentType, Ch extends UnknownType = DefaultChannelType, Co extends string = DefaultCommandType, Ev extends UnknownType = DefaultEventType, Me extends UnknownType = DefaultMessageType, Re extends UnknownType = DefaultReactionType, Us extends UnknownType = DefaultUserType, >() => useContext(ChatContext) as unknown as ChatContextValue; /** * Typescript currently does not support partial inference so if ChatContext * typing is desired while using the HOC withChatContext the Props for the * wrapped component must be provided as the first generic. */ export const withChatContext = < P extends UnknownType, At extends UnknownType = DefaultAttachmentType, Ch extends UnknownType = DefaultChannelType, Co extends string = DefaultCommandType, Ev extends UnknownType = DefaultEventType, Me extends UnknownType = DefaultMessageType, Re extends UnknownType = DefaultReactionType, Us extends UnknownType = DefaultUserType, >( Component: React.ComponentType

, ): React.FC>> => { const WithChatContextComponent = ( props: Omit>, ) => { const chatContext = useChatContext(); return ; }; WithChatContextComponent.displayName = `WithChatContext${getDisplayName(Component)}`; return WithChatContextComponent; };