convert the old history from localStorage (#21)

* convert the old localStorage["notehistory"] to the new history format, if there is no new history in localStorage
fixes #20

Signed-off-by: Philip Molares <philip.molares@udo.edu>

* used toDate instead of fromUnixTime

Signed-off-by: Philip Molares <philip.molares@udo.edu>

* extracted OldHistoryEntry interface

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2020-05-16 14:24:49 +02:00 committed by GitHub
parent 11a79d501a
commit 194199aee1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,6 +3,7 @@ import {HistoryCard} from "./history-card/history-card";
import {HistoryTable} from "./history-table/history-table";
import {HistoryTableRow} from './history-table/history-table-row';
import {ToggleButton, ToggleButtonGroup} from 'react-bootstrap';
import {toDate} from "date-fns";
interface HistoryChange {
onPinChange: () => void,
@ -27,9 +28,33 @@ interface HistoryEntry {
pinned: boolean
}
interface OldHistoryEntry {
id: string;
text: string;
time: number;
tags: string[];
pinned: boolean;
}
function loadHistoryFromLocalStore() {
const historyJsonString = window.localStorage.getItem("notehistory");
return historyJsonString ? JSON.parse(historyJsonString) : [];
const historyJsonString = window.localStorage.getItem("history");
if (historyJsonString === null) {
// if localStorage["history"] is empty we check the old localStorage["notehistory"]
// and convert it to the new format
const oldHistoryJsonString = window.localStorage.getItem("notehistory")
const oldHistory = oldHistoryJsonString ? JSON.parse(JSON.parse(oldHistoryJsonString)) : [];
return oldHistory.map((entry: OldHistoryEntry) => {
return {
id: entry.id,
title: entry.text,
lastVisited: toDate(entry.time),
tags: entry.tags,
pinned: entry.pinned,
}
})
} else {
return JSON.parse(historyJsonString)
}
}
const History: React.FC = () => {
@ -39,7 +64,7 @@ const History: React.FC = () => {
})
useEffect(() => {
let history = loadHistoryFromLocalStore();
const history = loadHistoryFromLocalStore();
setHistoryEntries(history);
}, [])