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

280 lines
7.8 KiB
JavaScript
Raw Normal View History

2020-04-19 21:27:03 +05:00
import React, {createRef} from 'react';
2020-04-25 11:52:08 +05:00
import {
Platform,
StatusBar,
Text,
TouchableOpacity,
View,
ActivityIndicator,
} from 'react-native';
2020-01-18 01:04:33 +05:00
import * as Animatable from 'react-native-animatable';
2020-04-19 21:27:03 +05:00
import {SIZE, WEIGHT} from '../../common/common';
import {useTracked} from '../../provider';
import {eSendEvent} from '../../services/eventManager';
import {eCloseLoginDialog} from '../../services/events';
2020-01-18 01:04:33 +05:00
import NavigationService from '../../services/NavigationService';
2020-04-25 11:52:08 +05:00
import {DDS, w} from '../../utils/utils';
2020-02-11 20:07:36 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2020-04-19 21:27:03 +05:00
import Menu, {MenuItem, MenuDivider} from 'react-native-material-menu';
import {ACTIONS} from '../../provider/actions';
import {sideMenuRef} from '../../utils/refs';
import {moveNoteHideEvent} from '../DialogManager/recievers';
2020-04-20 09:22:27 +05:00
import {useSafeArea} from 'react-native-safe-area-context';
2020-04-16 13:57:51 +05:00
const menuRef = createRef();
2019-12-06 18:13:15 +05:00
export const Header = ({
heading,
canGoBack = true,
hide,
showSearch,
2019-12-11 15:20:18 +05:00
menu,
2020-01-01 01:26:23 +05:00
verticalMenu = false,
2020-01-18 18:14:57 +05:00
preventDefaultMargins,
navigation = null,
2020-01-23 23:19:47 +05:00
isLoginNavigator,
2020-02-07 00:26:44 +05:00
headerColor,
2020-05-06 02:26:35 +05:00
route,
2019-12-06 18:13:15 +05:00
}) => {
2020-01-17 21:26:01 +05:00
const [state, dispatch] = useTracked();
2020-04-25 11:52:08 +05:00
const {colors, syncing} = state;
2020-04-20 09:22:27 +05:00
const insets = useSafeArea();
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-04-20 09:22:27 +05:00
? insets.top
2020-01-23 23:19:47 +05:00
: preventDefaultMargins || isLoginNavigator
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',
}}>
{canGoBack ? (
<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-01-18 18:14:57 +05:00
if (navigation && preventDefaultMargins) {
2020-05-06 02:26:35 +05:00
if (route.name === 'Folders') {
2020-01-18 18:14:57 +05:00
moveNoteHideEvent();
} else {
navigation.goBack();
}
2020-01-23 23:19:47 +05:00
} else if (navigation && isLoginNavigator) {
2020-05-06 02:26:35 +05:00
if (route.name === 'Login') {
2020-01-25 23:24:01 +05:00
eSendEvent(eCloseLoginDialog);
2020-01-23 23:19:47 +05:00
} else {
navigation.goBack();
}
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-01-22 02:51:24 +05:00
{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-03-15 10:03:13 +05:00
sideMenuRef.current?.openMenu(true);
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
2019-12-21 09:46:08 +05:00
<Text
2019-12-06 18:13:15 +05:00
style={{
2019-12-21 09:46:08 +05:00
fontSize: SIZE.xl,
2020-02-07 00:26:44 +05:00
color: headerColor ? headerColor : colors.pri,
2019-12-06 18:13:15 +05:00
fontFamily: WEIGHT.bold,
}}>
2020-02-03 12:31:18 +05:00
<Text
style={{
color: colors.accent,
}}>
{heading.slice(0, 1) === '#' ? '#' : null}
</Text>
{heading.slice(0, 1) === '#' ? heading.slice(1) : heading}
2019-12-21 09:46:08 +05:00
</Text>
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={{
opacity: hide ? 1 : 0,
}}>
<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-01-01 01:26:23 +05:00
{verticalMenu ? (
2020-02-20 10:59:30 +05:00
<Menu
ref={menuRef}
2020-01-01 01:26:23 +05:00
style={{
2020-02-20 10:59:30 +05:00
borderRadius: 5,
2020-03-15 11:43:33 +05:00
backgroundColor: colors.bg,
2020-02-20 10:59:30 +05:00
}}
button={
<TouchableOpacity
onPress={() => {
menuRef.current?.show();
}}
style={{
justifyContent: 'center',
alignItems: 'flex-end',
height: 40,
width: 60,
}}>
<Icon name="sort" size={SIZE.xl} color={colors.icon} />
</TouchableOpacity>
}>
<MenuItem
textStyle={{
color: colors.icon,
fontSize: 12,
}}>
Sort by:
</MenuItem>
<MenuDivider />
<MenuItem
2020-02-20 20:02:37 +05:00
textStyle={{
fontFamily: WEIGHT.regular,
2020-03-15 11:43:33 +05:00
color: colors.pri,
2020-02-20 20:02:37 +05:00
}}
2020-02-20 10:59:30 +05:00
onPress={() => {
2020-04-19 21:27:03 +05:00
dispatch({type: ACTIONS.NOTES, sort: null});
2020-02-20 10:59:30 +05:00
menuRef.current?.hide();
}}>
Default
</MenuItem>
<MenuItem
2020-02-20 20:02:37 +05:00
textStyle={{
fontFamily: WEIGHT.regular,
2020-03-15 11:43:33 +05:00
color: colors.pri,
2020-02-20 20:02:37 +05:00
}}
2020-02-20 10:59:30 +05:00
onPress={() => {
2020-04-19 21:27:03 +05:00
dispatch({type: ACTIONS.NOTES, sort: 'abc'});
2020-02-20 10:59:30 +05:00
menuRef.current?.hide();
}}>
Alphabetical
</MenuItem>
<MenuItem
2020-02-20 20:02:37 +05:00
textStyle={{
fontFamily: WEIGHT.regular,
2020-03-15 11:43:33 +05:00
color: colors.pri,
2020-02-20 20:02:37 +05:00
}}
2020-02-20 10:59:30 +05:00
onPress={() => {
2020-04-19 21:27:03 +05:00
dispatch({type: ACTIONS.NOTES, sort: 'year'});
2020-02-20 10:59:30 +05:00
menuRef.current?.hide();
}}>
By year
</MenuItem>
<MenuItem
2020-02-20 20:02:37 +05:00
textStyle={{
fontFamily: WEIGHT.regular,
2020-03-15 11:43:33 +05:00
color: colors.pri,
2020-02-20 20:02:37 +05:00
}}
2020-02-20 10:59:30 +05:00
onPress={() => {
2020-04-19 21:27:03 +05:00
dispatch({type: ACTIONS.NOTES, sort: 'month'});
2020-02-20 10:59:30 +05:00
menuRef.current?.hide();
}}>
By month
</MenuItem>
<MenuItem
2020-02-20 20:02:37 +05:00
textStyle={{
fontFamily: WEIGHT.regular,
2020-03-15 11:43:33 +05:00
color: colors.pri,
2020-02-20 20:02:37 +05:00
}}
2020-02-20 10:59:30 +05:00
onPress={() => {
2020-04-19 21:27:03 +05:00
dispatch({type: ACTIONS.NOTES, sort: 'week'});
2020-02-20 10:59:30 +05:00
menuRef.current?.hide();
}}>
By week
</MenuItem>
</Menu>
2020-01-01 01:26:23 +05:00
) : null}
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
);
};