1- import React , { useContext } from 'react' ;
2- import { SafeAreaView , View } from 'react-native ' ;
3- import { Channel , MessageInput , MessageFlashList } from 'stream-chat-expo' ;
4- import { Stack , useRouter } from 'expo-router' ;
1+ import React , { useContext , useEffect , useState } from 'react' ;
2+ import type { Channel as StreamChatChannel } from 'stream-chat ' ;
3+ import { Channel , MessageInput , MessageFlashList , useChatContext } from 'stream-chat-expo' ;
4+ import { Stack , useLocalSearchParams , useRouter } from 'expo-router' ;
55import { AuthProgressLoader } from '../../../components/AuthProgressLoader' ;
66import { AppContext } from '../../../context/AppContext' ;
77import { useHeaderHeight } from '@react-navigation/elements' ;
88import InputButtons from '../../../components/InputButtons' ;
99import { MessageLocation } from '../../../components/LocationSharing/MessageLocation' ;
10+ import { SafeAreaView } from 'react-native-safe-area-context' ;
11+ import { StyleSheet } from 'react-native' ;
1012
1113export default function ChannelScreen ( ) {
14+ const { client } = useChatContext ( ) ;
1215 const router = useRouter ( ) ;
13- const { setThread, channel, thread } = useContext ( AppContext ) ;
16+ const params = useLocalSearchParams ( ) ;
17+ const navigateThroughPushNotification = params . push_notification as string ;
18+ const channelId = params . cid as string ;
19+ const [ channelFromParams , setChannelFromParams ] = useState < StreamChatChannel | undefined > (
20+ undefined ,
21+ ) ;
22+
23+ // Effect to fetch channel from params if channel is navigated through push notification
24+ useEffect ( ( ) => {
25+ const initChannel = async ( ) => {
26+ if ( navigateThroughPushNotification ) {
27+ const channel = client ?. channel ( 'messaging' , channelId ) ;
28+ await channel ?. watch ( ) ;
29+ setChannelFromParams ( channel ) ;
30+ }
31+ } ;
32+ initChannel ( ) ;
33+ } , [ navigateThroughPushNotification , channelId , client ] ) ;
34+
35+ const { setThread, channel : channelContext , thread } = useContext ( AppContext ) ;
1436 const headerHeight = useHeaderHeight ( ) ;
1537
38+ const channel = channelFromParams || channelContext ;
39+
1640 if ( ! channel ) {
1741 return < AuthProgressLoader /> ;
1842 }
@@ -32,29 +56,36 @@ export default function ChannelScreen() {
3256 defaultHandler ?.( ) ;
3357 } ;
3458
59+ if ( ! channel ) {
60+ return null ;
61+ }
62+
3563 return (
36- < SafeAreaView >
64+ < Channel
65+ audioRecordingEnabled = { true }
66+ channel = { channel }
67+ onPressMessage = { onPressMessage }
68+ keyboardVerticalOffset = { headerHeight }
69+ MessageLocation = { MessageLocation }
70+ thread = { thread }
71+ >
3772 < Stack . Screen options = { { title : 'Channel Screen' } } />
38- { channel && (
39- < Channel
40- audioRecordingEnabled = { true }
41- channel = { channel }
42- onPressMessage = { onPressMessage }
43- keyboardVerticalOffset = { headerHeight }
44- MessageLocation = { MessageLocation }
45- thread = { thread }
46- >
47- < View style = { { flex : 1 } } >
48- < MessageFlashList
49- onThreadSelect = { ( thread ) => {
50- setThread ( thread ) ;
51- router . push ( `/channel/${ channel . cid } /thread/${ thread . cid } ` ) ;
52- } }
53- />
54- < MessageInput InputButtons = { InputButtons } />
55- </ View >
56- </ Channel >
57- ) }
58- </ SafeAreaView >
73+
74+ < SafeAreaView edges = { [ 'bottom' ] } style = { styles . container } >
75+ < MessageFlashList
76+ onThreadSelect = { ( thread ) => {
77+ setThread ( thread ) ;
78+ router . push ( `/channel/${ channel . cid } /thread/${ thread . cid } ` ) ;
79+ } }
80+ />
81+ < MessageInput InputButtons = { InputButtons } />
82+ </ SafeAreaView >
83+ </ Channel >
5984 ) ;
6085}
86+
87+ const styles = StyleSheet . create ( {
88+ container : {
89+ flex : 1 ,
90+ } ,
91+ } ) ;
0 commit comments