1- import React , { PropsWithChildren , useContext , useEffect , useState } from 'react' ;
1+ import React , { PropsWithChildren , useContext , useEffect , useMemo , useState } from 'react' ;
2+
3+ import { Attachment } from 'stream-chat' ;
24
35import { ImageGalleryStateStore } from '../../state-store/image-gallery-state-store' ;
4- import type { UnknownType } from '../../types/types' ;
56import { DEFAULT_BASE_CONTEXT_VALUE } from '../utils/defaultBaseContextValue' ;
67
78import { isTestEnvironment } from '../utils/isTestEnvironment' ;
89
910export type ImageGalleryContextValue = {
11+ autoPlayVideo ?: boolean ;
1012 imageGalleryStateStore : ImageGalleryStateStore ;
1113} ;
1214
15+ export type ImageGalleryProviderProps = {
16+ autoPlayVideo ?: boolean ;
17+ giphyVersion ?: keyof NonNullable < Attachment [ 'giphy' ] > ;
18+ } ;
19+
1320export const ImageGalleryContext = React . createContext (
1421 DEFAULT_BASE_CONTEXT_VALUE as ImageGalleryContextValue ,
1522) ;
1623
17- export const ImageGalleryProvider = ( { children } : PropsWithChildren < UnknownType > ) => {
18- const [ imageGalleryStateStore ] = useState (
19- ( ) => new ImageGalleryStateStore ( { autoPlayVideo : false , giphyVersion : 'fixed_height' } ) ,
20- ) ;
24+ export const ImageGalleryProvider = ( {
25+ children,
26+ value,
27+ } : PropsWithChildren < { value : ImageGalleryProviderProps } > ) => {
28+ const [ imageGalleryStateStore ] = useState ( ( ) => new ImageGalleryStateStore ( value ) ) ;
2129
2230 useEffect ( ( ) => {
2331 const unsubscribe = imageGalleryStateStore . registerSubscriptions ( ) ;
@@ -26,13 +34,17 @@ export const ImageGalleryProvider = ({ children }: PropsWithChildren<UnknownType
2634 } ;
2735 } , [ imageGalleryStateStore ] ) ;
2836
37+ const imageGalleryContextValue = useMemo (
38+ ( ) => ( {
39+ autoPlayVideo : value ?. autoPlayVideo ,
40+ imageGalleryStateStore,
41+ } ) ,
42+ [ value ?. autoPlayVideo , imageGalleryStateStore ] ,
43+ ) ;
44+
2945 return (
3046 < ImageGalleryContext . Provider
31- value = {
32- {
33- imageGalleryStateStore,
34- } as unknown as ImageGalleryContextValue
35- }
47+ value = { imageGalleryContextValue as unknown as ImageGalleryContextValue }
3648 >
3749 { children }
3850 </ ImageGalleryContext . Provider >
0 commit comments