-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeApp.tsx
223 lines (199 loc) · 8.31 KB
/
HomeApp.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { darkTheme, lightTheme } from '@expo/styleguide-native';
import Ionicons from '@expo/vector-icons/build/Ionicons';
import MaterialIcons from '@expo/vector-icons/build/MaterialIcons';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { Assets as StackAssets } from '@react-navigation/elements';
import { Asset } from 'expo-asset';
import { ThemePreference, ThemeProvider } from 'expo-dev-client-components';
import * as Font from 'expo-font';
import * as SplashScreen from 'expo-splash-screen';
import * as React from 'react';
import { Linking, Platform, StyleSheet, View, useColorScheme } from 'react-native';
import url from 'url';
import ApolloClient from './api/ApolloClient';
import { ColorTheme } from './constants/Colors';
import {
HomeScreenDataDocument,
HomeScreenDataQuery,
HomeScreenDataQueryVariables,
Home_CurrentUserDocument,
Home_CurrentUserQuery,
Home_CurrentUserQueryVariables,
} from './graphql/types';
import Navigation from './navigation/Navigation';
import HistoryActions from './redux/HistoryActions';
import { useDispatch, useSelector } from './redux/Hooks';
import SessionActions from './redux/SessionActions';
import SettingsActions from './redux/SettingsActions';
import LocalStorage from './storage/LocalStorage';
import { useAccountName } from './utils/AccountNameContext';
import { useInitialData } from './utils/InitialDataContext';
import * as UrlUtils from './utils/UrlUtils';
import addListenerWithNativeCallback from './utils/addListenerWithNativeCallback';
// Download and cache stack assets, don't block loading on this though
Asset.loadAsync(StackAssets);
function useSplashScreenWhileLoadingResources(loadResources: () => Promise<void>) {
const [isSplashScreenShown, setSplashScreenShown] = React.useState(true);
React.useEffect(() => {
(async () => {
// await SplashScreen.preventAutoHideAsync(); // this is called in App (main component of the application)
await loadResources();
setSplashScreenShown(false);
})();
}, []);
React.useEffect(() => {
(async () => {
if (!isSplashScreenShown) {
await SplashScreen.hideAsync();
}
})();
}, [isSplashScreenShown]);
return isSplashScreenShown;
}
export default function HomeApp() {
const colorScheme = useColorScheme();
const preferredAppearance = useSelector((data) => data.settings.preferredAppearance);
const dispatch = useDispatch();
const { setAccountName } = useAccountName();
const isShowingSplashScreen = useSplashScreenWhileLoadingResources(async () => {
await initStateAsync();
});
const { setCurrentUserData, setHomeScreenData } = useInitialData();
React.useEffect(() => {
addProjectHistoryListener();
}, []);
React.useEffect(() => {
if (!isShowingSplashScreen && Platform.OS === 'ios') {
// If Expo Go is opened via deep linking, we'll get the URL here
Linking.getInitialURL().then((initialUrl) => {
if (initialUrl && shouldOpenUrl(initialUrl)) {
Linking.openURL(UrlUtils.toExp(initialUrl));
}
});
}
}, [isShowingSplashScreen]);
const addProjectHistoryListener = () => {
addListenerWithNativeCallback('ExponentKernel.addHistoryItem', async (event) => {
let { manifestUrl, manifest, manifestString } = event;
if (!manifest && manifestString) {
manifest = JSON.parse(manifestString);
}
dispatch(HistoryActions.addHistoryItem(manifestUrl, manifest));
});
};
const initStateAsync = async () => {
try {
dispatch(SettingsActions.loadSettings());
dispatch(HistoryActions.loadHistory());
const storedSession = await LocalStorage.getSessionAsync();
if (storedSession) {
dispatch(SessionActions.setSession(storedSession));
}
const [currentUserQueryResult, persistedCurrentAccount] = await Promise.all([
ApolloClient.query<Home_CurrentUserQuery, Home_CurrentUserQueryVariables>({
query: Home_CurrentUserDocument,
context: { headers: { 'expo-session': storedSession?.sessionSecret } },
}),
AsyncStorage.getItem('currentAccount'),
Font.loadAsync(Ionicons.font),
Platform.OS === 'android'
? Font.loadAsync(MaterialIcons.font)
: new Promise((resolve) => setTimeout(resolve, 0)),
Font.loadAsync({
'Inter-Black': require('./assets/Inter/Inter-Black.otf'),
'Inter-BlackItalic': require('./assets/Inter/Inter-BlackItalic.otf'),
'Inter-Bold': require('./assets/Inter/Inter-Bold.otf'),
'Inter-BoldItalic': require('./assets/Inter/Inter-BoldItalic.otf'),
'Inter-ExtraBold': require('./assets/Inter/Inter-ExtraBold.otf'),
'Inter-ExtraBoldItalic': require('./assets/Inter/Inter-ExtraBoldItalic.otf'),
'Inter-ExtraLight': require('./assets/Inter/Inter-ExtraLight.otf'),
'Inter-ExtraLightItalic': require('./assets/Inter/Inter-ExtraLightItalic.otf'),
'Inter-Regular': require('./assets/Inter/Inter-Regular.otf'),
'Inter-Italic': require('./assets/Inter/Inter-Italic.otf'),
'Inter-Light': require('./assets/Inter/Inter-Light.otf'),
'Inter-LightItalic': require('./assets/Inter/Inter-LightItalic.otf'),
'Inter-Medium': require('./assets/Inter/Inter-Medium.otf'),
'Inter-MediumItalic': require('./assets/Inter/Inter-MediumItalic.otf'),
'Inter-SemiBold': require('./assets/Inter/Inter-SemiBold.otf'),
'Inter-SemiBoldItalic': require('./assets/Inter/Inter-SemiBoldItalic.otf'),
'Inter-Thin': require('./assets/Inter/Inter-Thin.otf'),
'Inter-ThinItalic': require('./assets/Inter/Inter-ThinItalic.otf'),
}),
]);
if (currentUserQueryResult.data && currentUserQueryResult.data.viewer) {
let firstLoadAccountName = persistedCurrentAccount;
if (firstLoadAccountName) {
// if there was a persisted account, and it matches the accounts available to the current user, use it
if (
[
currentUserQueryResult.data.viewer.username,
...currentUserQueryResult.data.viewer.accounts.map((account) => account.name),
].includes(firstLoadAccountName)
) {
setAccountName(firstLoadAccountName);
} else {
// if this persisted account is stale, clear it
await AsyncStorage.removeItem('currentAccount');
}
} else {
// if there was no persisted account, use the current user's personal account
firstLoadAccountName = currentUserQueryResult.data.viewer.username;
setAccountName(firstLoadAccountName);
}
// set initial data for home screen
setCurrentUserData(currentUserQueryResult.data);
if (firstLoadAccountName) {
const homeScreenData = await ApolloClient.query<
HomeScreenDataQuery,
HomeScreenDataQueryVariables
>({
query: HomeScreenDataDocument,
variables: {
accountName: firstLoadAccountName,
},
context: { headers: { 'expo-session': storedSession?.sessionSecret } },
});
setHomeScreenData(homeScreenData.data);
}
} else {
// if there is no current user data, clear the accountName
setAccountName(undefined);
}
} finally {
return;
}
};
if (isShowingSplashScreen) {
return null;
}
let theme = !preferredAppearance ? colorScheme : preferredAppearance;
if (theme === undefined || theme === null || (theme !== 'dark' && theme !== 'light')) {
theme = 'light';
}
const backgroundColor =
theme === 'dark' ? darkTheme.background.default : lightTheme.background.default;
return (
<ThemeProvider themePreference={theme as ThemePreference}>
<View
style={[
styles.container,
{
backgroundColor,
},
]}>
<Navigation theme={theme === 'light' ? ColorTheme.LIGHT : ColorTheme.DARK} />
</View>
</ThemeProvider>
);
}
// Certain links (i.e. 'expo.dev/expo-go') should just open the HomeScreen
function shouldOpenUrl(urlString: string) {
const parsedUrl = url.parse(urlString);
return !(
(parsedUrl.hostname === 'expo.io' || parsedUrl.hostname === 'expo.dev') &&
parsedUrl.pathname === '/expo-go'
);
}
const styles = StyleSheet.create({
container: { flex: 1 },
});