import React, { PropsWithChildren, useContext } from 'react'; import { FlatListProps } from 'react-native'; import { Channel, Thread } from 'stream-chat'; import { ThreadType } from '../threadContext/ThreadContext'; import { DEFAULT_BASE_CONTEXT_VALUE } from '../utils/defaultBaseContextValue'; import { isTestEnvironment } from '../utils/isTestEnvironment'; export type ThreadsContextValue = { isFocused: boolean; isLoading: boolean; isLoadingNext: boolean; threads: Thread[]; additionalFlatListProps?: Partial>; loadMore?: () => Promise; onThreadSelect?: (thread: ThreadType, channel: Channel) => void; }; export const ThreadsContext = React.createContext( DEFAULT_BASE_CONTEXT_VALUE as ThreadsContextValue, ); export const ThreadsProvider = ({ children, value, }: PropsWithChildren<{ value: ThreadsContextValue; }>) => ( {children} ); export const useThreadsContext = () => { const contextValue = useContext(ThreadsContext) as unknown as ThreadsContextValue; if (contextValue === DEFAULT_BASE_CONTEXT_VALUE && !isTestEnvironment()) { throw new Error( 'The useThreadsContext hook was called outside of the ThreadsContext provider. Make sure you have configured the ThreadList component correctly - https://getstream.io/chat/docs/sdk/react-native/ui-components/thread-list/', ); } return contextValue; };