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

545 lines
15 KiB
JavaScript
Raw Normal View History

2020-02-02 20:44:17 +05:00
import React, {useEffect, useState} from 'react';
2019-12-04 11:40:59 +05:00
import {
2020-01-18 01:04:33 +05:00
Platform,
2019-12-04 11:40:59 +05:00
ScrollView,
2020-01-18 01:04:33 +05:00
StatusBar,
2019-12-04 11:40:59 +05:00
Text,
TouchableOpacity,
2020-01-18 01:04:33 +05:00
View,
2019-12-04 11:40:59 +05:00
} from 'react-native';
2020-01-18 01:04:33 +05:00
import FastStorage from 'react-native-fast-storage';
import Icon from 'react-native-vector-icons/Feather';
2020-02-02 23:43:46 +05:00
import {db, DDS} from '../../../App';
2019-12-04 11:40:59 +05:00
import {
2020-01-17 21:06:15 +05:00
ACCENT,
COLOR_SCHEME,
2020-01-18 01:04:33 +05:00
COLOR_SCHEME_DARK,
COLOR_SCHEME_LIGHT,
2020-02-02 23:43:46 +05:00
normalize,
2020-01-18 01:04:33 +05:00
opacity,
pv,
2020-01-17 21:06:15 +05:00
setColorScheme,
2020-01-18 01:04:33 +05:00
SIZE,
WEIGHT,
2019-12-04 11:40:59 +05:00
} from '../../common/common';
2020-01-24 23:13:09 +05:00
import {useTracked} from '../../provider';
import {ACTIONS} from '../../provider/actions';
2020-02-02 23:43:46 +05:00
import {eSendEvent} from '../../services/eventManager';
2020-01-25 23:24:01 +05:00
import {
eOpenLoginDialog,
eOpenModalMenu,
eSendSideMenuOverlayRef,
} from '../../services/events';
2020-02-02 23:43:46 +05:00
import NavigationService from '../../services/NavigationService';
import {AnimatedSafeAreaView} from '../../views/Home';
2020-01-17 21:06:15 +05:00
2020-01-22 02:50:38 +05:00
export const Menu = ({
close = () => {},
hide,
update = () => {},
noTextMode = false,
}) => {
2020-01-17 21:26:01 +05:00
const [state, dispatch] = useTracked();
const {colors} = state;
2020-02-02 20:44:17 +05:00
const [tags, setTags] = useState([]);
2020-01-17 00:23:16 +05:00
// todo
2020-01-20 16:24:01 +05:00
let overlayRef;
2020-01-17 21:06:15 +05:00
function changeColorScheme(colors = COLOR_SCHEME, accent = ACCENT) {
let newColors = setColorScheme(colors, accent);
StatusBar.setBarStyle(newColors.night ? 'light-content' : 'dark-content');
dispatch({type: ACTIONS.THEME, colors: newColors});
}
2019-12-07 12:05:15 +05:00
2020-02-02 20:44:17 +05:00
useEffect(() => {
let allTags = db.getTags();
let tagsToAdd = [];
allTags.sort((a, b) => {
return a.count > b.count;
});
setTags([...allTags]);
}, []);
const listItems = [
{
name: 'Home',
icon: 'home',
2020-01-18 00:46:09 +05:00
func: () => NavigationService.navigate('Home'),
close: true,
},
{
name: 'Notebooks',
icon: 'book',
func: () =>
2020-01-18 00:46:09 +05:00
NavigationService.navigate('Folders', {
title: 'Notebooks',
canGoBack: false,
}),
close: true,
},
{
name: 'Lists',
icon: 'list',
2020-01-18 00:46:09 +05:00
func: () => NavigationService.navigate('Lists'),
close: true,
},
{
name: 'Favorites',
icon: 'star',
2020-01-18 00:46:09 +05:00
func: () => NavigationService.navigate('Favorites'),
close: true,
},
{
name: 'Dark Mode',
icon: 'moon',
func: () => {
if (!colors.night) {
2020-01-14 20:48:03 +05:00
FastStorage.setItem('theme', JSON.stringify({night: true}));
changeColorScheme(COLOR_SCHEME_DARK);
} else {
2020-01-14 20:48:03 +05:00
FastStorage.setItem('theme', JSON.stringify({night: false}));
changeColorScheme(COLOR_SCHEME_LIGHT);
}
},
switch: true,
on: colors.night ? true : false,
close: false,
},
{
name: 'Trash',
icon: 'trash',
2020-01-18 00:46:09 +05:00
func: () => NavigationService.navigate('Trash'),
close: true,
},
{
name: 'Settings',
icon: 'settings',
2020-01-18 00:46:09 +05:00
func: () => NavigationService.navigate('Settings'),
close: true,
},
];
2019-12-07 12:05:15 +05:00
return (
<AnimatedSafeAreaView
transition="backgroundColor"
2020-01-08 11:53:38 +05:00
duration={400}
2019-12-07 12:05:15 +05:00
style={{
2019-12-04 11:40:59 +05:00
height: '100%',
2019-12-07 12:05:15 +05:00
opacity: hide ? 0 : 1,
2019-12-21 10:38:40 +05:00
backgroundColor: colors.shade,
2020-01-22 02:50:38 +05:00
width: '100%',
2020-01-24 22:52:34 +05:00
borderRightWidth: noTextMode ? 1 : 0,
borderRightColor: noTextMode ? colors.accent : 'transparent',
2019-12-04 11:40:59 +05:00
}}>
2019-12-10 22:03:39 +05:00
<View
style={{
2020-01-22 02:50:38 +05:00
minHeight: 2,
2019-12-10 22:03:39 +05:00
width: '100%',
2020-01-27 18:50:17 +05:00
paddingHorizontal: noTextMode ? 0 : 12,
2020-01-22 02:50:38 +05:00
height: 50,
marginBottom: 0,
alignItems: 'center',
flexDirection: 'row',
justifyContent: noTextMode ? 'center' : 'space-between',
marginTop:
Platform.OS == 'ios'
? 0
: DDS.isTab
? noTextMode
? StatusBar.currentHeight
: 0
: StatusBar.currentHeight,
}}>
{noTextMode ? null : (
<Text
style={{
fontSize: SIZE.xxl,
fontFamily: WEIGHT.bold,
color: colors.accent,
}}>
notesnook
</Text>
)}
{DDS.isTab ? (
<TouchableOpacity
onPress={() => {
2020-01-25 23:24:01 +05:00
noTextMode ? eSendEvent(eOpenModalMenu) : close();
2020-01-22 02:50:38 +05:00
}}
style={{
alignItems: 'center',
height: 35,
justifyContent: 'center',
}}>
<Icon
style={{
marginTop: noTextMode ? 0 : 7.5,
}}
name={noTextMode ? 'menu' : 'x'}
size={noTextMode ? SIZE.lg : SIZE.xxl}
color={colors.pri}
/>
</TouchableOpacity>
) : null}
</View>
{DDS.isTab ? null : (
<View
ref={ref => (overlayRef = ref)}
onLayout={() => {
2020-01-25 23:24:01 +05:00
eSendEvent(eSendSideMenuOverlayRef, {ref: overlayRef});
2020-01-22 02:50:38 +05:00
}}
style={{
height: '100%',
width: '100%',
position: 'absolute',
zIndex: 999,
backgroundColor: colors.bg,
2020-01-27 00:25:19 +05:00
display: 'flex',
transform: [
{
translateX: 0,
},
],
2020-01-22 02:50:38 +05:00
}}
/>
)}
2019-12-14 16:00:16 +05:00
2019-12-07 12:05:15 +05:00
<ScrollView
2020-01-09 19:49:06 +05:00
contentContainerStyle={{minHeight: '80%'}}
2020-01-22 02:50:38 +05:00
style={{
2020-01-27 18:50:17 +05:00
paddingHorizontal: noTextMode ? 0 : 12,
2020-01-22 02:50:38 +05:00
}}
2020-01-08 11:53:38 +05:00
showsVerticalScrollIndicator={false}>
2019-12-07 12:05:15 +05:00
<View>
<View>
2020-01-22 02:50:38 +05:00
{listItems.map((item, index) => (
2019-12-07 12:05:15 +05:00
<TouchableOpacity
key={item.name}
2020-01-07 16:26:30 +05:00
activeOpacity={opacity / 2}
2019-12-07 12:05:15 +05:00
onPress={() => {
item.close === false ? null : close();
2019-12-11 01:10:04 +05:00
2019-12-07 12:05:15 +05:00
item.func();
}}
2019-12-04 11:40:59 +05:00
style={{
2019-12-07 12:05:15 +05:00
width: '100%',
alignSelf: 'center',
2019-12-04 11:40:59 +05:00
flexDirection: 'row',
2020-01-22 02:50:38 +05:00
justifyContent: noTextMode ? 'center' : 'space-between',
2019-12-14 16:00:16 +05:00
alignItems: 'center',
2020-01-27 18:50:17 +05:00
paddingBottom: noTextMode ? pv + 2 : normalize(15),
paddingTop:
index === 0 ? pv : noTextMode ? pv + 2 : normalize(15),
2019-12-04 11:40:59 +05:00
}}>
2019-12-07 12:05:15 +05:00
<View
2019-12-04 11:40:59 +05:00
style={{
2019-12-07 12:05:15 +05:00
flexDirection: 'row',
2019-12-14 16:00:16 +05:00
alignItems: 'center',
2019-12-04 11:40:59 +05:00
}}>
2019-12-07 12:05:15 +05:00
<Icon
style={{
2020-01-27 18:50:17 +05:00
minWidth: noTextMode ? 5 : 30,
2019-12-07 12:05:15 +05:00
}}
name={item.icon}
2019-12-21 10:38:40 +05:00
color={colors.pri}
2019-12-07 12:05:15 +05:00
size={SIZE.md}
/>
2020-01-22 02:50:38 +05:00
{noTextMode ? null : (
<Text
style={{
fontFamily: WEIGHT.regular,
fontSize: SIZE.sm,
color: colors.pri,
}}>
{item.name}
</Text>
)}
2019-12-07 12:05:15 +05:00
</View>
2020-01-22 02:50:38 +05:00
{item.switch && !noTextMode ? (
2019-12-07 12:05:15 +05:00
<Icon
size={SIZE.lg}
color={item.on ? colors.accent : colors.icon}
name={item.on ? 'toggle-right' : 'toggle-left'}
/>
) : (
undefined
)}
</TouchableOpacity>
))}
</View>
2019-12-04 11:40:59 +05:00
2019-12-07 12:05:15 +05:00
<TouchableOpacity
2020-01-07 16:26:30 +05:00
activeOpacity={opacity / 2}
2019-12-07 12:05:15 +05:00
onPress={() => {
close();
NavigationService.navigate('Tags');
}}
2019-12-04 11:40:59 +05:00
style={{
2019-12-07 12:05:15 +05:00
width: '100%',
alignSelf: 'center',
2019-12-04 11:40:59 +05:00
flexDirection: 'row',
2020-01-22 02:50:38 +05:00
justifyContent: noTextMode ? 'center' : 'space-between',
2019-12-04 11:40:59 +05:00
alignItems: 'flex-end',
2020-01-27 18:50:17 +05:00
marginTop: noTextMode ? pv : normalize(15),
2019-12-04 11:40:59 +05:00
}}>
2019-12-07 12:05:15 +05:00
<View
2019-12-04 11:40:59 +05:00
style={{
flexDirection: 'row',
2020-01-22 02:50:38 +05:00
justifyContent: noTextMode ? 'center' : 'flex-start',
2019-12-14 16:00:16 +05:00
alignItems: 'center',
2019-12-04 11:40:59 +05:00
}}>
2019-12-07 12:05:15 +05:00
<Icon
style={{
2020-01-27 18:50:17 +05:00
minWidth: noTextMode ? 5 : 30,
2019-12-07 12:05:15 +05:00
}}
name="tag"
2019-12-21 11:12:55 +05:00
color={colors.pri}
2019-12-07 12:05:15 +05:00
size={SIZE.md}
/>
2020-01-22 02:50:38 +05:00
{noTextMode ? null : (
2019-12-07 12:05:15 +05:00
<Text
2019-12-04 11:40:59 +05:00
style={{
2019-12-07 12:05:15 +05:00
fontFamily: WEIGHT.regular,
2020-01-22 02:50:38 +05:00
fontSize: SIZE.sm,
color: colors.pri,
2019-12-07 12:05:15 +05:00
}}>
2020-01-22 02:50:38 +05:00
Tags
2019-12-07 12:05:15 +05:00
</Text>
2020-01-22 02:50:38 +05:00
)}
</View>
</TouchableOpacity>
{noTextMode ? null : (
<View
style={{
flexDirection: 'row',
flexWrap: 'wrap',
marginBottom: 0,
}}>
2020-02-02 20:44:17 +05:00
{tags.map(item => (
2020-01-22 02:50:38 +05:00
<TouchableOpacity
2020-02-02 20:44:17 +05:00
key={item.title}
2020-01-22 02:50:38 +05:00
activeOpacity={opacity / 2}
onPress={() => {
close();
NavigationService.navigate('Notes', {
2020-02-03 12:31:18 +05:00
title: item.title,
2020-02-02 20:44:17 +05:00
tag: item,
type: 'tag',
2020-01-22 02:50:38 +05:00
});
}}
style={{
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
padding: 5,
paddingLeft: 2.5,
2020-02-02 20:44:17 +05:00
marginTop: 5,
2020-01-22 02:50:38 +05:00
}}>
2020-02-02 23:43:46 +05:00
<Text
style={{
fontFamily: WEIGHT.regular,
fontSize: SIZE.xs + 1,
color: colors.accent,
}}>
#
</Text>
2020-01-22 02:50:38 +05:00
<Text
style={{
fontFamily: WEIGHT.regular,
fontSize: SIZE.xs + 1,
color: colors.icon,
}}>
2020-02-02 23:43:46 +05:00
{item.title}
2020-01-22 02:50:38 +05:00
</Text>
2020-02-02 20:44:17 +05:00
{item.count > 1 ? (
<Text
style={{
color: 'white',
backgroundColor: colors.accent,
fontSize: SIZE.xxs - 2,
minWidth: 10,
minHeight: 10,
marginTop: -10,
borderRadius: 2,
textAlign: 'center',
padding: 0,
paddingHorizontal: 1,
}}>
{item.count}
</Text>
) : null}
2020-01-22 02:50:38 +05:00
</TouchableOpacity>
))}
</View>
)}
2019-12-04 11:40:59 +05:00
2020-01-08 11:53:38 +05:00
<View
style={{
2020-01-22 02:50:38 +05:00
flexDirection: noTextMode ? 'column' : 'row',
flexWrap: noTextMode ? 'nowrap' : 'wrap',
2020-01-27 18:50:17 +05:00
marginTop: pv / 2,
marginBottom: pv / 2,
2019-12-04 11:40:59 +05:00
}}>
2019-12-07 12:05:15 +05:00
{['red', 'yellow', 'green', 'blue', 'purple', 'orange', 'gray'].map(
item => (
<TouchableOpacity
key={item}
2020-01-07 16:26:30 +05:00
activeOpacity={opacity / 2}
2019-12-07 12:05:15 +05:00
style={{
flexDirection: 'row',
2020-01-22 02:50:38 +05:00
justifyContent: noTextMode ? 'center' : 'flex-start',
2019-12-07 12:05:15 +05:00
alignItems: 'center',
2020-01-22 02:50:38 +05:00
margin: noTextMode ? 0 : 5,
2020-01-27 18:50:17 +05:00
marginTop: normalize(15),
2019-12-07 12:05:15 +05:00
}}>
<View
style={{
2020-01-27 18:50:17 +05:00
width: noTextMode ? SIZE.md : normalize(30),
height: noTextMode ? SIZE.md : normalize(30),
2019-12-07 12:05:15 +05:00
backgroundColor: item,
borderRadius: 100,
2020-02-02 20:44:17 +05:00
}}></View>
<Text
style={{
color: colors.pri,
fontSize: SIZE.xxs - 2,
minWidth: 10,
minHeight: 10,
borderRadius: 2,
textAlign: 'center',
padding: 0,
paddingHorizontal: 1,
position: 'absolute',
top: -5,
right: -10,
}}></Text>
2019-12-07 12:05:15 +05:00
</TouchableOpacity>
),
)}
2020-01-08 11:53:38 +05:00
</View>
2019-12-07 12:05:15 +05:00
</View>
2019-12-04 11:40:59 +05:00
2019-12-07 12:05:15 +05:00
{/* <View
2019-12-04 11:40:59 +05:00
style={{
2019-12-07 12:05:15 +05:00
backgroundColor: '#F3A712',
width: '90%',
alignSelf: 'center',
2019-12-04 11:40:59 +05:00
borderRadius: 5,
2019-12-07 12:05:15 +05:00
flexDirection: 'row',
justifyContent: 'space-between',
2019-12-04 11:40:59 +05:00
alignItems: 'center',
2019-12-07 12:05:15 +05:00
height: 40,
paddingHorizontal: ph,
marginVertical: 10,
2019-12-04 11:40:59 +05:00
}}>
<Text
style={{
fontFamily: WEIGHT.medium,
color: 'white',
}}>
2019-12-07 12:05:15 +05:00
Upgrade to Pro
2019-12-04 11:40:59 +05:00
</Text>
2019-12-07 12:05:15 +05:00
<View
style={{
...getElevation(5),
paddingHorizontal: ph,
backgroundColor: 'white',
paddingVertical: pv - 8,
borderRadius: 5,
}}>
<Icon name="star" color="#FCBA04" size={SIZE.lg} />
</View>
</View> */}
2020-01-08 11:53:38 +05:00
</ScrollView>
<View
style={{
width: '100%',
2020-01-27 18:50:17 +05:00
justifyContent: noTextMode ? 'center' : 'space-between',
2020-01-08 11:53:38 +05:00
alignItems: 'center',
alignSelf: 'center',
2020-01-27 18:50:17 +05:00
marginBottom: 15,
2020-01-08 11:53:38 +05:00
flexDirection: 'row',
}}>
<TouchableOpacity
onPress={() => {
close();
2019-12-04 11:40:59 +05:00
2020-01-23 23:18:58 +05:00
DDS.isTab
2020-01-25 23:24:01 +05:00
? eSendEvent(eOpenLoginDialog)
2020-01-23 23:18:58 +05:00
: NavigationService.navigate('Login');
2020-01-08 11:53:38 +05:00
}}
activeOpacity={opacity / 2}
2019-12-04 11:40:59 +05:00
style={{
2020-01-08 11:53:38 +05:00
paddingVertical: pv + 5,
2020-01-27 18:50:17 +05:00
paddingHorizontal: noTextMode ? 0 : 12,
2020-01-08 11:53:38 +05:00
width: '100%',
2020-01-27 18:50:17 +05:00
justifyContent: noTextMode ? 'center' : 'flex-start',
2019-12-07 12:05:15 +05:00
alignItems: 'center',
flexDirection: 'row',
2019-12-04 11:40:59 +05:00
}}>
2020-01-08 11:53:38 +05:00
<Icon name="log-in" color={colors.accent} size={SIZE.lg} />
2019-12-07 12:05:15 +05:00
2020-01-22 02:50:38 +05:00
{noTextMode ? null : (
<Text
style={{
fontFamily: WEIGHT.regular,
color: colors.accent,
fontSize: SIZE.md,
}}>
{' '}Login
</Text>
)}
2020-01-08 11:53:38 +05:00
</TouchableOpacity>
2019-12-07 12:05:15 +05:00
2020-01-08 11:53:38 +05:00
{/* <Text
2019-12-07 12:05:15 +05:00
style={{
fontFamily: WEIGHT.semibold,
color: colors.accent,
fontSize: SIZE.md,
marginTop: 10,
}}>
Hi, Ammar!
</Text>
<Text
style={{
fontFamily: WEIGHT.regular,
color: colors.accent,
fontSize: SIZE.xs,
marginTop: 10,
}}>
80.45/100 MB
</Text> */}
2020-01-08 11:53:38 +05:00
{/* <View
2019-12-07 12:05:15 +05:00
style={{
borderRadius: 2.5,
backgroundColor: colors.accent,
marginTop: 10,
paddingHorizontal: 5,
paddingVertical: 2,
}}>
<Text
style={{
fontFamily: WEIGHT.bold,
fontSize: SIZE.xxs,
color: 'white',
}}>
Basic User
</Text>
</View> */}
2020-01-08 11:53:38 +05:00
</View>
</AnimatedSafeAreaView>
2019-12-07 12:05:15 +05:00
);
};