This repository has been archived by the owner on Jan 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoot.js
94 lines (88 loc) · 3.42 KB
/
Root.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
import { View } from "react-native";
import React from "react";
import { getValue } from "./utils/secureStore";
import { useState, useCallback, useEffect } from "react";
import * as SplashScreen from "expo-splash-screen";
import { useSelector, useDispatch } from "react-redux";
import { logIn } from "./features/authSlice";
import UnauthedNavigator from "./nav/UnauthedNavigator";
import { loadAsync } from "expo-font";
import AuthedNavigator from "./nav/AuthedNavigator";
import { loginToWallet } from "./utils/wallet";
import { NavigationContainer } from "@react-navigation/native";
import { StatusBar } from "expo-status-bar";
import { hydrateFromDatabase, init } from "./utils/database";
import { getPublicKey } from "nostr-tools";
import { initRelays } from "./utils/nostrV2";
import { updateFollowedUsers } from "./utils/nostrV2/getUserData";
import { hydrateStore } from "./utils/cache/asyncStorage";
SplashScreen.preventAutoHideAsync();
const Root = () => {
const [appIsReady, setAppIsReady] = useState();
const dispatch = useDispatch();
const { isLoggedIn, walletExpires } = useSelector((state) => state.auth);
useEffect(() => {
const prepare = async () => {
try {
await initRelays();
const privKey = await getValue("privKey");
await loadAsync({
"Montserrat-Regular": require("./assets/Montserrat-Regular.ttf"),
"Montserrat-Bold": require("./assets/Montserrat-Bold.ttf"),
"Satoshi-Symbol": require("./assets/Satoshi-Symbol.ttf"),
});
if (privKey) {
console.log("Initialising from storage...");
const {data: {access_token, username}} = await loginToWallet(
privKey
);
const pubKey = await getPublicKey(privKey)
console.log(username)
dispatch(logIn({ bearer: access_token, username, pubKey }));
}
await init();
await hydrateFromDatabase();
await updateFollowedUsers();
await hydrateStore();
} catch (e) {
console.warn(e);
} finally {
setAppIsReady(true);
}
};
prepare();
}, []);
const onLayoutRootView = useCallback(() => {
if (appIsReady) {
SplashScreen.hideAsync();
}
}, [appIsReady]);
if (!appIsReady) {
return null;
}
return (
<NavigationContainer
onStateChange={async () => {
if (isLoggedIn && new Date() > walletExpires) {
const privKey = await getValue("privKey");
const username = await getValue("username");
const { access_token } = loginToWallet(privKey, username);
dispatch(
logIn({ bearer: access_token, username: username })
);
console.log("Token refreshed");
}
}}
>
<StatusBar style="light" />
<View style={{ flex: 1 }} onLayout={onLayoutRootView}>
{isLoggedIn == false ? (
<UnauthedNavigator />
) : (
<AuthedNavigator />
)}
</View>
</NavigationContainer>
);
};
export default Root;