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

324 lines
9.0 KiB
JavaScript
Raw Normal View History

2022-01-22 12:57:05 +05:00
import React, { useEffect } from 'react';
import { BackHandler, Platform, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
2022-04-24 05:59:14 +05:00
import { useThemeStore } from '../../stores/use-theme-store';
import { useSelectionStore } from '../../stores/use-selection-store';
2022-02-28 23:25:18 +05:00
import { eSendEvent, ToastEvent } from '../../services/event-manager';
import Navigation from '../../services/navigation';
2022-01-22 12:57:05 +05:00
import { db } from '../../utils/database';
2022-02-28 13:48:59 +05:00
import { eOpenMoveNoteDialog, refreshNotesPage } from '../../utils/events';
2022-01-22 12:57:05 +05:00
import { deleteItems } from '../../utils/functions';
2022-02-28 13:48:59 +05:00
import { tabBarRef } from '../../utils/global-refs';
2021-12-31 09:05:44 +05:00
import layoutmanager from '../../utils/layout-manager';
2022-02-28 13:48:59 +05:00
import { SIZE } from '../../utils/size';
import { sleep } from '../../utils/time';
import { presentDialog } from '../dialog/functions';
import { IconButton } from '../ui/icon-button';
import Heading from '../ui/typography/heading';
2022-04-24 05:59:14 +05:00
import useNavigationStore from '../../stores/use-navigation-store';
2022-04-24 05:59:14 +05:00
export const SelectionHeader = React.memo(() => {
2022-02-28 23:25:18 +05:00
const colors = useThemeStore(state => state.colors);
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);
2022-04-24 05:59:14 +05:00
const currentScreen = useNavigationStore(state => state.currentScreen);
const screen = currentScreen.name;
2020-09-07 19:19:20 +05:00
const insets = useSafeAreaInsets();
2022-02-21 09:57:38 +05:00
useEffect(() => {
2022-04-24 05:59:14 +05:00
if (selectionMode) {
tabBarRef.current?.lock();
} else {
tabBarRef.current?.unlock();
}
2022-02-21 09:57:38 +05:00
}, [selectionMode]);
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();
});
2022-04-24 05:59:14 +05:00
Navigation.queueRoutesForUpdate(
'Notes',
'Favorites',
'ColoredNotes',
'TaggedNotes',
'TopicNotes'
);
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);
2022-04-24 05:59:14 +05:00
Navigation.queueRoutesForUpdate(
'Notes',
'Favorites',
'ColoredNotes',
'TaggedNotes',
'TopicNotes',
'Trash',
'Notebooks',
'Tags'
);
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 deleteItem = async () => {
presentDialog({
title: `Delete ${selectedItemsList.length > 1 ? 'items' : 'item'}`,
paragraph: `Are you sure you want to delete ${
selectedItemsList.length > 1 ? 'these items permanently?' : 'this item permanently?'
}`,
positiveText: 'Delete',
negativeText: 'Cancel',
positivePress: async () => {
if (selectedItemsList.length > 0) {
let noteIds = [];
selectedItemsList.forEach(item => {
noteIds.push(item.id);
});
await db.trash.delete(...noteIds);
2022-04-24 05:59:14 +05:00
Navigation.queueRoutesForUpdate(
'Notes',
'Favorites',
'ColoredNotes',
'TaggedNotes',
'TopicNotes',
'Trash',
'Notebooks',
'Tags'
);
clearSelection();
}
},
positiveType: 'errorShade'
});
};
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]);
2022-04-24 05:59:14 +05:00
return !selectionMode ? 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
2022-01-22 12:57:05 +05:00
}}
>
<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
2022-01-22 12:57:05 +05:00
}}
>
2022-02-28 13:48:59 +05:00
<IconButton
2020-11-04 20:49:21 +05:00
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'
2022-01-22 12:57:05 +05:00
}}
>
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'
2022-01-22 12:57:05 +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}
/> */}
{screen === 'Trash' || screen === 'Notebooks' || screen === 'Notebook' ? null : (
2022-02-28 13:48:59 +05:00
<IconButton
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}
/>
)}
2022-04-24 05:59:14 +05:00
{screen === 'TopicNotes' ? (
2022-02-28 13:48:59 +05:00
<IconButton
onPress={async () => {
if (selectedItemsList.length > 0) {
2022-04-24 05:59:14 +05:00
//TODO
let extras = {};
await db.notebooks
.notebook(extras.notebook)
.topics.topic(extras.topic)
.delete(...selectedItemsList.map(item => item.id));
eSendEvent(refreshNotesPage);
2022-04-24 05:59:14 +05:00
Navigation.queueRoutesForUpdate(
'Notes',
'Favorites',
'ColoredNotes',
'TaggedNotes',
'TopicNotes',
'Notebooks',
'Notebook'
);
2021-06-05 21:10:20 +05:00
clearSelection();
}
}}
customStyle={{
2021-12-31 09:05:44 +05:00
marginLeft: 10
}}
2022-04-01 10:02:41 +05:00
testID="select-minus"
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' ? (
2022-02-28 13:48:59 +05:00
<IconButton
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 : (
2022-02-28 13:48:59 +05:00
<IconButton
2020-11-04 20:49:21 +05:00
customStyle={{
2021-12-31 09:05:44 +05:00
marginLeft: 10
2020-11-04 20:49:21 +05:00
}}
onPress={async () => {
presentDialog({
2022-01-22 12:57:05 +05:00
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' ? (
<>
<IconButton
customStyle={{
marginLeft: 10
}}
color={colors.pri}
onPress={restoreItem}
name="delete-restore"
size={SIZE.xl - 3}
/>
<IconButton
customStyle={{
marginLeft: 10
}}
color={colors.pri}
onPress={deleteItem}
name="delete"
size={SIZE.xl - 3}
/>
</>
2020-11-04 20:49:21 +05:00
) : null}
</View>
2021-04-11 14:04:14 +05:00
</View>
);
2021-06-02 17:34:39 +05:00
});
2022-01-22 12:57:05 +05:00
SelectionHeader.displayName = 'SelectionHeader';
export default SelectionHeader;