Files
notesnook/apps/mobile/src/components/header/index.js

227 lines
6.2 KiB
JavaScript
Raw Normal View History

2020-05-10 22:16:55 +05:00
import React, {useEffect, useState, createRef, useCallback} from 'react';
2020-04-25 11:52:08 +05:00
import {
Platform,
TouchableOpacity,
View,
ActivityIndicator,
} from 'react-native';
2020-01-18 01:04:33 +05:00
import * as Animatable from 'react-native-animatable';
2020-05-10 22:16:55 +05:00
import {useSafeArea} from 'react-native-safe-area-context';
2020-04-19 21:27:03 +05:00
import {useTracked} from '../../provider';
2020-05-10 22:16:55 +05:00
import {eSubscribeEvent, eUnSubscribeEvent} from '../../services/eventManager';
import {eScrollEvent, eSetModalNavigator} from '../../services/events';
import {DDS, selection, w} from '../../utils/utils';
import {HeaderMenu} from './HeaderMenu';
import {HeaderTitle} from './HeaderTitle';
2020-02-11 20:07:36 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2020-05-10 22:16:55 +05:00
import {SIZE} from '../../common/common';
2020-04-19 21:27:03 +05:00
import {sideMenuRef} from '../../utils/refs';
2020-05-10 22:16:55 +05:00
import NavigationService from '../../services/NavigationService';
2020-04-19 21:27:03 +05:00
import {moveNoteHideEvent} from '../DialogManager/recievers';
2020-05-10 22:16:55 +05:00
let offsetY = 0;
function useForceUpdate() {
const [, setTick] = useState(0);
const update = useCallback(() => {
setTick(tick => tick + 1);
}, [])
return update;
}
export const Header = ({showSearch,root}) => {
2020-01-17 21:26:01 +05:00
const [state, dispatch] = useTracked();
2020-05-10 22:16:55 +05:00
const {
colors,
syncing,
isLoginNavigator,
preventDefaultMargins,
searchResults,
} = state;
const [hideHeader, setHideHeader] = useState(false);
let headerState = root? state.headerState : state.indHeaderState;
const [isModalNavigator, setIsModalNavigator] = useState(false);
2020-04-20 09:22:27 +05:00
const insets = useSafeArea();
2020-05-10 22:16:55 +05:00
const forceUpdate = useForceUpdate();
const onScroll = y => {
if (searchResults.results.length > 0) return;
if (y < 30) {
setHideHeader(false);
offsetY = y;
}
if (y > offsetY) {
if (y - offsetY < 100) return;
setHideHeader(true);
offsetY = y;
} else {
if (offsetY - y < 50) return;
setHideHeader(false);
offsetY = y;
}
};
const _setModalNavigator = value => {
if (root) return;
forceUpdate();
setIsModalNavigator(value);
};
useEffect(() => {
eSubscribeEvent(eSetModalNavigator, _setModalNavigator);
eSubscribeEvent(eScrollEvent, onScroll);
return () => {
eUnSubscribeEvent(eSetModalNavigator, _setModalNavigator);
eUnSubscribeEvent(eScrollEvent, onScroll);
};
}, []);
2020-04-16 13:57:51 +05:00
2019-11-29 11:31:43 +05:00
return (
2020-01-27 14:01:24 +05:00
<View
2019-11-29 11:31:43 +05:00
style={{
flexDirection: 'row',
2019-12-06 18:13:15 +05:00
zIndex: 10,
2019-12-21 09:46:08 +05:00
height: 50,
2020-01-18 18:14:57 +05:00
marginTop:
Platform.OS === 'ios'
2020-05-10 22:16:55 +05:00
? isModalNavigator && !root
? 0
: insets.top
: isModalNavigator && !root
2020-04-19 21:27:03 +05:00
? 0
2020-04-20 09:22:27 +05:00
: insets.top,
2019-12-21 09:46:08 +05:00
marginBottom: 10,
2019-12-06 18:13:15 +05:00
justifyContent: 'space-between',
2019-11-29 11:31:43 +05:00
alignItems: 'center',
2020-01-08 11:25:26 +05:00
paddingHorizontal: 12,
width: '100%',
2019-11-29 11:31:43 +05:00
}}>
2020-04-25 11:52:08 +05:00
<Animatable.View
transition={['opacity']}
duration={300}
style={{
width: 40,
height: 40,
position: 'absolute',
left: w / 2 - 20,
top: -20,
opacity: syncing ? 1 : 0,
alignSelf: 'center',
alignItems: 'center',
justifyContent: 'center',
zIndex: 999,
}}>
<View
style={{
backgroundColor: colors.bg,
width: 40,
height: 20,
position: 'absolute',
zIndex: 10,
top: 0,
}}
/>
<ActivityIndicator size={25} color={colors.accent} />
</Animatable.View>
2019-12-06 18:13:15 +05:00
<View
style={{
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
}}>
2020-05-10 22:16:55 +05:00
{headerState.canGoBack ? (
2019-12-06 18:13:15 +05:00
<TouchableOpacity
2020-04-19 21:27:03 +05:00
hitSlop={{top: 20, bottom: 20, left: 50, right: 40}}
2019-12-11 15:20:18 +05:00
onPress={() => {
2020-05-10 22:16:55 +05:00
headerState = root? state.headerState : state.indHeaderState;
console.log(headerState.route.name);
if (headerState.navigation && preventDefaultMargins) {
if (headerState.route.name === 'Folders') {
2020-01-18 18:14:57 +05:00
moveNoteHideEvent();
} else {
2020-05-10 22:16:55 +05:00
headerState.navigation.goBack();
2020-01-18 18:14:57 +05:00
}
2020-05-10 22:16:55 +05:00
} else if (headerState.navigation && isLoginNavigator) {
if (headerState.route.name === 'Login') {
2020-01-25 23:24:01 +05:00
eSendEvent(eCloseLoginDialog);
2020-01-23 23:19:47 +05:00
} else {
2020-05-10 22:16:55 +05:00
headerState.navigation.goBack();
2020-01-23 23:19:47 +05:00
}
2020-01-18 18:14:57 +05:00
} else {
NavigationService.goBack();
}
2019-12-11 15:20:18 +05:00
}}
2019-12-06 18:13:15 +05:00
style={{
2020-01-07 16:26:30 +05:00
justifyContent: 'center',
2019-12-11 15:20:18 +05:00
alignItems: 'flex-start',
2019-12-07 12:05:15 +05:00
height: 40,
2020-01-07 16:26:30 +05:00
width: 50,
2019-12-06 18:13:15 +05:00
}}>
2019-12-07 12:05:15 +05:00
<Icon
2019-12-11 15:20:18 +05:00
style={{
2020-01-08 11:31:43 +05:00
marginLeft: -5,
2019-12-11 15:20:18 +05:00
}}
2019-12-07 12:05:15 +05:00
color={colors.pri}
name={'chevron-left'}
2020-01-07 16:26:30 +05:00
size={SIZE.xxxl - 3}
2019-12-07 12:05:15 +05:00
/>
2019-12-06 18:13:15 +05:00
</TouchableOpacity>
) : (
2020-04-19 21:27:03 +05:00
undefined
)}
2020-05-10 22:16:55 +05:00
{headerState.menu && !DDS.isTab ? (
2019-12-11 15:20:18 +05:00
<TouchableOpacity
2020-04-19 21:27:03 +05:00
hitSlop={{top: 20, bottom: 20, left: 50, right: 40}}
2019-12-11 15:20:18 +05:00
onPress={() => {
2020-05-06 15:06:00 +05:00
sideMenuRef.current?.openDrawer();
2019-12-11 15:20:18 +05:00
}}
style={{
2019-12-14 16:00:16 +05:00
justifyContent: 'center',
2019-12-11 15:20:18 +05:00
alignItems: 'flex-start',
height: 40,
2020-01-01 00:04:59 +05:00
width: 60,
2019-12-11 15:20:18 +05:00
}}>
2020-02-11 20:07:36 +05:00
<Icon color={colors.pri} name={'menu'} size={SIZE.xxxl} />
2019-12-11 15:20:18 +05:00
</TouchableOpacity>
) : (
2020-04-19 21:27:03 +05:00
undefined
)}
2019-12-06 18:13:15 +05:00
2020-05-10 22:16:55 +05:00
<HeaderTitle root={root} />
2019-12-06 18:13:15 +05:00
</View>
2020-01-01 00:04:59 +05:00
<View
2019-12-06 18:13:15 +05:00
style={{
2020-01-01 00:04:59 +05:00
flexDirection: 'row',
alignItems: 'center',
2019-12-06 18:13:15 +05:00
}}>
2020-01-01 00:04:59 +05:00
<Animatable.View
transition="opacity"
useNativeDriver={true}
2020-01-01 00:04:59 +05:00
duration={500}
style={{
2020-05-10 22:16:55 +05:00
opacity: hideHeader ? 1 : 0,
2020-01-01 00:04:59 +05:00
}}>
<TouchableOpacity
onPress={() => showSearch()}
style={{
justifyContent: 'center',
alignItems: 'flex-end',
height: 40,
width: 60,
paddingRight: 0,
}}>
2020-02-11 20:07:36 +05:00
<Icon name={'magnify'} size={SIZE.xl} color={colors.icon} />
2020-01-01 00:04:59 +05:00
</TouchableOpacity>
</Animatable.View>
2020-04-25 11:52:08 +05:00
2020-05-10 22:16:55 +05:00
<HeaderMenu />
2020-01-01 00:04:59 +05:00
</View>
2020-01-27 14:01:24 +05:00
</View>
2019-11-29 11:31:43 +05:00
);
};