-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.js
More file actions
116 lines (96 loc) · 3.12 KB
/
App.js
File metadata and controls
116 lines (96 loc) · 3.12 KB
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
import "./polyfills";
import "./theme";
import { DarkTheme, NavigationContainer } from "@react-navigation/native";
import Mapbox from "@rnmapbox/maps";
import { useAssets } from "expo-asset";
import * as Linking from "expo-linking";
import * as SecureStore from "expo-secure-store";
import * as SplashScreen from "expo-splash-screen";
import * as TaskManager from "expo-task-manager";
import { useEffect } from "react";
import { Alert } from "react-native";
import { GestureHandlerRootView } from "react-native-gesture-handler";
import { useShallow } from "zustand/react/shallow";
import globalStyles from "./assets/styles/globalStyles";
import assetList from "./consts/assetList";
import MainNavigation from "./navigation/MainNavigation";
import useAuthStore from "./stores/useAuthStore";
SplashScreen.preventAutoHideAsync();
Mapbox.setAccessToken(process.env.EXPO_PUBLIC_MAPBOX_PUBLIC_TOKEN);
export default function App() {
const { isReady, principal, getPrincipal, fetchKeyAndPrincipal, logout } =
useAuthStore(
useShallow((state) => ({
isReady: state.isReady,
principal: state.principal,
getPrincipal: state.getPrincipal,
fetchKeyAndPrincipal: state.fetchKeyAndPrincipal,
logout: state.logout,
}))
);
const [assets] = useAssets(assetList);
useEffect(() => {
const handleSetIdentity = async ({ url }) => {
if (url && !principal) {
const { queryParams } = Linking.parse(url);
const delegation = queryParams.delegation;
if (delegation) {
getPrincipal(delegation);
console.log("Done setting identity");
}
}
};
const subscription = Linking.addEventListener("url", handleSetIdentity);
return () => {
subscription.remove();
};
}, [principal]);
useEffect(() => {
(async () => {
console.log("Fetching key and principal...");
await fetchKeyAndPrincipal();
console.log("Unregistering all tasks...");
await TaskManager.unregisterAllTasksAsync();
})();
}, []);
useEffect(() => {
// Ask to logout after waiting in splash screen for 30 seconds
const timer = setTimeout(() => {
Alert.alert(
"Error",
"An error occurred while logging in. Please try again.",
[
{
text: "OK",
onPress: async () => {
console.log("Logging out...");
// Log out the previous user
await logout();
// Delete base key
await SecureStore.deleteItemAsync("baseKey");
// Try to fetch key and principal again
await fetchKeyAndPrincipal();
},
},
]
);
}, 30000);
if (isReady && assets) {
clearTimeout(timer);
}
return () => clearTimeout(timer);
}, [assets, isReady]);
if (!isReady || !assets) {
return null;
}
return (
<GestureHandlerRootView style={globalStyles.flexFull}>
<NavigationContainer
theme={DarkTheme}
onReady={() => SplashScreen.hideAsync()}
>
<MainNavigation />
</NavigationContainer>
</GestureHandlerRootView>
);
}