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

157 lines
4.4 KiB
JavaScript
Raw Normal View History

2020-11-23 12:32:33 +05:00
import React, {useEffect, useState} from 'react';
import {ScrollView, View} from 'react-native';
2020-11-04 15:06:06 +05:00
import BaseDialog from '../../components/Dialog/base-dialog';
2020-11-23 12:32:33 +05:00
import {PressableButton} from '../../components/PressableButton';
2020-11-04 15:06:06 +05:00
import Seperator from '../../components/Seperator';
2020-11-23 12:32:33 +05:00
import {useTracked} from '../../provider';
import {DDS} from '../../services/DeviceDetection';
import {eSubscribeEvent, eUnSubscribeEvent} from '../../services/EventManager';
import {getElevation, scrollRef} from '../../utils';
import {eCloseJumpToDialog, eOpenJumpToDialog} from '../../utils/Events';
import {SIZE} from '../../utils/SizeUtils';
2020-11-20 01:23:05 +05:00
import Heading from '../Typography/Heading';
2020-11-04 15:06:06 +05:00
import Paragraph from '../Typography/Paragraph';
2020-11-04 15:42:39 +05:00
const offsets = [];
let timeout = null;
2020-11-04 15:06:06 +05:00
const JumpToDialog = () => {
2020-11-04 15:42:39 +05:00
const [state] = useTracked();
const {notes, colors, settings} = state;
const [visible, setVisible] = useState(false);
2020-11-16 09:42:34 +05:00
const [currentIndex, setCurrentIndex] = useState(null);
2020-11-04 15:42:39 +05:00
const onPress = (item, index) => {
let offset = 35 * index;
2020-11-04 15:42:39 +05:00
let ind = notes.findIndex(
(i) => i.title === item.title && i.type === 'header',
);
ind = ind + 1;
ind = ind - (index + 1);
offset = offset + ind * 100 + 200;
scrollRef.current?.scrollToOffset(0, index === 0 ? 0 : offset, true);
2020-11-04 15:42:39 +05:00
close();
};
useEffect(() => {
eSubscribeEvent(eOpenJumpToDialog, open);
eSubscribeEvent(eCloseJumpToDialog, close);
//eSubscribeEvent(eScrollEvent, onScroll);
2020-11-04 15:42:39 +05:00
return () => {
eUnSubscribeEvent(eOpenJumpToDialog, open);
eUnSubscribeEvent(eCloseJumpToDialog, close);
//eUnSubscribeEvent(eScrollEvent, onScroll);
2020-11-04 15:42:39 +05:00
};
}, []);
const onScroll = (y) => {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
timeout = setTimeout(() => {
let index = offsets.findIndex((o, i) => o <= y && offsets[i + 1] > y);
//setCurrentIndex(index);
}, 100);
2020-11-04 15:42:39 +05:00
};
const open = () => {
setVisible(true);
2020-11-04 15:06:06 +05:00
};
2020-11-04 15:42:39 +05:00
const close = () => {
setVisible(false);
};
useEffect(() => {
loadOffsets();
}, [notes]);
const loadOffsets = () => {
notes
.filter((i) => i.type === 'header')
.map((item, index) => {
let offset = 35 * index;
2020-11-04 15:42:39 +05:00
let ind = notes.findIndex(
(i) => i.title === item.title && i.type === 'header',
);
ind = ind + 1;
ind = ind - (index + 1);
offset = offset + ind * 100 + 200;
2020-11-04 15:42:39 +05:00
offsets.push(offset);
});
};
2020-11-23 12:32:33 +05:00
return !visible ? null : (
2020-11-04 15:42:39 +05:00
<BaseDialog
onShow={() => {
loadOffsets();
}}
onRequestClose={close}
2020-11-23 12:32:33 +05:00
visible={true}>
2020-11-04 15:42:39 +05:00
<View
style={{
...getElevation(5),
width: DDS.isTab ? 500 : '80%',
backgroundColor: colors.bg,
zIndex: 100,
bottom: 20,
maxHeight: '65%',
borderRadius: 5,
alignSelf: 'center',
padding: 10,
}}>
2020-11-20 01:23:05 +05:00
<Heading
size={SIZE.xl}
2020-11-04 15:42:39 +05:00
style={{
alignSelf: 'center',
}}>
{settings.sort.slice(0, 1).toUpperCase() +
settings.sort.slice(1, settings.sort.length)}
2020-11-20 01:23:05 +05:00
</Heading>
2020-11-04 15:42:39 +05:00
<Seperator />
<ScrollView
style={{
maxHeight: '100%',
}}>
<View
style={{
flexDirection: 'row',
flexWrap: 'wrap',
alignSelf: 'center',
justifyContent: 'center',
}}>
{notes
.filter((i) => i.type === 'header')
.map((item, index) => (
<PressableButton
key={item.title}
2020-11-04 15:42:39 +05:00
onPress={() => onPress(item, index)}
2020-12-01 17:51:39 +05:00
type={currentIndex === index? 'shade' : 'gray'}
2020-11-04 15:42:39 +05:00
customStyle={{
minWidth: '20%',
maxWidth: '46%',
width: null,
padding: 15,
margin: 5,
}}>
<Paragraph
size={SIZE.xs}
color={currentIndex === index ? colors.accent : null}
style={{
textAlign: 'center',
}}>
{item.title}
</Paragraph>
</PressableButton>
))}
</View>
</ScrollView>
</View>
</BaseDialog>
);
};
export default JumpToDialog;