11import type { VideoPlayer , VideoPlayerStatus , VideoView } from 'expo-video' ;
2- import React , { useCallback , useContext , useEffect , useMemo , useRef , useState } from 'react' ;
2+ import React , { useCallback , useContext , useEffect , useRef , useState } from 'react' ;
33import type { View } from 'react-native' ;
44import { getReportOrDraftReport , isChatThread } from '@libs/ReportUtils' ;
55import Navigation from '@navigation/Navigation' ;
66import type ChildrenProps from '@src/types/utils/ChildrenProps' ;
77import type { ProtectedCurrentRouteReportID } from './playbackContextReportIDUtils' ;
88import { findURLInReportOrAncestorAttachments , getCurrentRouteReportID , NO_REPORT_ID , NO_REPORT_ID_IN_PARAMS , normalizeReportID } from './playbackContextReportIDUtils' ;
9- import type { OriginalParent , PlaybackContext , PlaybackContextValues } from './types' ;
9+ import type { OriginalParent , PlaybackActionsContext , PlaybackActionsContextValues , PlaybackStateContext , PlaybackStateContextValues } from './types' ;
1010import usePlaybackContextVideoRefs from './usePlaybackContextVideoRefs' ;
1111
12- const Context = React . createContext < PlaybackContext | null > ( null ) ;
12+ const ContextState = React . createContext < PlaybackStateContext | null > ( null ) ;
13+ const ContextActions = React . createContext < PlaybackActionsContext | null > ( null ) ;
1314
1415function PlaybackContextProvider ( { children} : ChildrenProps ) {
15- const [ currentlyPlayingURL , setCurrentlyPlayingURL ] = useState < PlaybackContextValues [ 'currentlyPlayingURL' ] > ( null ) ;
16- const [ sharedElement , setSharedElement ] = useState < PlaybackContextValues [ 'sharedElement' ] > ( null ) ;
16+ const [ currentlyPlayingURL , setCurrentlyPlayingURL ] = useState < PlaybackStateContextValues [ 'currentlyPlayingURL' ] > ( null ) ;
17+ const [ sharedElement , setSharedElement ] = useState < PlaybackStateContextValues [ 'sharedElement' ] > ( null ) ;
1718 const [ originalParent , setOriginalParent ] = useState < OriginalParent > ( null ) ;
1819 const [ currentRouteReportID , setCurrentRouteReportID ] = useState < ProtectedCurrentRouteReportID > ( NO_REPORT_ID ) ;
1920 const mountedVideoPlayersRef = useRef < string [ ] > ( [ ] ) ;
@@ -28,7 +29,7 @@ function PlaybackContextProvider({children}: ChildrenProps) {
2829
2930 const video = usePlaybackContextVideoRefs ( resetContextProperties ) ;
3031
31- const updateCurrentURLAndReportID : PlaybackContextValues [ 'updateCurrentURLAndReportID' ] = useCallback (
32+ const updateCurrentURLAndReportID : PlaybackActionsContextValues [ 'updateCurrentURLAndReportID' ] = useCallback (
3233 ( url , reportID ) => {
3334 if ( ! reportID ) {
3435 return ;
@@ -69,7 +70,7 @@ function PlaybackContextProvider({children}: ChildrenProps) {
6970 playerStatus . current = newStatus ;
7071 } , [ ] ) ;
7172
72- const shareVideoPlayerElements : PlaybackContextValues [ 'shareVideoPlayerElements' ] = useCallback (
73+ const shareVideoPlayerElements : PlaybackActionsContextValues [ 'shareVideoPlayerElements' ] = useCallback (
7374 (
7475 videoPlayerRef : VideoPlayer | null ,
7576 videoViewRef : VideoView | null ,
@@ -117,55 +118,55 @@ function PlaybackContextProvider({children}: ChildrenProps) {
117118 } ) ;
118119 } , [ currentRouteReportID , currentlyPlayingURL , video , video . resetPlayerData ] ) ;
119120
120- const contextValue : PlaybackContext = useMemo (
121- ( ) => ( {
122- updateCurrentURLAndReportID,
123- currentlyPlayingURL,
124- currentRouteReportID : normalizeReportID ( currentRouteReportID ) ,
125- originalParent,
126- sharedElement,
127- shareVideoPlayerElements,
128- setCurrentlyPlayingURL,
129- currentVideoPlayerRef : video . playerRef ,
130- currentVideoViewRef : video . viewRef ,
131- playVideo : video . play ,
132- pauseVideo : video . pause ,
133- replayVideo : video . replay ,
134- stopVideo : video . stop ,
135- checkIfVideoIsPlaying : video . isPlaying ,
136- resetVideoPlayerData : video . resetPlayerData ,
137- mountedVideoPlayersRef,
138- playerStatus,
139- updatePlayerStatus,
140- } ) ,
141- [
142- updateCurrentURLAndReportID ,
143- currentlyPlayingURL ,
144- currentRouteReportID ,
145- originalParent ,
146- sharedElement ,
147- shareVideoPlayerElements ,
148- video . playerRef ,
149- video . viewRef ,
150- video . play ,
151- video . pause ,
152- video . replay ,
153- video . stop ,
154- video . isPlaying ,
155- video . resetPlayerData ,
156- updatePlayerStatus ,
157- ] ,
121+ // Because of the React Compiler we don't need to memoize it manually
122+ // eslint-disable-next-line react/jsx-no-constructed-context-values
123+ const stateValue : PlaybackStateContext = {
124+ currentlyPlayingURL,
125+ currentRouteReportID : normalizeReportID ( currentRouteReportID ) ,
126+ originalParent,
127+ sharedElement,
128+ currentVideoPlayerRef : video . playerRef ,
129+ currentVideoViewRef : video . viewRef ,
130+ mountedVideoPlayersRef,
131+ playerStatus,
132+ } ;
133+
134+ // Because of the React Compiler we don't need to memoize it manually
135+ // eslint-disable-next-line react/jsx-no-constructed-context-values
136+ const actionsValue : PlaybackActionsContext = {
137+ updateCurrentURLAndReportID,
138+ shareVideoPlayerElements,
139+ setCurrentlyPlayingURL,
140+ playVideo : video . play ,
141+ pauseVideo : video . pause ,
142+ replayVideo : video . replay ,
143+ stopVideo : video . stop ,
144+ checkIfVideoIsPlaying : video . isPlaying ,
145+ resetVideoPlayerData : video . resetPlayerData ,
146+ updatePlayerStatus,
147+ } ;
148+
149+ return (
150+ < ContextState . Provider value = { stateValue } >
151+ < ContextActions . Provider value = { actionsValue } > { children } </ ContextActions . Provider >
152+ </ ContextState . Provider >
158153 ) ;
154+ }
159155
160- return < Context . Provider value = { contextValue } > { children } </ Context . Provider > ;
156+ function usePlaybackStateContext ( ) {
157+ const playbackStateContext = useContext ( ContextState ) ;
158+ if ( ! playbackStateContext ) {
159+ throw new Error ( 'usePlaybackStateContext must be used within a PlaybackContextProvider' ) ;
160+ }
161+ return playbackStateContext ;
161162}
162163
163- function usePlaybackContext ( ) {
164- const playbackContext = useContext ( Context ) ;
165- if ( ! playbackContext ) {
166- throw new Error ( 'usePlaybackContext must be used within a PlaybackContextProvider' ) ;
164+ function usePlaybackActionsContext ( ) {
165+ const playbackActionsContext = useContext ( ContextActions ) ;
166+ if ( ! playbackActionsContext ) {
167+ throw new Error ( 'usePlaybackActionsContext must be used within a PlaybackContextProvider' ) ;
167168 }
168- return playbackContext ;
169+ return playbackActionsContext ;
169170}
170171
171- export { Context as PlaybackContext , PlaybackContextProvider , usePlaybackContext } ;
172+ export { ContextActions as PlaybackActionsContext , ContextState as PlaybackStateContext , PlaybackContextProvider , usePlaybackStateContext , usePlaybackActionsContext } ;
0 commit comments