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
|
import messaging from '@react-native-firebase/messaging';
import React, {useContext, useEffect, useState} from 'react';
import DeviceInfo from 'react-native-device-info';
import SplashScreen from 'react-native-splash-screen';
import {useDispatch, useSelector, useStore} from 'react-redux';
import {ChatContext} from '../App';
import {fcmService, getCurrentLiveVersions} from '../services';
import {
updateNewNotificationReceived,
updateNewVersionAvailable,
} from '../store/actions';
import {RootState} from '../store/rootReducer';
import {connectChatAccount, userLogin} from '../utils';
import Onboarding from './onboarding';
import NavigationBar from './tabs';
const Routes: React.FC = () => {
const {
user: {userId},
} = useSelector((state: RootState) => state.user);
const state: RootState = useStore().getState();
const loggedInUserId = state.user.user.userId;
const {chatClient} = useContext(ChatContext);
const [newVersionAvailable, setNewVersionAvailable] = useState(false);
const dispatch = useDispatch();
/**
* Load the user from AsyncStorage if any
* Note that this makes logout triggered by invalid Token have no effect.
* We should figure out a way to handle that.
* Suggestions?
* NOTE : Not something introduced by this commit but something we already have.
*/
/**
* SplashScreen is the actual react-native's splash screen.
* We can hide / show it depending on our application needs.
*/
useEffect(() => {
messaging().onMessage(() => {
dispatch(updateNewNotificationReceived(true));
});
if (!userId) {
userLogin(dispatch, {userId: '', username: ''});
} else {
SplashScreen.hide();
}
}, [dispatch, userId]);
useEffect(() => {
if (userId) {
fcmService.setUpPushNotifications();
fcmService.sendFcmTokenToServer(chatClient);
}
});
useEffect(() => {
if (loggedInUserId) {
connectChatAccount(loggedInUserId, chatClient);
}
}, [loggedInUserId]);
useEffect(() => {
const checkVersion = async () => {
const liveVersions = await getCurrentLiveVersions();
if (liveVersions && !liveVersions.includes(DeviceInfo.getVersion())) {
setNewVersionAvailable(true);
dispatch(updateNewVersionAvailable(true));
}
};
checkVersion();
}, []);
return userId && !newVersionAvailable ? <NavigationBar /> : <Onboarding />;
};
export default Routes;
|