|
|
|
@ -1,7 +1,9 @@
|
|
|
|
|
import { useContext } from "react";
|
|
|
|
|
import appContext from "../stores/appContext";
|
|
|
|
|
import { globalStateService, memoService } from "../services";
|
|
|
|
|
import utils from "../helpers/utils";
|
|
|
|
|
import { formatMemoContent } from "./Memo";
|
|
|
|
|
import toastHelper from "./Toast";
|
|
|
|
|
import "../less/preferences-section.less";
|
|
|
|
|
|
|
|
|
|
interface Props {}
|
|
|
|
@ -41,13 +43,49 @@ const PreferencesSection: React.FC<Props> = () => {
|
|
|
|
|
const jsonStr = JSON.stringify(formatedMemos);
|
|
|
|
|
const element = document.createElement("a");
|
|
|
|
|
element.setAttribute("href", "data:text/json;charset=utf-8," + encodeURIComponent(jsonStr));
|
|
|
|
|
element.setAttribute("download", "data.json");
|
|
|
|
|
element.setAttribute("download", `memos-${utils.getDateTimeString(Date.now())}.json`);
|
|
|
|
|
element.style.display = "none";
|
|
|
|
|
document.body.appendChild(element);
|
|
|
|
|
element.click();
|
|
|
|
|
document.body.removeChild(element);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleImportBtnClick = async () => {
|
|
|
|
|
const fileInputEl = document.createElement("input");
|
|
|
|
|
fileInputEl.type = "file";
|
|
|
|
|
fileInputEl.accept = "application/JSON";
|
|
|
|
|
fileInputEl.onchange = () => {
|
|
|
|
|
if (fileInputEl.files?.length && fileInputEl.files.length > 0) {
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.readAsText(fileInputEl.files[0]);
|
|
|
|
|
reader.onload = async (event) => {
|
|
|
|
|
const memoList = JSON.parse(event.target?.result as string) as Model.Memo[];
|
|
|
|
|
if (!Array.isArray(memoList)) {
|
|
|
|
|
toastHelper.error("Unexpected data type.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let succeedAmount = 0;
|
|
|
|
|
|
|
|
|
|
for (const memo of memoList) {
|
|
|
|
|
const content = memo.content || "";
|
|
|
|
|
const createdAt = memo.createdAt || utils.getDateTimeString(Date.now());
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await memoService.importMemo(content, createdAt);
|
|
|
|
|
succeedAmount++;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// do nth
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await memoService.fetchAllMemos();
|
|
|
|
|
toastHelper.success(`${succeedAmount} memos successfully imported.`);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
fileInputEl.click();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<div className="section-container preferences-section-container">
|
|
|
|
@ -75,6 +113,9 @@ const PreferencesSection: React.FC<Props> = () => {
|
|
|
|
|
<button className="px-2 py-1 border rounded text-base hover:opacity-80" onClick={handleExportBtnClick}>
|
|
|
|
|
Export data as JSON
|
|
|
|
|
</button>
|
|
|
|
|
<button className="ml-2 px-2 py-1 border rounded text-base hover:opacity-80" onClick={handleImportBtnClick}>
|
|
|
|
|
Import from JSON
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|