Files
notesnook/apps/mobile/app/components/dialogs/jump-to-section/index.js

194 lines
5.5 KiB
JavaScript
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2022 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-30 16:13:11 +05:00
2022-08-29 16:19:17 +05:00
import React, { useEffect, useState } from "react";
import { ScrollView, View } from "react-native";
import { DDS } from "../../../services/device-detection";
import {
eSubscribeEvent,
eUnSubscribeEvent
} from "../../../services/event-manager";
2022-08-29 16:19:17 +05:00
import { useMessageStore } from "../../../stores/use-message-store";
import { useThemeStore } from "../../../stores/use-theme-store";
import { getElevation } from "../../../utils";
import {
eCloseJumpToDialog,
eOpenJumpToDialog,
eScrollEvent
} from "../../../utils/events";
import { SIZE } from "../../../utils/size";
import BaseDialog from "../../dialog/base-dialog";
import { PressableButton } from "../../ui/pressable";
import Paragraph from "../../ui/typography/paragraph";
2022-08-30 18:27:09 +05:00
import { useCallback } from "react";
2020-11-04 15:06:06 +05:00
2020-11-04 15:42:39 +05:00
const offsets = [];
let timeout = null;
const JumpToSectionDialog = ({ scrollRef, data, type }) => {
const colors = useThemeStore((state) => state.colors);
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) => {
let ind = notes.findIndex(
(i) => i.title === item.title && i.type === "header"
);
2022-07-11 16:18:24 +05:00
console.log(scrollRef.current);
2021-11-19 10:29:27 +05:00
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
};
2022-08-30 18:27:09 +05:00
}, [open]);
2020-11-04 15:42:39 +05:00
const onScroll = (data) => {
2021-11-19 10:29:27 +05:00
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
};
2022-08-30 18:27:09 +05:00
const open = useCallback(
(_type) => {
if (_type !== type) return;
setVisible(true);
},
[type]
);
2020-11-04 15:06:06 +05:00
2020-11-04 15:42:39 +05:00
const close = () => {
setVisible(false);
};
useEffect(() => {
loadOffsets();
2022-08-30 18:27:09 +05:00
}, [loadOffsets, notes]);
2020-11-04 15:42:39 +05:00
2022-08-30 18:27:09 +05:00
const loadOffsets = useCallback(() => {
2020-11-04 15:42:39 +05:00
notes
.filter((i) => i.type === "header")
2020-11-04 15:42:39 +05:00
.map((item, index) => {
let offset = 35 * index;
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);
});
2022-08-30 18:27:09 +05:00
}, [notes]);
2020-11-04 15:42:39 +05:00
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),
width: DDS.isTab ? 500 : "85%",
2020-11-04 15:42:39 +05:00
backgroundColor: colors.bg,
zIndex: 100,
bottom: 20,
maxHeight: "65%",
borderRadius: 10,
alignSelf: "center",
2020-11-04 15:42:39 +05:00
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={{
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
.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)}
type={currentIndex === index ? "accent" : "transparent"}
2020-12-16 20:45:15 +05:00
customStyle={{
minWidth: "20%",
2020-12-16 20:45:15 +05:00
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}
color={
currentIndex === index ? colors.light : colors.accent
}
2020-12-16 20:45:15 +05:00
style={{
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>
);
};
2022-02-28 15:32:55 +05:00
export default JumpToSectionDialog;