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

241 lines
6.5 KiB
JavaScript
Raw Normal View History

2020-07-26 13:32:17 +05:00
import React, {useCallback, useEffect, useState} from 'react';
import {
ActivityIndicator,
Platform,
TouchableOpacity,
View,
} from 'react-native';
2020-01-18 01:04:33 +05:00
import * as Animatable from 'react-native-animatable';
2020-07-26 13:32:17 +05:00
import {useSafeArea} from 'react-native-safe-area-context';
2020-02-11 20:07:36 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2020-07-26 13:32:17 +05:00
import {SIZE} from '../../common/common';
import {useTracked} from '../../provider';
import {
eSendEvent,
eSubscribeEvent,
eUnSubscribeEvent,
} from '../../services/eventManager';
import {
eCloseLoginDialog,
eScrollEvent,
eSetModalNavigator,
} from '../../services/events';
2020-05-10 22:16:55 +05:00
import NavigationService from '../../services/NavigationService';
2020-07-26 13:32:17 +05:00
import {sideMenuRef} from '../../utils/refs';
import {DDS, w} from '../../utils/utils';
import {moveNoteHideEvent} from '../DialogManager/recievers';
import {HeaderMenu} from './HeaderMenu';
import {HeaderTitle} from './HeaderTitle';
2020-05-10 22:16:55 +05:00
let offsetY = 0;
2020-09-06 16:10:34 +05:00
let timeout = null;
2020-05-10 22:16:55 +05:00
function useForceUpdate() {
const [, setTick] = useState(0);
const update = useCallback(() => {
2020-09-06 16:10:34 +05:00
setTick((tick) => tick + 1);
2020-05-11 01:02:11 +05:00
}, []);
2020-05-10 22:16:55 +05:00
return update;
}
2020-05-11 01:02:11 +05:00
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);
2020-05-11 01:02:11 +05:00
let headerState = root ? state.headerState : state.indHeaderState;
2020-05-10 22:16:55 +05:00
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();
2020-09-06 16:10:34 +05:00
const onScroll = (y) => {
2020-05-10 22:16:55 +05:00
if (searchResults.results.length > 0) return;
if (y < 30) {
setHideHeader(false);
2020-09-06 16:10:34 +05:00
offsetY = y;
2020-05-10 22:16:55 +05:00
}
if (y > offsetY) {
if (y - offsetY < 100) return;
2020-08-17 10:01:32 +05:00
clearTimeout(timeout);
2020-09-06 16:10:34 +05:00
timeout = null;
2020-08-17 10:01:32 +05:00
timeout = setTimeout(() => {
setHideHeader(true);
2020-09-06 16:10:34 +05:00
}, 300);
offsetY = y;
2020-05-10 22:16:55 +05:00
} else {
if (offsetY - y < 50) return;
2020-08-17 10:01:32 +05:00
clearTimeout(timeout);
2020-09-06 16:10:34 +05:00
timeout = null;
2020-08-17 10:01:32 +05:00
timeout = setTimeout(() => {
setHideHeader(false);
2020-09-06 16:10:34 +05:00
}, 300);
offsetY = y;
2020-05-10 22:16:55 +05:00
}
};
2020-09-06 16:10:34 +05:00
const _setModalNavigator = (value) => {
2020-05-10 22:16:55 +05:00
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',
2020-08-17 10:01:32 +05:00
zIndex: 11,
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%',
2020-09-06 16:10:34 +05:00
backgroundColor: colors.bg,
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-11 01:02:11 +05:00
headerState = root ? state.headerState : state.indHeaderState;
2020-05-10 22:16:55 +05:00
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-11 01:02:11 +05:00
} else if (headerState.navigation && isModalNavigator) {
2020-05-10 22:16:55 +05:00
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-05-21 18:23:08 +05:00
width: 60,
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}
2020-08-16 18:59:07 +05:00
name={'arrow-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-09-06 16:10:34 +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-09-06 16:10:34 +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-09-06 16:10:34 +05:00
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
2020-07-26 13:32:17 +05:00
onPress={() => {
setHideHeader(false);
eSendEvent('showSearch');
}}
2020-01-01 00:04:59 +05:00
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
);
};