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

191 lines
5.3 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';
2020-12-17 11:38:50 +05:00
import {
eCloseJumpToDialog,
eOpenJumpToDialog,
eScrollEvent,
} from '../../utils/Events';
2020-11-23 12:32:33 +05:00
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
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);
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
};
}, []);
const onScroll = (y) => {
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
};
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);
2020-12-17 11:38:50 +05:00
offset = offset + ind * 100 + 190;
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',
2020-12-17 11:38:50 +05:00
paddingBottom: 20,
2020-11-04 15:42:39 +05:00
}}>
2020-12-17 11:38:50 +05:00
<PressableButton
key="go to top"
onPress={() => {
scrollRef.current?.scrollToOffset(0, 0, true);
close()
}}
type='shade'
customStyle={{
minWidth: '20%',
maxWidth: '46%',
width: null,
paddingHorizontal: 10,
margin: 5,
borderRadius: 100,
height: 22,
}}>
<Heading
size={SIZE.sm}
color={colors.accent}
style={{
textAlign: 'center',
}}>
Top
</Heading>
</PressableButton>
2020-11-04 15:42:39 +05:00
{notes
.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)}
2020-12-17 11:38:50 +05:00
type={currentIndex === index ? 'accent' : 'shade'}
2020-12-16 20:45:15 +05:00
customStyle={{
minWidth: '20%',
maxWidth: '46%',
width: null,
2020-12-17 11:38:50 +05:00
paddingHorizontal: 0,
2020-12-16 20:45:15 +05:00
margin: 5,
2020-12-17 11:38:50 +05:00
borderRadius: 100,
height: 22,
2020-11-04 15:42:39 +05:00
}}>
2020-12-17 11:38:50 +05:00
<Heading
2020-12-16 20:45:15 +05:00
size={SIZE.sm}
2020-12-17 11:38:50 +05:00
color={
currentIndex === index ? colors.light : colors.accent
}
2020-12-16 20:45:15 +05:00
style={{
textAlign: 'center',
}}>
{item.title}
2020-12-17 11:38:50 +05:00
</Heading>
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;