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

349 lines
9.5 KiB
JavaScript
Raw Normal View History

2020-01-11 23:06:29 +05:00
import React, {useState, useRef} from 'react';
2020-01-06 19:03:21 +05:00
import {
View,
Text,
TouchableOpacity,
Dimensions,
ScrollView,
2020-01-09 19:48:39 +05:00
TextInput,
FlatList,
Modal,
2020-01-06 19:03:21 +05:00
} from 'react-native';
2020-01-09 19:48:39 +05:00
import {
COLOR_SCHEME,
SIZE,
br,
ph,
pv,
WEIGHT,
opacity,
} from '../../common/common';
2019-12-04 19:38:19 +05:00
import Icon from 'react-native-vector-icons/Feather';
2019-12-21 09:46:08 +05:00
import {
timeSince,
ToastEvent,
SideMenuEvent,
getElevation,
} from '../../utils/utils';
2019-11-28 21:40:37 +05:00
import NavigationService from '../../services/NavigationService';
2019-12-04 19:38:19 +05:00
import Menu, {MenuItem, MenuDivider} from 'react-native-material-menu';
2019-12-05 17:40:09 +05:00
import {Dialog} from '../Dialog';
2019-12-07 08:41:48 +05:00
import {VaultDialog} from '../VaultDialog';
2019-12-11 01:10:04 +05:00
import {db} from '../../../App';
2019-12-14 16:00:16 +05:00
import {DDS} from '../../../App';
2019-12-14 19:26:44 +05:00
import {useAppContext} from '../../provider/useAppContext';
2020-01-09 19:48:39 +05:00
import ActionSheet from '../ActionSheet';
2020-01-09 20:14:51 +05:00
import {ActionSheetComponent} from '../ActionSheetComponent';
2019-11-17 16:02:19 +05:00
const w = Dimensions.get('window').width;
const h = Dimensions.get('window').height;
2019-12-05 17:40:09 +05:00
2019-11-17 16:02:19 +05:00
const NoteItem = props => {
const {colors, updateDB} = useAppContext();
2019-12-05 17:40:09 +05:00
const [visible, setVisible] = useState(false);
2019-12-07 08:41:48 +05:00
const [vaultDialog, setVaultDialog] = useState(false);
2020-01-05 18:03:40 +05:00
const [unlock, setUnlock] = useState(false);
const [isPerm, setIsPerm] = useState(false);
2019-11-17 16:02:19 +05:00
const item = props.item;
2020-01-11 23:06:29 +05:00
let actionSheet;
2019-12-09 13:17:40 +05:00
let show = null;
2019-12-05 17:40:09 +05:00
let setMenuRef = {};
2020-01-09 19:48:39 +05:00
let willRefresh = false;
2019-12-09 13:17:40 +05:00
const onMenuHide = () => {
if (show) {
if (show === 'delete') {
setVisible(true);
show = null;
2020-01-05 18:03:40 +05:00
} else if (show == 'lock') {
setVaultDialog(true);
show = null;
} else if (show == 'unlock') {
setUnlock(true);
setIsPerm(true);
2019-12-09 13:17:40 +05:00
setVaultDialog(true);
show = null;
}
}
};
const hideMenu = () => {
setMenuRef[props.index].hide();
};
const showMenu = () => {
setMenuRef[props.index].show();
};
const deleteItem = async () => {
2019-12-11 01:10:04 +05:00
await db.deleteNotes([item]);
2019-12-09 13:17:40 +05:00
ToastEvent.show('Note moved to trash', 'success', 3000);
setVisible(false);
updateDB();
2019-12-09 13:17:40 +05:00
};
2019-11-17 16:02:19 +05:00
return (
<View
2019-12-21 09:46:08 +05:00
style={[
{
paddingVertical: pv,
justifyContent: 'flex-start',
alignItems: 'center',
flexDirection: 'row',
2020-01-10 18:46:52 +05:00
marginHorizontal: 12,
2020-01-11 23:06:29 +05:00
width: props.width,
2020-01-10 18:46:52 +05:00
paddingRight: 6,
2019-12-21 09:46:08 +05:00
alignSelf: 'center',
borderBottomWidth: 1,
borderBottomColor: colors.nav,
},
props.customStyle ? props.customStyle : {},
]}>
2019-12-05 17:40:09 +05:00
<Dialog
visible={visible}
title="Delete note"
icon="trash"
paragraph="Do you want to delete this note?"
positiveText="Delete"
2019-12-09 13:17:40 +05:00
positivePress={deleteItem}
2019-12-05 17:40:09 +05:00
close={() => {
setVisible(false);
}}
/>
2020-01-05 18:03:40 +05:00
<VaultDialog
close={() => {
setVaultDialog(false);
setUnlock(false);
setIsPerm(false);
updateDB();
2020-01-05 18:03:40 +05:00
}}
note={item}
perm={isPerm}
openedToUnlock={unlock}
visible={vaultDialog}
/>
2020-01-09 19:48:39 +05:00
2019-12-21 09:46:08 +05:00
{props.pinned ? (
<View
style={{
...getElevation(3),
width: 30,
height: 30,
backgroundColor: colors.accent,
borderRadius: 100,
position: 'absolute',
left: 20,
top: -15,
justifyContent: 'center',
alignItems: 'center',
}}>
<View
style={{
width: 5,
height: 5,
backgroundColor: 'white',
borderRadius: 100,
}}
/>
</View>
) : null}
2019-12-07 08:41:48 +05:00
2019-11-28 21:40:37 +05:00
<TouchableOpacity
activeOpacity={1}
2020-01-11 23:06:29 +05:00
onLongPress={() => props.onLongPress()}
2019-11-28 21:40:37 +05:00
onPress={() => {
2020-01-05 18:03:40 +05:00
if (item.locked) {
setUnlock(true);
setVaultDialog(true);
} else {
NavigationService.navigate('Editor', {
note: item,
});
}
2019-11-28 21:40:37 +05:00
}}
2019-11-17 16:02:19 +05:00
style={{
2019-12-14 16:00:16 +05:00
paddingLeft: 0,
width: '95%',
2019-11-17 16:02:19 +05:00
}}>
2019-11-28 21:40:37 +05:00
<>
<Text
numberOfLines={1}
style={{
color: colors.pri,
fontSize: SIZE.md,
fontFamily: WEIGHT.bold,
2019-12-14 16:00:16 +05:00
maxWidth: '95%',
2019-11-28 21:40:37 +05:00
marginBottom: 5,
}}>
{item.title.replace('\n', '')}
</Text>
<View
style={{
justifyContent: 'flex-start',
alignItems: 'flex-start',
width: '100%',
}}>
<Text
style={{
2019-12-14 16:00:16 +05:00
fontSize: SIZE.xs + 1,
color: colors.pri,
2019-11-28 21:40:37 +05:00
fontFamily: WEIGHT.regular,
width: '100%',
maxWidth: '100%',
paddingRight: ph,
}}>
2020-01-06 19:03:21 +05:00
{item.headline[item.headline.length - 1] === '\n'
? item.headline.slice(0, item.headline.length - 1)
: item.headline}
2019-11-28 21:40:37 +05:00
</Text>
2019-11-17 17:48:56 +05:00
2019-12-06 12:06:03 +05:00
<View
2019-11-28 21:40:37 +05:00
style={{
2019-12-06 12:06:03 +05:00
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
2020-01-06 19:03:21 +05:00
width: '100%',
marginTop: 10,
2019-11-28 21:40:37 +05:00
}}>
2019-12-21 09:46:08 +05:00
{!props.isTrash ? (
<>
2020-01-10 18:46:52 +05:00
{item.colors.length > 0 ? (
<View
style={{
marginRight: 10,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
}}>
{item.colors.map(item => (
<View
2020-01-11 23:06:29 +05:00
key={item}
2020-01-10 18:46:52 +05:00
style={{
width: SIZE.xs,
height: SIZE.xs,
borderRadius: 100,
backgroundColor: item,
marginRight: -4.5,
}}></View>
))}
</View>
) : null}
2020-01-06 19:03:21 +05:00
2020-01-06 16:15:47 +05:00
{item.locked ? (
<Icon
2020-01-10 18:46:52 +05:00
style={{marginRight: 10}}
2020-01-06 16:15:47 +05:00
name="lock"
size={SIZE.xs}
color={colors.icon}
/>
) : null}
2019-12-06 12:06:03 +05:00
2020-01-06 16:15:47 +05:00
{item.favorite ? (
<Icon
2020-01-10 18:46:52 +05:00
style={{marginRight: 10}}
2020-01-06 16:15:47 +05:00
name="star"
size={SIZE.xs + 1}
color="orange"
2020-01-06 16:15:47 +05:00
/>
) : null}
2019-12-21 09:46:08 +05:00
<Text
style={{
color: colors.icon,
fontSize: SIZE.xs - 1,
textAlignVertical: 'center',
fontFamily: WEIGHT.regular,
2020-01-10 18:46:52 +05:00
marginRight: 10,
2019-12-21 09:46:08 +05:00
}}>
2020-01-06 16:15:47 +05:00
{timeSince(item.dateCreated)}
2019-12-21 09:46:08 +05:00
</Text>
</>
) : null}
{props.isTrash ? (
<>
<Text
style={{
color: colors.icon,
fontSize: SIZE.xs - 1,
textAlignVertical: 'center',
fontFamily: WEIGHT.regular,
}}>
{'Deleted on: ' +
new Date(item.dateDeleted).toISOString().slice(0, 10) +
' '}
</Text>
<Text
style={{
color: colors.accent,
fontSize: SIZE.xs - 1,
textAlignVertical: 'center',
fontFamily: WEIGHT.regular,
}}>
{item.type[0].toUpperCase() + item.type.slice(1) + ' '}
</Text>
</>
) : null}
2019-12-06 12:06:03 +05:00
</View>
2019-11-28 21:40:37 +05:00
</View>
</>
</TouchableOpacity>
2019-12-04 19:38:19 +05:00
<View
2019-11-17 17:48:56 +05:00
style={{
2019-12-14 16:00:16 +05:00
width: DDS.isTab ? w * 0.7 * 0.05 : w * 0.05,
2019-11-28 21:40:37 +05:00
justifyContent: 'center',
minHeight: 70,
alignItems: 'center',
2019-12-06 18:13:15 +05:00
paddingRight: ph,
2019-11-17 17:48:56 +05:00
}}>
2020-01-09 19:48:39 +05:00
<TouchableOpacity
style={{
width: w * 0.05,
justifyContent: 'center',
minHeight: 70,
alignItems: 'center',
}}
onPress={() => {
2020-01-11 23:06:29 +05:00
actionSheet._setModalVisible();
2020-01-09 19:48:39 +05:00
}}>
<Icon name="more-horizontal" size={SIZE.lg} color={colors.icon} />
</TouchableOpacity>
<ActionSheet
2020-01-11 23:06:29 +05:00
ref={ref => (actionSheet = ref)}
2020-01-09 19:48:39 +05:00
customStyles={{
backgroundColor: colors.bg,
}}
2020-01-10 18:46:52 +05:00
indicatorColor={colors.shade}
2020-01-09 19:48:39 +05:00
onClose={() => {
onMenuHide();
if (willRefresh) {
updateDB();
2020-01-09 19:48:39 +05:00
}
2020-01-11 23:06:29 +05:00
}}>
<ActionSheetComponent
item={props.item}
setWillRefresh={value => {
willRefresh = true;
}}
hasColors={true}
hasTags={true}
overlayColor={
colors.night ? 'rgba(225,225,225,0.1)' : 'rgba(0,0,0,0.3)'
}
rowItems={['Add to', 'Share', 'Export', 'Delete']}
columnItems={['Add to Vault', 'Pin', 'Favorite']}
close={value => {
if (value) {
show = value;
2020-01-10 18:46:52 +05:00
}
actionSheet._setModalVisible();
2020-01-11 23:06:29 +05:00
}}
/>
</ActionSheet>
2019-12-04 19:38:19 +05:00
</View>
2019-11-17 16:02:19 +05:00
</View>
);
};
export default NoteItem;