mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-12-22 20:11:38 +00:00
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:
parent
11a79d501a
commit
194199aee1
1 changed files with 28 additions and 3 deletions
|
@ -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);
|
||||
}, [])
|
||||
|
||||
|
|
Loading…
Reference in a new issue