-
Notifications
You must be signed in to change notification settings - Fork 16
/
App.tsx
93 lines (78 loc) · 4 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
import { BottomTabNavigationProp, createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { NavigationContainer, RouteProp } from '@react-navigation/native';
import AppLoading from 'expo-app-loading';
import React, { ReactElement, useCallback } from 'react';
import 'react-native-get-random-values';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { enableScreens } from 'react-native-screens';
import { Toast } from './src/components/common/Toast/Toast';
import { ToastProvider } from './src/components/common/Toast/ToastProvider';
import HomeScreen from './src/components/Home/HomeScreen';
import LoadingScreen from './src/components/Loading/LoadingScreen';
import ProjectScreen from './src/components/Project/ProjectScreen';
import SettingsScreen from './src/components/Settings/SettingsScreen';
import LabelsContextProvider from './src/contexts/labels/LabelsContextProvider';
import { PreferencesContext } from './src/contexts/preferences-context';
import usePouchdbDatastore from './src/hooks/use-pouchdb-datastore';
import usePreferences from './src/hooks/use-preferences';
export type AppParamList = {
HomeScreen: undefined;
ProjectScreen: undefined;
SettingsScreen: undefined;
LoadingScreen: undefined;
};
export type AppNavProps<T extends keyof AppParamList> = {
navigation: BottomTabNavigationProp<AppParamList, T>;
route: RouteProp<AppParamList, T>;
};
enableScreens();
const Tab = createBottomTabNavigator();
export default function App(): ReactElement {
const preferences = usePreferences();
// TODO refactor
const pouchdbDatastore = usePouchdbDatastore('');
const deleteProject = useCallback(async (project: string) => {
preferences.removeProject(project);
await pouchdbDatastore?.destroyDb(project);
}, [preferences, pouchdbDatastore]);
const initialRouteName = preferences.preferences.currentProject ? 'ProjectScreen' : 'HomeScreen';
if (preferences && pouchdbDatastore) {
return (
<SafeAreaProvider>
<PreferencesContext.Provider value={ preferences }>
<LabelsContextProvider>
<ToastProvider>
<NavigationContainer>
<Tab.Navigator
initialRouteName={ initialRouteName }
screenOptions={ { unmountOnBlur: true, tabBarVisible: false } }
>
<Tab.Screen name="HomeScreen">
{ ({ navigation }) => <HomeScreen
deleteProject={ deleteProject }
navigate={ (screen: string) => navigation.navigate(screen) }
/> }
</Tab.Screen>
<Tab.Screen name="ProjectScreen">
{ () => preferences.preferences.currentProject && <ProjectScreen /> }
</Tab.Screen>
<Tab.Screen name="SettingsScreen">
{ (props) => <SettingsScreen { ...props } /> }
</Tab.Screen>
<Tab.Screen name="LoadingScreen">
{ ({ navigation }) => preferences.preferences.currentProject && <LoadingScreen
navigation={ navigation }
/> }
</Tab.Screen>
</Tab.Navigator>
</NavigationContainer>
<Toast />
</ToastProvider>
</LabelsContextProvider>
</PreferencesContext.Provider>
</SafeAreaProvider>
);
} else {
return <AppLoading />;
}
}