-
Notifications
You must be signed in to change notification settings - Fork 26
/
App.tsx
129 lines (119 loc) · 4.16 KB
/
App.tsx
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
import { StatusBar } from "expo-status-bar";
import { SafeAreaProvider } from "react-native-safe-area-context";
import * as eva from "@eva-design/eva";
import { ApplicationProvider } from "@ui-kitten/components";
import { QueryClient, QueryClientProvider } from "react-query";
import { useState, useEffect } from "react";
import * as SecureStore from "expo-secure-store";
import { LogBox } from "react-native";
import * as Notifications from "expo-notifications";
import useCachedResources from "./hooks/useCachedResources";
import useColorScheme from "./hooks/useColorScheme";
import Navigation from "./navigation";
import { theme } from "./theme";
import { AuthContext, LoadingContext } from "./context";
import { User } from "./types/user";
import { socket } from "./constants/socket";
import { queryKeys } from "./constants";
import { refreshTokens } from "./services/tokens";
const queryClient = new QueryClient();
LogBox.ignoreAllLogs();
export default function App() {
const isLoadingComplete = useCachedResources();
const colorScheme = useColorScheme();
const [user, setUser] = useState<User | null>(null);
const [loading, setLoading] = useState<boolean>(false);
useEffect(() => {
async function getUser() {
const user = await SecureStore.getItemAsync("user");
if (user) {
const userObj: User = JSON.parse(user);
const newTokens = await refreshTokens(userObj.refreshToken);
if (newTokens) {
userObj.accessToken = newTokens.accessToken;
userObj.refreshToken = newTokens.refreshToken;
SecureStore.setItemAsync("user", JSON.stringify(userObj));
}
setUser(userObj);
socket.auth = {
userID: userObj.ID,
username:
userObj.firstName && userObj.lastName
? `${userObj.firstName} ${userObj.lastName}`
: `${userObj.email}`,
accessToken: userObj.accessToken,
};
socket.connect();
}
}
getUser().then(() => {
socket.on(
"getMessage",
(data: {
senderID: number;
senderName: string;
conversationID: number;
text: string;
}) => {
queryClient.invalidateQueries(queryKeys.conversations);
queryClient.invalidateQueries(queryKeys.selectedConversation);
Notifications.scheduleNotificationAsync({
content: {
title: data.senderName,
body: data.text,
data: {
// will need to change url in prod build (use process.ENV && eas.json)
url: `exp://192.168.30.24:19000/--/messages/${data.conversationID}/${data.senderName}`,
},
},
trigger: null,
});
}
);
socket.on("session", (data: { sessionID: string }) => {
socket.auth = { sessionID: data.sessionID };
if (user) {
const updatedUser = { ...user };
updatedUser.sessionID = data.sessionID;
setUser(updatedUser);
SecureStore.setItemAsync("user", JSON.stringify(updatedUser));
}
});
socket.on("connect_error", (err) => {
if (err.message === "Invalid userID" && user) {
socket.auth = {
userID: user?.ID,
username:
user.firstName && user.lastName
? `${user.firstName} ${user.lastName}`
: `${user.email}`,
};
socket.connect();
}
});
});
return () => {
socket.off("getMesssage");
socket.off("session");
socket.off("connect_error");
};
}, []);
if (!isLoadingComplete) {
return null;
} else {
return (
<LoadingContext.Provider value={{ loading, setLoading }}>
<AuthContext.Provider value={{ user, setUser }}>
<QueryClientProvider client={queryClient}>
<ApplicationProvider {...eva} theme={theme}>
<SafeAreaProvider>
<Navigation colorScheme={colorScheme} />
<StatusBar />
</SafeAreaProvider>
</ApplicationProvider>
</QueryClientProvider>
</AuthContext.Provider>
</LoadingContext.Provider>
);
}
}