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

765 lines
19 KiB
JavaScript
Raw Normal View History

2020-01-18 01:04:33 +05:00
import React, {useEffect, useState} from 'react';
2020-01-09 20:14:51 +05:00
import {
Dimensions,
2020-01-18 18:13:34 +05:00
StatusBar,
2020-01-18 01:04:33 +05:00
Text,
2020-01-09 20:14:51 +05:00
TextInput,
2020-01-18 01:04:33 +05:00
TouchableOpacity,
View,
2020-01-09 20:14:51 +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-01-22 02:49:29 +05:00
import {db, DDS} from '../../../App';
2020-01-10 18:44:41 +05:00
import {
2020-01-18 01:04:33 +05:00
ACCENT,
COLOR_SCHEME,
2020-01-10 18:44:41 +05:00
COLOR_SCHEME_DARK,
COLOR_SCHEME_LIGHT,
2020-01-18 01:04:33 +05:00
opacity,
pv,
2020-01-18 18:13:34 +05:00
setColorScheme,
2020-01-18 01:04:33 +05:00
SIZE,
WEIGHT,
2020-02-03 12:21:38 +05:00
ph,
2020-01-10 18:44:41 +05:00
} from '../../common/common';
2020-01-24 23:13:09 +05:00
import {useTracked} from '../../provider';
import {ACTIONS} from '../../provider/actions';
2020-01-25 23:24:01 +05:00
import {moveNoteEvent} from '../DialogManager';
2020-01-29 17:38:50 +05:00
import Share from 'react-native-share';
2020-02-03 12:18:45 +05:00
import {timeConverter} from '../../utils/utils';
2020-01-09 20:14:51 +05:00
const w = Dimensions.get('window').width;
const h = Dimensions.get('window').height;
let tagsInputRef;
export const ActionSheetComponent = ({
close = () => {},
2020-01-11 23:05:39 +05:00
item,
2020-01-10 18:44:41 +05:00
hasColors = false,
hasTags = false,
rowItems = [],
columnItems = [],
2020-01-09 20:14:51 +05:00
}) => {
2020-01-17 21:26:01 +05:00
const [state, dispatch] = useTracked();
2020-02-06 14:17:28 +05:00
const {colors, tags} = state;
2020-01-09 20:14:51 +05:00
const [focused, setFocused] = useState(false);
2020-01-15 20:20:53 +05:00
const [note, setNote] = useState(
item
? item
: {
colors: [],
tags: [],
pinned: false,
favorite: false,
locked: false,
content: {
2020-01-29 18:20:54 +05:00
text: null,
delta: null,
2020-01-15 20:20:53 +05:00
},
dateCreated: null,
},
);
2020-01-09 20:14:51 +05:00
2020-01-22 02:49:29 +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});
}
2020-01-11 23:05:39 +05:00
useEffect(() => {
2020-01-14 17:33:48 +05:00
if (item.dateCreated !== null) {
setNote({...item});
}
2020-01-11 23:05:39 +05:00
}, [item]);
2020-01-09 20:14:51 +05:00
let tagToAdd = null;
let backPressCount = 0;
2020-02-02 14:24:24 +05:00
const _onSubmit = async () => {
if (!tagToAdd || tagToAdd === '') return;
2020-01-09 20:14:51 +05:00
let tag = tagToAdd;
2020-02-02 14:24:24 +05:00
2020-01-09 20:14:51 +05:00
if (tag.includes(' ')) {
tag = tag.replace(' ', '_');
}
tagsInputRef.setNativeProps({
2020-02-02 14:24:24 +05:00
text: '',
2020-01-09 20:14:51 +05:00
});
2020-02-02 14:24:24 +05:00
2020-02-06 13:08:35 +05:00
await db.notes.note(note.id).tag(tag);
setNote({...db.notes.note(note.id).data});
2020-01-09 20:14:51 +05:00
tagToAdd = '';
};
2020-02-02 14:24:24 +05:00
const _onKeyPress = async event => {
2020-01-09 20:14:51 +05:00
if (event.nativeEvent.key === 'Backspace') {
if (backPressCount === 0 && !tagToAdd) {
backPressCount = 1;
return;
}
if (backPressCount === 1 && !tagToAdd) {
backPressCount = 0;
let tagInputValue = note.tags[note.tags.length - 1];
let oldProps = {...note};
2020-02-02 14:24:24 +05:00
if (oldProps.tags.length === 0) return;
2020-02-06 14:17:28 +05:00
2020-02-06 13:08:35 +05:00
await db.notes
.note(note.id)
.untag(oldProps.tags[oldProps.tags.length - 1]);
2020-01-09 20:14:51 +05:00
2020-02-06 13:08:35 +05:00
setNote({...db.notes.note(note.id).data});
2020-01-09 20:14:51 +05:00
tagsInputRef.setNativeProps({
text: tagInputValue,
});
}
}
};
2020-01-20 10:33:36 +05:00
const localRefresh = (type, nodispatch = false) => {
2020-02-06 13:08:35 +05:00
if (!note || !note.id) return;
2020-01-10 18:44:41 +05:00
let toAdd;
2020-02-02 23:50:55 +05:00
2020-01-10 18:44:41 +05:00
switch (type) {
case 'note': {
2020-02-06 13:08:35 +05:00
toAdd = db.notes.note(note.id).data;
2020-01-10 18:44:41 +05:00
break;
}
case 'notebook': {
2020-02-06 13:08:35 +05:00
toAdd = db.notebooks.notebook(note.id).data;
2020-01-10 18:44:41 +05:00
break;
}
case 'topic': {
2020-02-06 13:08:35 +05:00
toAdd = db.notebooks.notebook(note.notebookId).topics.topic(note.title);
2020-02-02 23:50:55 +05:00
2020-01-10 18:44:41 +05:00
break;
}
}
2020-02-06 13:08:35 +05:00
if (!toAdd || !toAdd.id) return;
2020-01-20 15:32:32 +05:00
2020-01-20 10:33:36 +05:00
if (!nodispatch) {
dispatch({type: type});
2020-02-01 12:56:30 +05:00
dispatch({type: ACTIONS.PINNED});
dispatch({type: ACTIONS.FAVORITES});
2020-01-20 10:33:36 +05:00
}
2020-01-10 18:44:41 +05:00
setNote({...toAdd});
};
const rowItemsData = [
{
name: 'Add to',
icon: 'book',
func: () => {
2020-01-18 18:13:34 +05:00
dispatch({type: ACTIONS.MODAL_NAVIGATOR, enabled: true});
dispatch({type: ACTIONS.SELECTED_ITEMS, item: note});
moveNoteEvent();
2020-01-10 18:44:41 +05:00
close();
},
},
{
name: 'Share',
icon: 'share-2',
func: () => {
2020-01-29 17:38:50 +05:00
if (note.locked) {
close('unlock_share');
} else {
close();
let m = `${note.title}\n \n ${note.content.text}`;
Share.open({
title: 'Share note to',
failOnCancel: false,
message: m,
});
}
2020-01-10 18:44:41 +05:00
},
},
{
name: 'Export',
icon: 'external-link',
func: () => {
close();
},
},
{
name: 'Delete',
icon: 'trash',
func: () => close('delete'),
},
{
name: 'Edit Notebook',
icon: 'trash',
func: () => {
2020-01-18 18:13:34 +05:00
close('notebook');
2020-01-10 18:44:41 +05:00
},
},
{
name: 'Edit Topic',
icon: 'trash',
func: () => {
close('topic');
},
},
2020-02-03 12:18:45 +05:00
{
name: 'Open',
icon: 'arrow-up-right',
func: () => {},
},
2020-01-10 18:44:41 +05:00
{
name: 'Restore',
icon: 'trash',
func: () => {
2020-02-06 14:17:28 +05:00
// TODO
//db.restoreItem(item.dateCreated);
2020-01-10 18:44:41 +05:00
ToastEvent.show(
2020-01-19 21:31:26 +05:00
item.type === 'note' ? 'Note restored' : 'Notebook restored',
2020-01-10 18:44:41 +05:00
'success',
1000,
() => {},
'',
);
close();
},
},
{
name: 'Remove',
icon: 'trash',
func: () => {
close();
},
},
];
2020-01-29 17:38:50 +05:00
const columnItemsData = [
2020-01-10 18:44:41 +05:00
{
name: 'Dark Mode',
icon: 'moon',
func: () => {
if (!colors.night) {
2020-01-14 20:48:03 +05:00
FastStorage.setItem('theme', JSON.stringify({night: true}));
2020-01-10 18:44:41 +05:00
changeColorScheme(COLOR_SCHEME_DARK);
} else {
2020-01-14 20:48:03 +05:00
FastStorage.setItem('theme', JSON.stringify({night: false}));
2020-01-10 18:44:41 +05:00
changeColorScheme(COLOR_SCHEME_LIGHT);
}
},
switch: true,
on: colors.night ? true : false,
close: false,
},
{
name: 'Pin',
icon: 'tag',
2020-02-01 12:56:30 +05:00
func: async () => {
2020-02-06 13:08:35 +05:00
if (!note.id) return;
2020-02-02 16:18:52 +05:00
if (note.type === 'note') {
2020-02-06 13:08:35 +05:00
await db.notes.note(note.id).pin();
2020-02-02 16:18:52 +05:00
} else {
2020-02-06 13:08:35 +05:00
await db.notebooks.notebook(note.id).pin();
2020-02-02 16:18:52 +05:00
}
2020-01-18 00:45:37 +05:00
dispatch({type: ACTIONS.PINNED});
2020-02-02 16:18:52 +05:00
localRefresh(item.type);
2020-01-10 18:44:41 +05:00
},
close: false,
check: true,
on: note.pinned,
},
{
name: 'Favorite',
icon: 'star',
2020-02-01 12:56:30 +05:00
func: async () => {
2020-02-06 13:08:35 +05:00
if (!note.id) return;
2020-02-02 16:18:52 +05:00
if (note.type === 'note') {
2020-02-06 13:08:35 +05:00
await db.notes.note(note.id).favorite();
2020-02-02 16:18:52 +05:00
} else {
2020-02-06 13:08:35 +05:00
await db.notebooks.notebook(note.id).favorite();
2020-02-02 16:18:52 +05:00
}
2020-02-01 12:56:30 +05:00
dispatch({type: ACTIONS.FAVORITES});
2020-02-02 16:18:52 +05:00
localRefresh(item.type);
2020-01-10 18:44:41 +05:00
},
close: false,
check: true,
on: note.favorite,
},
{
name: 'Add to Vault',
icon: 'lock',
func: () => {
2020-02-06 13:08:35 +05:00
if (!note.id) return;
2020-01-10 18:44:41 +05:00
note.locked ? close('unlock') : close('lock');
},
close: true,
check: true,
on: note.locked,
},
];
2020-01-16 19:53:16 +05:00
const _renderTag = tag => (
<TouchableOpacity
key={tag}
2020-02-02 14:24:24 +05:00
onPress={async () => {
2020-01-16 19:53:16 +05:00
let oldProps = {...note};
2020-02-06 13:08:35 +05:00
await db.notes.note(note.id).untag(oldProps.tags.indexOf(tag));
2020-01-16 19:53:16 +05:00
localRefresh(item.type);
}}
style={{
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
margin: 1,
paddingHorizontal: 5,
paddingVertical: 2.5,
}}>
<Text
style={{
fontFamily: WEIGHT.regular,
fontSize: SIZE.sm,
color: colors.pri,
}}>
<Text
style={{
color: colors.accent,
}}>
2020-02-03 12:31:18 +05:00
#
2020-01-16 19:53:16 +05:00
</Text>
2020-02-03 12:31:18 +05:00
{tag}
2020-01-16 19:53:16 +05:00
</Text>
</TouchableOpacity>
);
const _renderColor = color => (
<TouchableOpacity
key={color}
onPress={() => {
let noteColors = note.colors;
if (noteColors.includes(color)) {
noteColors.splice(color, 1);
} else {
noteColors.push(color);
}
2020-02-06 21:33:18 +05:00
//db.notes.note(note.id).
2020-02-06 13:08:35 +05:00
// TODO
/* db.addNote({
2020-01-16 19:53:16 +05:00
dateCreated: note.dateCreated,
colors: noteColors,
content: note.content,
title: note.title,
2020-02-06 13:08:35 +05:00
}); */
2020-01-16 19:53:16 +05:00
localRefresh(item.type);
}}
style={{
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
borderColor: colors.nav,
}}>
<View
style={{
2020-01-24 18:48:33 +05:00
width: DDS.isTab ? 500 / 10 : w / 10,
height: DDS.isTab ? 500 / 10 : w / 10,
2020-01-16 19:53:16 +05:00
backgroundColor: color,
borderRadius: 100,
justifyContent: 'center',
alignItems: 'center',
}}>
{note && note.colors && note.colors.includes(color) ? (
<Icon name="check" color="white" size={SIZE.lg} />
) : null}
</View>
</TouchableOpacity>
);
const _renderRowItem = rowItem =>
rowItems.includes(rowItem.name) ? (
<TouchableOpacity
onPress={rowItem.func}
key={rowItem.name}
style={{
alignItems: 'center',
2020-02-03 12:18:45 +05:00
width: (w - 24) / rowItems.length,
2020-01-16 19:53:16 +05:00
}}>
<Icon
style={{
width: 50,
height: 40,
borderRadius: 100,
justifyContent: 'center',
alignItems: 'center',
textAlign: 'center',
textAlignVertical: 'center',
2020-01-24 18:48:33 +05:00
marginBottom: DDS.isTab ? 7 : 3.5,
2020-01-16 19:53:16 +05:00
}}
name={rowItem.icon}
2020-01-24 18:48:33 +05:00
size={DDS.isTab ? SIZE.xl : SIZE.lg}
2020-01-16 19:53:16 +05:00
color={colors.accent}
/>
<Text
style={{
fontFamily: WEIGHT.regular,
2020-02-03 12:18:45 +05:00
fontSize: DDS.isTab ? SIZE.sm : SIZE.xs + 1,
2020-01-16 19:53:16 +05:00
color: colors.pri,
}}>
{rowItem.name}
</Text>
</TouchableOpacity>
) : null;
const _renderColumnItem = item =>
2020-02-06 13:08:35 +05:00
(note.id && columnItems.includes(item.name)) ||
2020-02-02 19:09:33 +05:00
(item.name === 'Dark Mode' && columnItems.includes(item.name)) ? (
2020-01-16 19:53:16 +05:00
<TouchableOpacity
key={item.name}
activeOpacity={opacity}
onPress={() => {
item.func();
}}
style={{
width: '100%',
alignSelf: 'center',
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'flex-end',
paddingHorizontal: 12,
2020-01-24 18:48:33 +05:00
paddingVertical: pv,
2020-01-16 19:53:16 +05:00
}}>
<View
style={{
flexDirection: 'row',
alignItems: 'center',
}}>
<Icon
style={{
width: 30,
}}
name={item.icon}
color={colors.pri}
size={SIZE.md}
/>
<Text
style={{
fontFamily: WEIGHT.regular,
fontSize: SIZE.sm,
color: colors.pri,
}}>
{item.name}
</Text>
</View>
{item.switch ? (
<Icon
size={SIZE.lg + 2}
color={item.on ? colors.accent : colors.icon}
name={item.on ? 'toggle-right' : 'toggle-left'}
/>
) : (
undefined
)}
{item.check ? (
<TouchableOpacity
style={{
borderWidth: 2,
borderColor: item.on ? colors.accent : colors.icon,
width: 23,
height: 23,
justifyContent: 'center',
alignItems: 'center',
borderRadius: 100,
paddingTop: 3,
}}>
{item.on ? (
<Icon size={SIZE.sm - 2} color={colors.accent} name="check" />
) : null}
</TouchableOpacity>
) : null}
</TouchableOpacity>
) : null;
2020-01-09 20:14:51 +05:00
return (
2020-01-10 18:44:41 +05:00
<View
2020-01-20 10:33:36 +05:00
onLayout={() => {
2020-02-02 16:18:52 +05:00
if (!item.dateDeleted) {
localRefresh(item.type, true);
}
2020-01-20 10:33:36 +05:00
}}
2020-01-10 18:44:41 +05:00
style={{
paddingBottom: 15,
backgroundColor: colors.bg,
2020-01-22 02:49:29 +05:00
width: '100%',
2020-01-24 18:48:33 +05:00
paddingHorizontal: 0,
2020-01-10 18:44:41 +05:00
}}>
2020-02-06 13:08:35 +05:00
{!note.id ? (
2020-01-29 18:20:54 +05:00
<Text
style={{
width: '100%',
textAlign: 'center',
marginVertical: 10,
color: colors.icon,
fontFamily: WEIGHT.regular,
}}>
Please start writing to save your note.
</Text>
2020-02-03 12:18:45 +05:00
) : (
<View
style={{
paddingHorizontal: 12,
alignItems: 'center',
marginVertical: 10,
}}>
<Text
numberOfLines={1}
style={{
color: colors.pri,
fontSize: SIZE.sm + 1,
fontFamily: WEIGHT.bold,
maxWidth: '100%',
}}>
{note.title.replace('\n', '')}
</Text>
<Text
numberOfLines={2}
style={{
fontSize: SIZE.sm - 1,
color: colors.pri + 'B3',
fontFamily: WEIGHT.regular,
width: '100%',
textAlign: 'center',
maxWidth: '100%',
}}>
{note.type === 'notebook' && note.description
? note.description
: null}
{note.type === 'note'
? note.headline[item.headline.length - 1] === '\n'
? note.headline.slice(0, note.headline.length - 1)
: note.headline
: null}
</Text>
<Text
style={{
color: colors.icon,
fontSize: SIZE.xs - 1,
textAlignVertical: 'center',
fontFamily: WEIGHT.regular,
marginTop: 2.5,
}}>
{note.type === 'note'
2020-02-06 13:08:35 +05:00
? 'Last edited on ' + timeConverter(note.dateEdited)
2020-02-03 12:18:45 +05:00
: null}
{note.type !== 'note' && !note.dateDeleted
? 'Created on ' + timeConverter(note.dateCreated)
: null}
{note.dateDeleted
? 'Deleted on ' + timeConverter(note.dateDeleted)
: null}
</Text>
2020-02-03 12:21:38 +05:00
{note.type !== 'notebook' ? null : (
<View
style={{
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
width: '100%',
maxWidth: '100%',
flexWrap: 'wrap',
}}>
{note && note.topics
? note.topics.slice(1, 4).map(topic => (
<View
key={topic.dateCreated.toString() + topic.title}
style={{
borderRadius: 5,
backgroundColor: colors.accent,
paddingHorizontal: ph / 1.5,
paddingVertical: pv / 4,
marginRight: 5,
marginVertical: 2.5,
}}>
<Text
numberOfLines={1}
style={{
color: 'white',
fontFamily: WEIGHT.regular,
fontSize: SIZE.xxs,
maxWidth: '100%',
}}>
{topic.title}
</Text>
</View>
))
: null}
</View>
)}
{note.type !== 'note' ? null : (
<Text
style={{
color: colors.accent,
fontSize: SIZE.xs - 1,
textAlignVertical: 'center',
fontFamily: WEIGHT.regular,
marginTop: 2,
borderWidth: 1,
textAlign: 'center',
borderColor: colors.accent,
paddingHorizontal: 5,
borderRadius: 2,
}}>
Synced
</Text>
)}
2020-02-03 12:18:45 +05:00
</View>
)}
2020-01-29 18:20:54 +05:00
2020-02-06 13:08:35 +05:00
{note.id ? (
2020-01-29 18:20:54 +05:00
<View
style={{
width: '100%',
justifyContent: 'space-between',
alignItems: 'center',
paddingVertical: 10,
flexDirection: 'row',
2020-02-03 12:18:45 +05:00
paddingHorizontal: 12,
2020-01-29 18:20:54 +05:00
}}>
{rowItemsData.map(_renderRowItem)}
</View>
) : null}
2020-01-09 20:14:51 +05:00
2020-02-06 13:08:35 +05:00
{hasColors && note.id ? (
2020-01-10 18:44:41 +05:00
<View
style={{
flexDirection: 'row',
flexWrap: 'wrap',
paddingHorizontal: 12,
width: '100%',
marginVertical: 10,
alignItems: 'center',
justifyContent: 'space-between',
}}>
{['red', 'yellow', 'green', 'blue', 'purple', 'orange', 'gray'].map(
2020-01-16 19:53:16 +05:00
_renderColor,
2020-01-10 18:44:41 +05:00
)}
</View>
) : null}
2020-01-09 20:14:51 +05:00
2020-02-06 13:08:35 +05:00
{hasTags && note.id ? (
2020-01-10 18:44:41 +05:00
<View
style={{
marginHorizontal: 12,
}}>
2020-02-03 12:18:45 +05:00
<View
2020-01-10 18:44:41 +05:00
style={{
2020-02-03 12:18:45 +05:00
flexDirection: 'row',
flexWrap: 'wrap',
borderRadius: 5,
borderWidth: 1.5,
borderColor: focused ? colors.accent : colors.nav,
paddingVertical: 5,
}}>
{note && note.tags ? note.tags.map(_renderTag) : null}
<TextInput
style={{
backgroundColor: 'transparent',
minWidth: 100,
fontFamily: WEIGHT.regular,
color: colors.pri,
paddingHorizontal: 5,
paddingVertical: 1.5,
margin: 1,
}}
blurOnSubmit={false}
ref={ref => (tagsInputRef = ref)}
placeholderTextColor={colors.icon}
onFocus={() => {
setFocused(true);
}}
selectionColor={colors.accent}
onBlur={() => {
setFocused(false);
}}
placeholder="#hashtag"
onChangeText={value => {
tagToAdd = value;
if (tagToAdd.length > 0) backPressCount = 0;
}}
onSubmitEditing={_onSubmit}
onKeyPress={_onKeyPress}
/>
</View>
<View
style={{
flexDirection: 'row',
flexWrap: 'wrap',
marginBottom: 5,
marginTop: 5,
alignItems: 'center',
}}>
<Text
style={{
fontFamily: WEIGHT.regular,
fontSize: SIZE.xs,
color: colors.accent,
}}>
Suggestions:{' '}
</Text>
2020-02-06 14:17:28 +05:00
{tags
.filter(o => o.count > 1)
.map(tag => (
<TouchableOpacity
key={tag}
onPress={async () => {}}
2020-02-03 12:18:45 +05:00
style={{
2020-02-06 14:17:28 +05:00
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
margin: 1,
marginRight: 3,
paddingHorizontal: 5,
paddingVertical: 1,
backgroundColor: colors.shade,
borderRadius: 2.5,
2020-02-03 12:18:45 +05:00
}}>
<Text
style={{
2020-02-06 14:17:28 +05:00
fontFamily: WEIGHT.regular,
fontSize: SIZE.xs,
color: colors.pri,
2020-02-03 12:18:45 +05:00
}}>
2020-02-06 14:17:28 +05:00
<Text
style={{
color: colors.accent,
}}>
#
</Text>
{tag.title}
2020-02-03 12:18:45 +05:00
</Text>
2020-02-06 14:17:28 +05:00
</TouchableOpacity>
))}
2020-02-03 12:18:45 +05:00
</View>
2020-01-10 18:44:41 +05:00
</View>
) : null}
{columnItems.length > 0 ? (
2020-01-16 19:53:16 +05:00
<View>{columnItemsData.map(_renderColumnItem)}</View>
2020-01-10 18:44:41 +05:00
) : null}
2020-01-24 18:48:33 +05:00
{DDS.isTab ? (
<View
style={{
height: 20,
}}
/>
) : null}
2020-01-09 20:14:51 +05:00
</View>
);
};