-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathApp.tsx
100 lines (94 loc) · 2.67 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
import AsyncStorage from '@react-native-async-storage/async-storage';
import {ConnectionProvider} from '@solana/wallet-adapter-react';
import {clusterApiUrl, PublicKey, PublicKeyInitData} from '@solana/web3.js';
import React, {Suspense} from 'react';
import {
ActivityIndicator,
AppState,
SafeAreaView,
StyleSheet,
View,
} from 'react-native';
import {Provider as PaperProvider} from 'react-native-paper';
import {Cache, SWRConfig} from 'swr';
import SnackbarProvider from './components/SnackbarProvider';
import MainScreen from './screens/MainScreen';
const DEVNET_ENDPOINT = /*#__PURE__*/ clusterApiUrl('devnet');
function cacheReviver(key: string, value: any) {
if (key === 'publicKey') {
return new PublicKey(value as PublicKeyInitData);
} else {
return value;
}
}
const STORAGE_KEY = 'app-cache';
let initialCacheFetchPromise: Promise<void>;
let initialCacheFetchResult: any;
function asyncStorageProvider() {
if (initialCacheFetchPromise == null) {
initialCacheFetchPromise = AsyncStorage.getItem(STORAGE_KEY).then(
result => {
initialCacheFetchResult = result;
},
);
throw initialCacheFetchPromise;
}
let storedAppCache;
try {
storedAppCache = JSON.parse(initialCacheFetchResult, cacheReviver);
} catch {}
const map = new Map(storedAppCache || []);
initialCacheFetchResult = undefined;
function persistCache() {
const appCache = JSON.stringify(Array.from(map.entries()));
AsyncStorage.setItem(STORAGE_KEY, appCache);
}
AppState.addEventListener('change', state => {
if (state !== 'active') {
persistCache();
}
});
AppState.addEventListener('memoryWarning', () => {
persistCache();
});
return map as Cache<any>;
}
export default function App() {
return (
<ConnectionProvider
config={{commitment: 'processed'}}
endpoint={DEVNET_ENDPOINT}>
<SafeAreaView style={styles.shell}>
<PaperProvider>
<SnackbarProvider>
<Suspense
fallback={
<View style={styles.loadingContainer}>
<ActivityIndicator
size="large"
style={styles.loadingIndicator}
/>
</View>
}>
<SWRConfig value={{provider: asyncStorageProvider}}>
<MainScreen />
</SWRConfig>
</Suspense>
</SnackbarProvider>
</PaperProvider>
</SafeAreaView>
</ConnectionProvider>
);
}
const styles = StyleSheet.create({
loadingContainer: {
height: '100%',
justifyContent: 'center',
},
loadingIndicator: {
marginVertical: 'auto',
},
shell: {
height: '100%',
},
});