Files
notesnook/apps/mobile/src/navigation/navigationstack.js

96 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-01-22 12:57:05 +05:00
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
2020-10-13 17:02:14 +05:00
import * as React from 'react';
2022-02-28 13:48:59 +05:00
import Container from '../components/container';
2022-02-28 23:25:18 +05:00
import { useThemeStore } from '../stores/theme';
import { useSelectionStore } from '../stores/stores';
import { eSendEvent } from '../services/event-manager';
import Navigation from '../services/navigation';
2022-01-22 12:57:05 +05:00
import { history } from '../utils';
2022-02-28 13:48:59 +05:00
import { hideAllTooltips } from '../utils/hooks/use-tooltip';
import { MMKV } from '../utils/database/mmkv';
import { rootNavigatorRef } from '../utils/global-refs';
2022-02-28 15:32:55 +05:00
import Favorites from '../screens/favorites';
import Notebooks from '../screens/notebooks';
import Home from '../screens/home';
import Notebook from '../screens/notebook';
import Notes from '../screens/notes';
import { Search } from '../screens/search';
import Settings from '../screens/settings';
import Tags from '../screens/tags';
import Trash from '../screens/trash';
2020-10-13 17:02:14 +05:00
2021-06-03 12:04:55 +05:00
const Stack = createNativeStackNavigator();
2022-02-28 15:32:55 +05:00
export const NavigationStack = React.memo(
2020-11-14 10:04:11 +05:00
() => {
2022-02-28 23:25:18 +05:00
const colors = useThemeStore(state => state.colors);
2021-06-15 18:26:46 +05:00
const [render, setRender] = React.useState(false);
2021-06-05 21:10:20 +05:00
const clearSelection = useSelectionStore(state => state.clearSelection);
2022-02-19 11:13:08 +05:00
const homepage = React.useRef('Notes');
2022-01-08 11:33:35 +05:00
2021-02-15 11:00:13 +05:00
const onStateChange = React.useCallback(() => {
2021-04-11 14:04:14 +05:00
if (history.selectionMode) {
2021-12-31 12:35:39 +05:00
clearSelection(true);
2021-04-11 14:04:14 +05:00
}
2022-02-19 13:48:48 +05:00
hideAllTooltips();
2021-02-15 11:00:13 +05:00
eSendEvent('navigate');
});
2020-11-26 22:19:15 +05:00
2021-06-15 18:26:46 +05:00
React.useEffect(() => {
2022-01-08 11:33:35 +05:00
(async () => {
2022-04-13 03:00:04 +05:00
let settings = MMKV.getString('appSettings');
2022-01-08 11:33:35 +05:00
if (settings) {
settings = JSON.parse(settings);
2022-02-19 11:13:08 +05:00
homepage.current = settings.homepage;
2022-01-08 11:33:35 +05:00
}
2021-02-19 16:03:18 +05:00
setRender(true);
2021-03-05 11:50:51 +05:00
Navigation.setHeaderState(
2021-06-15 18:26:46 +05:00
settings.homepage,
2021-02-27 12:25:30 +05:00
{
2022-01-08 11:33:35 +05:00
menu: true
2021-02-27 12:25:30 +05:00
},
{
2021-06-15 18:26:46 +05:00
heading: settings.homepage,
2022-01-08 11:33:35 +05:00
id: settings.homepage.toLowerCase() + '_navigation'
}
2021-02-27 12:25:30 +05:00
);
2022-01-08 11:33:35 +05:00
})();
}, []);
2021-02-19 16:03:18 +05:00
2020-11-14 10:04:11 +05:00
return (
<Container root={true}>
2020-12-31 19:53:21 +05:00
<NavigationContainer
2021-02-15 11:00:13 +05:00
onStateChange={onStateChange}
2020-12-31 19:53:21 +05:00
independent={true}
2022-01-22 12:57:05 +05:00
ref={rootNavigatorRef}
>
2021-09-13 08:53:16 +05:00
{render ? (
2021-02-19 16:03:18 +05:00
<Stack.Navigator
2022-02-19 11:13:08 +05:00
initialRouteName={homepage.current}
2021-02-19 16:03:18 +05:00
screenOptions={{
headerShown: false,
gestureEnabled: false,
2022-01-08 11:33:35 +05:00
animation: 'none',
contentStyle: {
backgroundColor: colors.bg
}
2022-01-22 12:57:05 +05:00
}}
>
2021-02-19 16:03:18 +05:00
<Stack.Screen name="Notes" component={Home} />
2022-02-28 15:32:55 +05:00
<Stack.Screen name="Notebooks" component={Notebooks} />
2021-02-19 16:03:18 +05:00
<Stack.Screen name="Favorites" component={Favorites} />
<Stack.Screen name="Trash" component={Trash} />
2021-07-17 20:32:29 +05:00
<Stack.Screen name="NotesPage" component={Notes} />
2021-02-19 16:03:18 +05:00
<Stack.Screen name="Tags" component={Tags} />
2021-07-17 20:32:29 +05:00
<Stack.Screen name="Notebook" component={Notebook} />
<Stack.Screen name="Search" component={Search} />
2022-04-06 16:15:49 +05:00
<Stack.Screen name="Settings" component={Settings} />
2021-02-19 16:03:18 +05:00
</Stack.Navigator>
2021-09-13 08:53:16 +05:00
) : null}
2020-11-14 10:04:11 +05:00
</NavigationContainer>
</Container>
);
},
2022-01-08 11:33:35 +05:00
() => true
2020-11-14 10:04:11 +05:00
);