forked from GetStream/stream-chat-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
207 lines (185 loc) · 5.78 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* eslint-disable react/display-name */
import 'react-native-gesture-handler';
import React, { useContext, useEffect, useMemo, useState } from 'react';
import { LogBox, SafeAreaView, StatusBar, useColorScheme, View } from 'react-native';
import { DarkTheme, DefaultTheme, NavigationContainer } from '@react-navigation/native';
import { createStackNavigator, useHeaderHeight } from '@react-navigation/stack';
import { SafeAreaProvider, useSafeAreaInsets } from 'react-native-safe-area-context';
import { StreamChat } from 'stream-chat';
import {
Channel,
ChannelList,
Chat,
MessageInput,
MessageList,
OverlayProvider,
Streami18n,
Thread,
useAttachmentPickerContext,
} from 'stream-chat-expo';
import { useStreamChatTheme } from './useStreamChatTheme';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
LogBox.ignoreAllLogs(true);
const chatClient = StreamChat.getInstance('q95x9hkbyd6p');
const userToken =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoicm9uIn0.eRVjxLvd4aqCEHY_JRa97g6k7WpHEhxL7Z4K4yTot1c';
const user = {
id: 'ron',
};
const filters = {
example: 'example-apps',
members: { $in: ['ron'] },
type: 'messaging',
};
const sort = { last_message_at: -1 };
const options = {
state: true,
watch: true,
};
/**
* Start playing with streami18n instance here:
* Please refer to description of this PR for details: https://github.com/GetStream/stream-chat-react-native/pull/150
*/
const streami18n = new Streami18n({
language: 'en',
});
const ChannelListScreen = ({ navigation }) => {
const { setChannel } = useContext(AppContext);
const memoizedFilters = useMemo(() => filters, []);
return (
<Chat client={chatClient} i18nInstance={streami18n}>
<View style={{ height: '100%' }}>
<ChannelList
filters={memoizedFilters}
onSelect={(channel) => {
setChannel(channel);
navigation.navigate('Channel');
}}
options={options}
sort={sort}
/>
</View>
</Chat>
);
};
const ChannelScreen = ({ navigation }) => {
const { channel, setThread, thread } = useContext(AppContext);
const headerHeight = useHeaderHeight();
const { setTopInset } = useAttachmentPickerContext();
useEffect(() => {
setTopInset(headerHeight);
}, [headerHeight]);
return (
<SafeAreaView>
<Chat client={chatClient} i18nInstance={streami18n}>
<Channel channel={channel} keyboardVerticalOffset={headerHeight} thread={thread}>
<View style={{ flex: 1 }}>
<MessageList
onThreadSelect={(thread) => {
setThread(thread);
navigation.navigate('Thread');
}}
/>
<MessageInput />
</View>
</Channel>
</Chat>
</SafeAreaView>
);
};
const ThreadScreen = () => {
const { channel, setThread, thread } = useContext(AppContext);
const headerHeight = useHeaderHeight();
return (
<SafeAreaView>
<Chat client={chatClient} i18nInstance={streami18n}>
<Channel channel={channel} keyboardVerticalOffset={headerHeight} thread={thread} threadList>
<View
style={{
flex: 1,
justifyContent: 'flex-start',
}}
>
<Thread onThreadDismount={() => setThread(null)} />
</View>
</Channel>
</Chat>
</SafeAreaView>
);
};
const Stack = createStackNavigator();
const AppContext = React.createContext();
const App = () => {
const colorScheme = useColorScheme();
const { bottom } = useSafeAreaInsets();
const theme = useStreamChatTheme();
const [channel, setChannel] = useState();
const [clientReady, setClientReady] = useState(false);
const [thread, setThread] = useState();
useEffect(() => {
const setupClient = async () => {
await chatClient.connectUser(user, userToken);
setClientReady(true);
};
setupClient();
}, []);
return (
<NavigationContainer
theme={{
colors: {
...(colorScheme === 'dark' ? DarkTheme : DefaultTheme).colors,
background: theme.colors?.white_snow || '#FCFCFC',
},
dark: colorScheme === 'dark',
}}
>
<AppContext.Provider value={{ channel, setChannel, setThread, thread }}>
<GestureHandlerRootView style={{ flex: 1 }}>
<OverlayProvider
bottomInset={bottom}
i18nInstance={streami18n}
translucentStatusBar
value={{ style: theme }}
>
{clientReady && (
<Stack.Navigator
initialRouteName='ChannelList'
screenOptions={{
headerTitleStyle: { alignSelf: 'center', fontWeight: 'bold' },
}}
>
<Stack.Screen
component={ChannelScreen}
name='Channel'
options={() => ({
headerBackTitle: 'Back',
headerRight: () => <></>,
headerTitle: channel?.data?.name,
})}
/>
<Stack.Screen
component={ChannelListScreen}
name='ChannelList'
options={{ headerTitle: 'Channel List' }}
/>
<Stack.Screen
component={ThreadScreen}
name='Thread'
options={() => ({ headerLeft: () => <></> })}
/>
</Stack.Navigator>
)}
</OverlayProvider>
</GestureHandlerRootView>
</AppContext.Provider>
</NavigationContainer>
);
};
export default () => {
const theme = useStreamChatTheme();
return (
<SafeAreaProvider style={{ backgroundColor: theme.colors?.white_snow || '#FCFCFC' }}>
<App />
</SafeAreaProvider>
);
};