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

161 lines
4.6 KiB
JavaScript
Raw Normal View History

2022-01-22 12:57:05 +05:00
import React, { useEffect, useState } from 'react';
import { ScrollView, View } from 'react-native';
2022-02-28 13:48:59 +05:00
import BaseDialog from '../dialog/base-dialog';
import { PressableButton } from '../ui/pressable';
import Seperator from '../ui/seperator';
2022-01-22 12:57:05 +05:00
import { useTracked } from '../../provider';
import { useMessageStore } from '../../provider/stores';
import { DDS } from '../../services/DeviceDetection';
import { eSubscribeEvent, eUnSubscribeEvent } from '../../services/EventManager';
import { getElevation } from '../../utils';
2022-02-28 13:48:59 +05:00
import { eCloseJumpToDialog, eOpenJumpToDialog, eScrollEvent } from '../../utils/events';
import { SIZE } from '../../utils/size';
import Heading from '../ui/typography/heading';
import Paragraph from '../ui/typography/paragraph';
2020-11-04 15:06:06 +05:00
2020-11-04 15:42:39 +05:00
const offsets = [];
let timeout = null;
2022-01-22 12:57:05 +05:00
const JumpToDialog = ({ scrollRef, data, type, screen }) => {
2020-11-04 15:42:39 +05:00
const [state] = useTracked();
2022-01-22 12:57:05 +05:00
const { colors } = state;
2021-07-13 14:10:51 +05:00
const notes = data;
2020-11-04 15:42:39 +05:00
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) => {
2022-01-22 12:57:05 +05:00
let ind = notes.findIndex(i => i.title === item.title && i.type === 'header');
2021-11-19 10:29:27 +05:00
console.log(ind);
scrollRef.current?.scrollToIndex({
index: ind,
animated: true
});
2020-11-04 15:42:39 +05:00
close();
};
useEffect(() => {
eSubscribeEvent(eOpenJumpToDialog, open);
eSubscribeEvent(eCloseJumpToDialog, close);
2020-12-17 11:38:50 +05:00
eSubscribeEvent(eScrollEvent, onScroll);
2020-11-04 15:42:39 +05:00
return () => {
eUnSubscribeEvent(eOpenJumpToDialog, open);
eUnSubscribeEvent(eCloseJumpToDialog, close);
2020-12-17 11:38:50 +05:00
eUnSubscribeEvent(eScrollEvent, onScroll);
2020-11-04 15:42:39 +05:00
};
}, []);
2021-11-19 10:29:27 +05:00
const onScroll = data => {
let y = data.y;
2020-11-04 15:42:39 +05:00
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
timeout = setTimeout(() => {
let index = offsets.findIndex((o, i) => o <= y && offsets[i + 1] > y);
2020-12-17 11:38:50 +05:00
setCurrentIndex(index);
}, 200);
2020-11-04 15:42:39 +05:00
};
2021-07-13 14:10:51 +05:00
const open = _type => {
if (_type !== type) return;
2020-11-04 15:42:39 +05:00
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
2021-06-05 01:35:58 +05:00
.filter(i => i.type === 'header')
2020-11-04 15:42:39 +05:00
.map((item, index) => {
let offset = 35 * index;
2022-01-22 12:57:05 +05:00
let ind = notes.findIndex(i => i.title === item.title && i.type === 'header');
2021-07-13 14:10:51 +05:00
let messageState = useMessageStore.getState().message;
let msgOffset = messageState?.visible ? 60 : 10;
2020-11-04 15:42:39 +05:00
ind = ind + 1;
ind = ind - (index + 1);
2021-07-13 14:10:51 +05:00
offset = offset + ind * 100 + msgOffset;
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}
2022-01-22 12:57:05 +05:00
visible={true}
>
2020-11-04 15:42:39 +05:00
<View
style={{
...getElevation(5),
2020-12-22 10:27:51 +05:00
width: DDS.isTab ? 500 : '85%',
2020-11-04 15:42:39 +05:00
backgroundColor: colors.bg,
zIndex: 100,
bottom: 20,
maxHeight: '65%',
borderRadius: 10,
2020-11-04 15:42:39 +05:00
alignSelf: 'center',
padding: 10,
2021-11-19 10:29:27 +05:00
paddingTop: 30
2022-01-22 12:57:05 +05:00
}}
>
2020-11-04 15:42:39 +05:00
<ScrollView
style={{
2021-11-19 10:29:27 +05:00
maxHeight: '100%'
2022-01-22 12:57:05 +05:00
}}
>
2020-11-04 15:42:39 +05:00
<View
style={{
flexDirection: 'row',
flexWrap: 'wrap',
alignSelf: 'center',
justifyContent: 'center',
2021-11-19 10:29:27 +05:00
paddingBottom: 20
2022-01-22 12:57:05 +05:00
}}
>
2020-11-04 15:42:39 +05:00
{notes
2021-06-05 01:35:58 +05:00
.filter(i => i.type === 'header')
2020-12-16 20:45:15 +05:00
.map((item, index) => {
return item.title ? (
<PressableButton
key={item.title}
onPress={() => onPress(item, index)}
2021-11-19 10:29:27 +05:00
type={currentIndex === index ? 'accent' : 'transparent'}
2020-12-16 20:45:15 +05:00
customStyle={{
minWidth: '20%',
width: null,
2021-07-12 14:47:06 +05:00
paddingHorizontal: 12,
2020-12-16 20:45:15 +05:00
margin: 5,
2020-12-17 11:38:50 +05:00
borderRadius: 100,
2021-07-12 14:47:06 +05:00
height: 25,
2021-11-19 10:29:27 +05:00
marginVertical: 10
2022-01-22 12:57:05 +05:00
}}
>
2021-08-04 08:37:00 +05:00
<Paragraph
2020-12-16 20:45:15 +05:00
size={SIZE.sm}
2022-01-22 12:57:05 +05:00
color={currentIndex === index ? colors.light : colors.accent}
2020-12-16 20:45:15 +05:00
style={{
2021-11-19 10:29:27 +05:00
textAlign: 'center'
2022-01-22 12:57:05 +05:00
}}
>
2020-12-16 20:45:15 +05:00
{item.title}
2021-08-04 08:37:00 +05:00
</Paragraph>
2020-12-16 20:45:15 +05:00
</PressableButton>
) : null;
})}
2020-11-04 15:42:39 +05:00
</View>
</ScrollView>
</View>
</BaseDialog>
);
};
export default JumpToDialog;