2022-08-31 06:33:37 +05:00
|
|
|
/*
|
|
|
|
|
This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
|
2023-01-16 13:44:52 +05:00
|
|
|
Copyright (C) 2023 Streetwriters (Private) Limited
|
2022-08-31 06:33:37 +05:00
|
|
|
|
|
|
|
|
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, { useCallback, useEffect, useState } from "react";
|
2022-08-26 16:19:39 +05:00
|
|
|
import { Text, View } from "react-native";
|
|
|
|
|
import { FlatList } from "react-native-gesture-handler";
|
|
|
|
|
import Icon from "react-native-vector-icons/MaterialCommunityIcons";
|
|
|
|
|
import { db } from "../../common/database";
|
2022-08-29 16:19:17 +05:00
|
|
|
import { presentSheet } from "../../services/event-manager";
|
|
|
|
|
import { useThemeStore } from "../../stores/use-theme-store";
|
2022-08-26 16:19:39 +05:00
|
|
|
import { openLinkInBrowser } from "../../utils/functions";
|
|
|
|
|
import { SIZE } from "../../utils/size";
|
|
|
|
|
import { timeConverter, timeSince } from "../../utils/time";
|
|
|
|
|
import DialogHeader from "../dialog/dialog-header";
|
|
|
|
|
import SheetProvider from "../sheet-provider";
|
|
|
|
|
import { PressableButton } from "../ui/pressable";
|
|
|
|
|
import Seperator from "../ui/seperator";
|
|
|
|
|
import Paragraph from "../ui/typography/paragraph";
|
|
|
|
|
import NotePreview from "./preview";
|
2021-12-14 12:33:39 +05:00
|
|
|
|
2022-06-30 09:45:28 +05:00
|
|
|
export default function NoteHistory({ note, fwdRef }) {
|
2021-12-14 12:33:39 +05:00
|
|
|
const [history, setHistory] = useState([]);
|
2022-08-27 15:23:11 +05:00
|
|
|
const [_loading, setLoading] = useState(true);
|
2022-08-26 16:19:39 +05:00
|
|
|
const colors = useThemeStore((state) => state.colors);
|
2021-12-14 12:33:39 +05:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
(async () => {
|
2021-12-22 13:59:58 +05:00
|
|
|
setHistory([...(await db.noteHistory.get(note.id))]);
|
2021-12-14 12:33:39 +05:00
|
|
|
setLoading(false);
|
|
|
|
|
})();
|
2022-08-30 18:27:09 +05:00
|
|
|
}, [note.id]);
|
2021-12-14 12:33:39 +05:00
|
|
|
|
2022-08-30 18:27:09 +05:00
|
|
|
const preview = useCallback(async (item) => {
|
2021-12-23 12:21:30 +05:00
|
|
|
let content = await db.noteHistory.content(item.id);
|
2021-12-14 12:33:39 +05:00
|
|
|
presentSheet({
|
2021-12-22 13:59:58 +05:00
|
|
|
component: (
|
|
|
|
|
<NotePreview
|
|
|
|
|
session={{
|
|
|
|
|
...item,
|
|
|
|
|
session: getDate(item.dateCreated, item.dateModified)
|
|
|
|
|
}}
|
|
|
|
|
content={content}
|
|
|
|
|
/>
|
|
|
|
|
),
|
2022-08-26 16:19:39 +05:00
|
|
|
context: "note_history"
|
2021-12-14 12:33:39 +05:00
|
|
|
});
|
2022-08-30 18:27:09 +05:00
|
|
|
}, []);
|
2021-12-14 12:33:39 +05:00
|
|
|
|
2021-12-22 13:59:58 +05:00
|
|
|
const getDate = (start, end) => {
|
|
|
|
|
let _start = timeConverter(start);
|
|
|
|
|
let _end = timeConverter(end + 60000);
|
|
|
|
|
if (_start === _end) return _start;
|
2022-08-26 16:19:39 +05:00
|
|
|
let final = _end.lastIndexOf(",");
|
2021-12-22 13:59:58 +05:00
|
|
|
let part = _end.slice(0, final + 1);
|
|
|
|
|
if (_start.includes(part)) {
|
2022-08-26 16:19:39 +05:00
|
|
|
return _start + " —" + _end.replace(part, "");
|
2021-12-22 13:59:58 +05:00
|
|
|
}
|
2022-08-26 16:19:39 +05:00
|
|
|
return _start + " — " + _end;
|
2021-12-22 13:59:58 +05:00
|
|
|
};
|
|
|
|
|
|
2021-12-14 12:33:39 +05:00
|
|
|
const renderItem = useCallback(
|
2022-08-27 15:23:11 +05:00
|
|
|
({ item }) => (
|
2021-12-14 12:33:39 +05:00
|
|
|
<PressableButton
|
|
|
|
|
type="grayBg"
|
|
|
|
|
onPress={() => preview(item)}
|
|
|
|
|
customStyle={{
|
2022-08-26 16:19:39 +05:00
|
|
|
justifyContent: "space-between",
|
|
|
|
|
alignItems: "center",
|
2021-12-14 12:33:39 +05:00
|
|
|
paddingHorizontal: 12,
|
|
|
|
|
height: 45,
|
2021-12-22 13:59:58 +05:00
|
|
|
marginBottom: 10,
|
2022-08-26 16:19:39 +05:00
|
|
|
flexDirection: "row"
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2021-12-22 13:59:58 +05:00
|
|
|
<Paragraph>{getDate(item.dateCreated, item.dateModified)}</Paragraph>
|
|
|
|
|
<Paragraph color={colors.icon} size={SIZE.xs}>
|
|
|
|
|
{timeSince(item.dateModified)}
|
|
|
|
|
</Paragraph>
|
2021-12-14 12:33:39 +05:00
|
|
|
</PressableButton>
|
|
|
|
|
),
|
2022-08-30 18:27:09 +05:00
|
|
|
[colors.icon, preview]
|
2021-12-14 12:33:39 +05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<View>
|
2022-02-28 13:48:59 +05:00
|
|
|
<SheetProvider context="note_history" />
|
2021-12-14 12:33:39 +05:00
|
|
|
<DialogHeader
|
|
|
|
|
title="Note history"
|
|
|
|
|
paragraph="Revert back to an older version of this note"
|
|
|
|
|
padding={12}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<Seperator />
|
|
|
|
|
|
|
|
|
|
<FlatList
|
|
|
|
|
onMomentumScrollEnd={() => {
|
2022-06-30 09:45:28 +05:00
|
|
|
fwdRef?.current?.handleChildScrollEnd();
|
2021-12-14 12:33:39 +05:00
|
|
|
}}
|
|
|
|
|
style={{
|
|
|
|
|
paddingHorizontal: 12
|
|
|
|
|
}}
|
2023-01-03 22:13:43 +05:00
|
|
|
nestedScrollEnabled
|
2022-08-26 16:19:39 +05:00
|
|
|
keyExtractor={(item) => item.id}
|
2021-12-14 12:33:39 +05:00
|
|
|
data={history}
|
2023-01-03 22:13:43 +05:00
|
|
|
ListFooterComponent={<View style={{ height: 250 }} />}
|
2021-12-14 12:33:39 +05:00
|
|
|
ListEmptyComponent={
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
2022-08-26 16:19:39 +05:00
|
|
|
width: "100%",
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
alignItems: "center",
|
2021-12-14 12:33:39 +05:00
|
|
|
height: 200
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2021-12-14 12:33:39 +05:00
|
|
|
<Icon name="history" size={60} color={colors.icon} />
|
2022-08-26 16:19:39 +05:00
|
|
|
<Paragraph color={colors.icon}>
|
|
|
|
|
No note history found on this device.
|
|
|
|
|
</Paragraph>
|
2021-12-14 12:33:39 +05:00
|
|
|
</View>
|
|
|
|
|
}
|
|
|
|
|
renderItem={renderItem}
|
|
|
|
|
/>
|
2021-12-22 13:59:58 +05:00
|
|
|
<Paragraph
|
|
|
|
|
size={SIZE.xs}
|
2021-12-28 13:13:01 +05:00
|
|
|
color={colors.icon}
|
2021-12-22 13:59:58 +05:00
|
|
|
style={{
|
2022-08-26 16:19:39 +05:00
|
|
|
alignSelf: "center"
|
2021-12-28 13:13:01 +05:00
|
|
|
}}
|
2022-01-22 12:57:05 +05:00
|
|
|
>
|
2022-08-26 16:19:39 +05:00
|
|
|
Note version history is local only.{" "}
|
2022-01-22 12:57:05 +05:00
|
|
|
<Text
|
|
|
|
|
onPress={() => {
|
2022-08-26 16:19:39 +05:00
|
|
|
openLinkInBrowser(
|
|
|
|
|
"https://docs.notesnook.com/versionhistory",
|
|
|
|
|
colors
|
|
|
|
|
);
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
2022-08-26 16:19:39 +05:00
|
|
|
style={{ color: colors.accent, textDecorationLine: "underline" }}
|
2022-01-22 12:57:05 +05:00
|
|
|
>
|
2021-12-28 13:13:01 +05:00
|
|
|
Learn how this works.
|
|
|
|
|
</Text>
|
2021-12-22 13:59:58 +05:00
|
|
|
</Paragraph>
|
2021-12-14 12:33:39 +05:00
|
|
|
</View>
|
|
|
|
|
);
|
|
|
|
|
}
|