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

144 lines
3.5 KiB
JavaScript
Raw Normal View History

import React, {useEffect, useState} from 'react';
2021-02-09 11:15:09 +05:00
import {TouchableOpacity, View} from 'react-native';
2020-02-11 20:33:36 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2020-12-29 11:25:32 +05:00
import {useTracked} from '../../provider';
2021-02-09 11:15:09 +05:00
import {eSubscribeEvent, eUnSubscribeEvent} from '../../services/EventManager';
2020-12-29 11:25:32 +05:00
import {SIZE} from '../../utils/SizeUtils';
import {PressableButton} from '../PressableButton';
2021-02-09 11:15:09 +05:00
import {ActionStrip} from './action-strip';
import {Filler} from './back-fill';
2020-12-20 17:38:28 +05:00
2020-03-18 12:31:06 +05:00
const SelectionWrapper = ({
children,
item,
index,
background,
2020-09-09 14:55:59 +05:00
onLongPress,
2020-09-09 22:09:57 +05:00
onPress,
2020-12-16 12:07:34 +05:00
testID,
2020-03-18 12:31:06 +05:00
}) => {
2020-01-17 21:26:01 +05:00
const [state, dispatch] = useTracked();
2020-12-16 20:01:02 +05:00
const {colors, selectionMode, selectedItemsList} = state;
2020-01-31 18:22:20 +05:00
const [selected, setSelected] = useState(false);
2020-12-20 17:38:28 +05:00
const [actionStrip, setActionStrip] = useState(false);
2020-12-29 17:19:58 +05:00
2020-01-31 18:22:20 +05:00
useEffect(() => {
2020-12-20 17:38:28 +05:00
if (selectionMode) {
setActionStrip(false);
let exists = selectedItemsList.filter(
(o) => o.dateCreated === item.dateCreated,
);
2020-01-31 18:22:20 +05:00
2020-12-20 17:38:28 +05:00
if (exists[0]) {
if (!selected) {
setSelected(true);
}
} else {
if (selected) {
setSelected(false);
}
2020-01-31 18:22:20 +05:00
}
}
}, [selectedItemsList]);
2020-01-17 00:23:16 +05:00
2020-12-31 21:49:28 +05:00
const onLong = () => {
2020-12-29 17:19:58 +05:00
if (selectionMode) return;
2021-01-08 12:58:30 +05:00
if (item.type === 'topic' && item.title === 'General') return;
2020-12-29 17:19:58 +05:00
setActionStrip(!actionStrip);
};
2021-01-01 15:49:29 +05:00
const _onPress = async () => {
if (actionStrip) {
setActionStrip(false);
return;
}
await onPress();
2021-01-03 12:22:55 +05:00
};
2021-01-01 15:49:29 +05:00
2020-12-31 21:49:28 +05:00
const closeStrip = () => {
setActionStrip(false);
};
useEffect(() => {
eSubscribeEvent('navigate', closeStrip);
return () => {
eUnSubscribeEvent('navigate', closeStrip);
};
}, []);
2021-02-16 16:11:10 +05:00
return (
2020-09-09 14:55:59 +05:00
<PressableButton
2020-12-16 20:01:02 +05:00
customColor="transparent"
2020-12-01 22:52:01 +05:00
testID={testID}
2020-12-29 17:19:58 +05:00
onLongPress={onLong}
2021-01-01 15:49:29 +05:00
onPress={_onPress}
2020-12-16 20:01:02 +05:00
customSelectedColor={colors.nav}
2020-12-06 14:42:46 +05:00
customAlpha={!colors.night ? -0.02 : 0.02}
2020-12-16 20:01:02 +05:00
customOpacity={1}
2020-09-09 14:55:59 +05:00
customStyle={{
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
2020-01-22 02:51:24 +05:00
width: '100%',
2020-09-18 20:54:31 +05:00
borderRadius: 0,
2020-12-16 20:01:02 +05:00
overflow: 'hidden',
2020-12-20 17:38:28 +05:00
paddingHorizontal: 12,
2020-03-18 12:31:06 +05:00
marginTop:
2020-11-14 10:06:41 +05:00
index === 0 && !selectionMode
2020-03-18 12:31:06 +05:00
? 15
2020-11-14 10:06:41 +05:00
: index === 0 && selectionMode
2020-03-18 12:31:06 +05:00
? 30
: 0,
}}>
2020-12-20 17:38:28 +05:00
{actionStrip && (
<ActionStrip note={item} setActionStrip={setActionStrip} />
)}
{item.type === 'note' && <Filler background={background} item={item} />}
2020-03-18 12:31:06 +05:00
2020-12-29 17:19:58 +05:00
{selectionMode && (
<View
style={{
display: 'flex',
opacity: 1,
width: '10%',
height: 70,
justifyContent: 'center',
alignItems: 'center',
paddingRight: 8,
}}>
{item.type !== 'topic' ||
(item.type === 'topic' && item.title !== 'General') ? (
<TouchableOpacity
activeOpacity={1}
onPress={onLongPress}
style={{
justifyContent: 'center',
alignItems: 'center',
height: 70,
}}>
<Icon
size={SIZE.lg}
color={selected ? colors.accent : colors.icon}
name={
selected
? 'check-circle-outline'
: 'checkbox-blank-circle-outline'
}
/>
</TouchableOpacity>
) : null}
</View>
)}
2020-02-06 23:38:40 +05:00
{children}
2020-09-09 14:55:59 +05:00
</PressableButton>
);
};
export default SelectionWrapper;