From 194199aee12ecc8cbe39d2aba61e4d24e76f9cac Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Sat, 16 May 2020 14:24:49 +0200 Subject: [PATCH] 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 * used toDate instead of fromUnixTime Signed-off-by: Philip Molares * extracted OldHistoryEntry interface Signed-off-by: Philip Molares --- .../landing/pages/history/history.tsx | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/components/landing/pages/history/history.tsx b/src/components/landing/pages/history/history.tsx index 8c71a1f0c..d1c0012ac 100644 --- a/src/components/landing/pages/history/history.tsx +++ b/src/components/landing/pages/history/history.tsx @@ -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); }, [])