Repository navigation
Expand file tree
/
Copy pathApp.js
More file actions
123 lines (109 loc) · 4.1 KB
/
Copy pathApp.js
File metadata and controls
123 lines (109 loc) · 4.1 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
117
118
119
120
121
122
123
import { ApolloClient, ApolloProvider, InMemoryCache, ApolloLink } from '@apollo/client';
import { onError } from '@apollo/client/link/error';
import { NavigationContainer, DefaultTheme } from '@react-navigation/native';
import { isDevice } from 'expo-device';
import * as SplashScreen from 'expo-splash-screen';
import * as React from 'react';
import { Platform, useColorScheme } from 'react-native';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import Sentry from './Sentry';
import BottomTabNavigator from './navigation/BottomTabNavigator';
import linking from './navigation/useLinking';
import { createAbsintheSocketLink } from './src/absintheSocketLink';
import { persistCacheOnChange, restoreCache } from './src/persistedCache';
import * as serviceWorkerRegistration from './src/serviceWorkerRegistration';
const onErrorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.map(({ message, locations, path }) =>
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`)
);
if (networkError) console.log(`[Network error]:`, networkError);
});
// Simulator and web talk to the local Phoenix; a physical phone always talks to
// production, also in the dev client.
const host =
__DEV__ && (Platform.OS === 'web' || !isDevice)
? 'ws://localhost:4010/socket'
: 'wss://picape.whybug.com/socket';
const link = ApolloLink.from([onErrorLink, createAbsintheSocketLink(host)]);
const cache = new InMemoryCache();
// Before the first render, so the screens have something to paint rather than
// sweeping skeletons while the socket connects.
restoreCache(cache);
persistCacheOnChange(cache);
const client = new ApolloClient({
link,
cache,
defaultOptions: {
watchQuery: {
// Show what was restored straight away and refresh it over the socket.
// Without this the restored cache would satisfy the query and the screen
// would keep showing whatever was true when the app was last closed.
fetchPolicy: 'cache-and-network',
// Only the first fetch needs the network; refetches within a session are
// driven by the subscriptions.
nextFetchPolicy: 'cache-first',
},
},
});
const LightTheme = {
...DefaultTheme,
colors: {
...DefaultTheme.colors,
background: 'white', // by default this is grey
},
};
const DarkTheme = {
...DefaultTheme,
dark: true,
colors: {
...DefaultTheme.colors,
background: 'black', // by default this is grey
border: '#404040',
},
};
export default function App(props) {
const [isLoadingComplete, setLoadingComplete] = React.useState(false);
const [initialNavigationState, setInitialNavigationState] = React.useState();
const containerRef = React.useRef();
const colorScheme = useColorScheme();
const theme = colorScheme === 'dark' ? DarkTheme : LightTheme;
// Load any resources or data that we need prior to rendering the app
React.useEffect(() => {
async function loadResourcesAndDataAsync() {
try {
SplashScreen.preventAutoHideAsync();
// Load our initial navigation state
// setInitialNavigationState(await getInitialState());
} catch (e) {
// We might want to provide this error information to an error reporting service
console.warn(e);
} finally {
setLoadingComplete(true);
SplashScreen.hideAsync();
}
}
loadResourcesAndDataAsync();
}, []);
if (!isLoadingComplete && !props.skipLoadingScreen) {
return null;
} else {
return (
<SafeAreaProvider>
<ApolloProvider client={client}>
<NavigationContainer
ref={containerRef}
linking={linking}
initialState={initialNavigationState}
theme={theme}>
<BottomTabNavigator />
</NavigationContainer>
</ApolloProvider>
</SafeAreaProvider>
);
}
}
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://cra.link/PWA
serviceWorkerRegistration.register();