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

204 lines
5.7 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';
2021-07-13 14:10:51 +05:00
import {
useMessageStore,
useNoteStore,
useSettingStore,
} from '../../provider/stores';
2020-11-23 12:32:33 +05:00
import {DDS} from '../../services/DeviceDetection';
import {eSubscribeEvent, eUnSubscribeEvent} from '../../services/EventManager';
2021-06-05 01:35:58 +05:00
import {getElevation} 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';
2021-08-04 08:37:00 +05:00
import Paragraph from '../Typography/Paragraph';
2020-11-04 15:06:06 +05:00
2020-11-04 15:42:39 +05:00
const offsets = [];
let timeout = null;
2021-07-13 14:10:51 +05:00
const JumpToDialog = ({scrollRef, data, type, screen}) => {
2020-11-04 15:42:39 +05:00
const [state] = useTracked();
2021-06-05 21:10:20 +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) => {
let offset = 35 * index;
2020-11-04 15:42:39 +05:00
let ind = notes.findIndex(
2021-06-05 01:35:58 +05:00
i => i.title === item.title && i.type === 'header',
2020-11-04 15:42:39 +05:00
);
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;
console.log(scrollRef.current?.scrollToOffset);
scrollRef.current?.scrollToOffset({
offset: offset,
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-06-05 01:35:58 +05:00
const onScroll = 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;
2020-11-04 15:42:39 +05:00
let ind = notes.findIndex(
2021-06-05 01:35:58 +05:00
i => i.title === item.title && i.type === 'header',
2020-11-04 15:42:39 +05:00
);
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}
2020-11-23 12:32:33 +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: 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',
}}>
2021-07-12 14:47:06 +05:00
Jump to
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);
2021-06-05 01:35:58 +05:00
close();
2020-12-17 11:38:50 +05:00
}}
2021-06-05 01:35:58 +05:00
type="shade"
2020-12-17 11:38:50 +05:00
customStyle={{
minWidth: '20%',
width: null,
2021-07-12 14:47:06 +05:00
paddingHorizontal: 12,
2020-12-17 11:38:50 +05:00
margin: 5,
borderRadius: 100,
2021-07-12 14:47:06 +05:00
height: 25,
2020-12-17 11:38:50 +05:00
}}>
2021-08-04 08:37:00 +05:00
<Paragraph
2020-12-17 11:38:50 +05:00
size={SIZE.sm}
color={colors.accent}
style={{
textAlign: 'center',
}}>
Top
2021-08-04 08:37:00 +05:00
</Paragraph>
2020-12-17 11:38:50 +05:00
</PressableButton>
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)}
2020-12-17 11:38:50 +05:00
type={currentIndex === index ? 'accent' : 'shade'}
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,
2020-11-04 15:42:39 +05:00
}}>
2021-08-04 08:37:00 +05:00
<Paragraph
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}
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;