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

261 lines
7.5 KiB
JavaScript
Raw Normal View History

2021-12-31 09:05:44 +05:00
import React, {useEffect} from 'react';
import {BackHandler, View} from 'react-native';
import {useSafeAreaInsets} from 'react-native-safe-area-context';
import {useTracked} from '../../provider';
import {useSelectionStore} from '../../provider/stores';
import {eSendEvent, ToastEvent} from '../../services/EventManager';
import Navigation from '../../services/Navigation';
2021-12-31 09:05:44 +05:00
import SearchService from '../../services/SearchService';
import {db} from '../../utils/database';
import {eOpenMoveNoteDialog, refreshNotesPage} from '../../utils/Events';
import {deleteItems} from '../../utils/functions';
import layoutmanager from '../../utils/layout-manager';
import {SIZE} from '../../utils/SizeUtils';
import {sleep} from '../../utils/TimeUtils';
import {ActionIcon} from '../ActionIcon';
import {presentDialog} from '../Dialog/functions';
2020-11-04 20:49:21 +05:00
import Heading from '../Typography/Heading';
export const SelectionHeader = React.memo(({screen, type, extras}) => {
2020-01-17 21:26:01 +05:00
const [state, dispatch] = useTracked();
2021-06-05 21:10:20 +05:00
const {colors} = state;
2021-06-05 21:10:20 +05:00
const selectionMode = useSelectionStore(state => state.selectionMode);
const selectedItemsList = useSelectionStore(state => state.selectedItemsList);
const setSelectionMode = useSelectionStore(state => state.setSelectionMode);
const clearSelection = useSelectionStore(state => state.clearSelection);
2021-06-05 21:10:20 +05:00
2020-09-07 19:19:20 +05:00
const insets = useSafeAreaInsets();
2021-02-16 16:11:10 +05:00
const addToFavorite = async () => {
if (selectedItemsList.length > 0) {
2021-04-11 14:04:14 +05:00
selectedItemsList.forEach(item => {
2021-02-16 16:11:10 +05:00
db.notes.note(item.id).favorite();
});
Navigation.setRoutesToUpdate([
Navigation.routeNames.Notes,
Navigation.routeNames.NotesPage,
2021-12-31 09:05:44 +05:00
Navigation.routeNames.Favorites
2021-02-16 16:11:10 +05:00
]);
2021-06-05 21:10:20 +05:00
clearSelection();
2021-02-16 16:11:10 +05:00
}
};
const restoreItem = async () => {
if (selectedItemsList.length > 0) {
let noteIds = [];
2021-04-11 14:04:14 +05:00
selectedItemsList.forEach(item => {
2021-02-16 16:11:10 +05:00
noteIds.push(item.id);
});
await db.trash.restore(...noteIds);
Navigation.setRoutesToUpdate([
Navigation.routeNames.Tags,
Navigation.routeNames.Notes,
Navigation.routeNames.Notebooks,
Navigation.routeNames.NotesPage,
Navigation.routeNames.Favorites,
2021-12-31 09:05:44 +05:00
Navigation.routeNames.Trash
2021-02-16 16:11:10 +05:00
]);
2021-06-05 21:10:20 +05:00
clearSelection();
2021-02-20 15:03:02 +05:00
ToastEvent.show({
heading: 'Restore successful',
2021-12-31 09:05:44 +05:00
type: 'success'
2021-02-20 15:03:02 +05:00
});
2021-02-16 16:11:10 +05:00
}
};
const onBackPress = () => {
2021-12-31 09:05:44 +05:00
layoutmanager.withSpringAnimation(500);
2021-06-05 21:10:20 +05:00
clearSelection();
return true;
};
useEffect(() => {
if (selectionMode) {
BackHandler.addEventListener('hardwareBackPress', onBackPress);
} else {
BackHandler.removeEventListener('hardwareBackPress', onBackPress);
}
}, [selectionMode]);
return !selectionMode || Navigation.getCurrentScreen() !== screen ? null : (
2021-04-11 14:04:14 +05:00
<View
style={{
width: '100%',
2020-11-04 20:49:21 +05:00
height: 50 + insets.top,
2021-12-31 17:20:29 +05:00
paddingTop: Platform.OS === 'android' ? insets.top : null,
2021-12-31 09:05:44 +05:00
backgroundColor: colors.bg,
justifyContent: 'space-between',
2020-11-04 20:49:21 +05:00
alignItems: 'center',
flexDirection: 'row',
2020-01-31 18:40:56 +05:00
zIndex: 999,
2021-12-31 09:05:44 +05:00
paddingHorizontal: 12
}}>
<View
style={{
flexDirection: 'row',
2020-11-04 20:49:21 +05:00
justifyContent: 'flex-start',
alignItems: 'center',
2021-12-31 09:05:44 +05:00
borderRadius: 100
}}>
2020-11-04 20:49:21 +05:00
<ActionIcon
customStyle={{
justifyContent: 'center',
alignItems: 'center',
2020-11-04 20:49:21 +05:00
height: 40,
width: 40,
borderRadius: 100,
2021-12-31 09:05:44 +05:00
marginRight: 10
2020-11-04 20:49:21 +05:00
}}
2021-12-31 09:05:44 +05:00
type="grayBg"
2020-11-04 20:49:21 +05:00
onPress={() => {
setSelectionMode(!selectionMode);
2020-11-04 20:49:21 +05:00
}}
2021-12-31 09:05:44 +05:00
size={SIZE.xl}
color={colors.icon}
2021-02-16 16:11:10 +05:00
name="close"
2020-11-04 20:49:21 +05:00
/>
2020-03-02 11:13:40 +05:00
2021-12-31 09:05:44 +05:00
<View
style={{
backgroundColor: colors.nav,
height: 40,
borderRadius: 100,
paddingHorizontal: 16,
justifyContent: 'center',
flexDirection: 'row',
alignItems: 'center'
}}>
2021-12-31 12:35:39 +05:00
<Heading size={SIZE.md} color={colors.accent}>
{selectedItemsList.length + ' Selected'}
</Heading>
2021-12-31 09:05:44 +05:00
</View>
2020-11-04 20:49:21 +05:00
</View>
<View
style={{
flexDirection: 'row',
justifyContent: 'flex-start',
2021-12-31 09:05:44 +05:00
alignItems: 'center'
2020-11-04 20:49:21 +05:00
}}>
2021-12-31 09:05:44 +05:00
{/* <ActionIcon
onPress={async () => {
// await sleep(100);
// eSendEvent(eOpenMoveNoteDialog);
useSelectionStore.getState().setAll([...SearchService.getSearchInformation().data]);
}}
customStyle={{
marginLeft: 10
}}
color={colors.pri}
name="select-all"
size={SIZE.xl}
/> */}
2021-04-11 14:04:14 +05:00
{screen === 'Trash' ||
screen === 'Notebooks' ||
screen === 'Notebook' ||
type === 'topic' ? null : (
2020-11-04 20:49:21 +05:00
<ActionIcon
onPress={async () => {
2021-06-05 21:10:20 +05:00
//setSelectionMode(false);
await sleep(100);
2020-11-04 20:49:21 +05:00
eSendEvent(eOpenMoveNoteDialog);
}}
customStyle={{
2021-12-31 09:05:44 +05:00
marginLeft: 10
2020-11-04 20:49:21 +05:00
}}
2021-12-31 09:05:44 +05:00
color={colors.pri}
2020-11-04 20:49:21 +05:00
name="plus"
size={SIZE.xl}
/>
)}
2021-12-03 09:34:44 +05:00
{type === 'topic' ? (
<ActionIcon
onPress={async () => {
if (selectedItemsList.length > 0) {
await db.notebooks
.notebook(extras.notebook)
.topics.topic(extras.topic)
.delete(...selectedItemsList.map(item => item.id));
eSendEvent(refreshNotesPage);
Navigation.setRoutesToUpdate([
Navigation.routeNames.NotesPage,
Navigation.routeNames.Favorites,
Navigation.routeNames.Notes,
Navigation.routeNames.Notebook,
2021-12-31 09:05:44 +05:00
Navigation.routeNames.Notebooks
]);
2021-06-05 21:10:20 +05:00
clearSelection();
}
}}
customStyle={{
2021-12-31 09:05:44 +05:00
marginLeft: 10
}}
2021-12-31 09:05:44 +05:00
color={colors.pri}
name="minus"
size={SIZE.xl}
/>
2021-12-03 09:34:44 +05:00
) : null}
2021-04-11 14:04:14 +05:00
{screen === 'Favorites' ? (
2020-12-16 13:12:35 +05:00
<ActionIcon
2021-02-16 16:11:10 +05:00
onPress={addToFavorite}
2020-12-16 13:12:35 +05:00
customStyle={{
2021-12-31 09:05:44 +05:00
marginLeft: 10
2020-12-16 13:12:35 +05:00
}}
2021-12-31 09:05:44 +05:00
color={colors.pri}
2020-12-16 13:12:35 +05:00
name="star-off"
size={SIZE.xl}
/>
) : null}
2021-04-11 14:04:14 +05:00
{screen === 'Trash' ? null : (
2020-11-04 20:49:21 +05:00
<ActionIcon
customStyle={{
2021-12-31 09:05:44 +05:00
marginLeft: 10
2020-11-04 20:49:21 +05:00
}}
onPress={async () => {
presentDialog({
title: `Delete ${
selectedItemsList.length > 1 ? 'items' : 'item'
}`,
paragraph: `Are you sure you want to delete ${
selectedItemsList.length > 1 ? 'these items?' : 'this item?'
}`,
positiveText: 'Delete',
negativeText: 'Cancel',
positivePress: () => {
deleteItems();
},
2021-12-31 09:05:44 +05:00
positiveType: 'errorShade'
});
2020-11-04 20:49:21 +05:00
return;
}}
2021-12-31 09:05:44 +05:00
color={colors.pri}
2020-11-04 20:49:21 +05:00
name="delete"
size={SIZE.xl}
/>
)}
2020-02-20 14:45:23 +05:00
2021-04-11 14:04:14 +05:00
{screen === 'Trash' ? (
2020-11-04 20:49:21 +05:00
<ActionIcon
customStyle={{
2021-12-31 09:05:44 +05:00
marginLeft: 10
2020-11-04 20:49:21 +05:00
}}
2021-12-31 09:05:44 +05:00
color={colors.pri}
2021-02-16 16:11:10 +05:00
onPress={restoreItem}
2020-11-04 20:49:21 +05:00
name="delete-restore"
size={SIZE.xl - 3}
/>
) : null}
</View>
2021-04-11 14:04:14 +05:00
</View>
);
2021-06-02 17:34:39 +05:00
});
export default SelectionHeader;