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

136 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-12-14 12:33:39 +05:00
import React, {useCallback, useEffect, useState} from 'react';
2021-12-28 13:13:01 +05:00
import {Text, View} from 'react-native';
2021-12-14 12:33:39 +05:00
import {FlatList} from 'react-native-gesture-handler';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import {useTracked} from '../../provider';
import {presentSheet} from '../../services/EventManager';
import {db} from '../../utils/database';
2021-12-28 13:13:01 +05:00
import { openLinkInBrowser } from '../../utils/functions';
2021-12-22 13:59:58 +05:00
import {SIZE} from '../../utils/SizeUtils';
import {timeConverter, timeSince} from '../../utils/TimeUtils';
2021-12-14 12:33:39 +05:00
import DialogHeader from '../Dialog/dialog-header';
import GeneralSheet from '../GeneralSheet';
import {PressableButton} from '../PressableButton';
import Seperator from '../Seperator';
import Paragraph from '../Typography/Paragraph';
import NotePreview from './preview';
export default function NoteHistory({note, ref}) {
const [history, setHistory] = useState([]);
const [loading, setLoading] = useState(true);
const [state] = useTracked();
const {colors} = state;
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);
})();
}, []);
async function preview(item) {
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}
/>
),
2021-12-14 12:33:39 +05:00
context: 'note_history',
});
}
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;
let final = _end.lastIndexOf(',');
let part = _end.slice(0, final + 1);
if (_start.includes(part)) {
return _start + ' —' + _end.replace(part, '');
}
return _start + ' — ' + _end;
};
2021-12-14 12:33:39 +05:00
const renderItem = useCallback(
({item, index}) => (
<PressableButton
type="grayBg"
onPress={() => preview(item)}
customStyle={{
2021-12-22 13:59:58 +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,
flexDirection: 'row'
2021-12-14 12:33:39 +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>
),
[]
);
return (
<View>
<GeneralSheet context="note_history" />
<DialogHeader
title="Note history"
paragraph="Revert back to an older version of this note"
padding={12}
/>
<Seperator />
<FlatList
onMomentumScrollEnd={() => {
ref?.current?.handleChildScrollEnd();
}}
style={{
paddingHorizontal: 12
}}
keyExtractor={item => item.id}
data={history}
ListEmptyComponent={
<View
style={{
width: '100%',
justifyContent: 'center',
alignItems: 'center',
height: 200
}}>
<Icon name="history" size={60} color={colors.icon} />
2021-12-28 13:13:01 +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={{
alignSelf: 'center'
}}>
2021-12-28 13:13:01 +05:00
Note version history is local only.{' '}
<Text
onPress={() => {
openLinkInBrowser("https://docs.notesnook.com/versionhistory",colors);
}}
style={{color: colors.accent, textDecorationLine: 'underline'}}>
Learn how this works.
</Text>
2021-12-22 13:59:58 +05:00
</Paragraph>
2021-12-14 12:33:39 +05:00
</View>
);
}