2021-02-06 13:32:38 +11:00
|
|
|
import React, { useRef, useState, useEffect } from "react";
|
2021-01-26 21:49:44 +11:00
|
|
|
import { Box, Label, Text, Button, Flex } from "theme-ui";
|
2021-02-07 22:53:47 +11:00
|
|
|
import { saveAs } from "file-saver";
|
2021-01-27 11:57:23 +11:00
|
|
|
import * as Comlink from "comlink";
|
2021-01-26 21:49:44 +11:00
|
|
|
|
|
|
|
|
import Modal from "../components/Modal";
|
|
|
|
|
import LoadingOverlay from "../components/LoadingOverlay";
|
|
|
|
|
import LoadingBar from "../components/LoadingBar";
|
2021-02-13 13:21:13 +11:00
|
|
|
import Banner from "../components/Banner";
|
2021-01-26 21:49:44 +11:00
|
|
|
|
2021-01-27 11:57:23 +11:00
|
|
|
import DatabaseWorker from "worker-loader!../workers/DatabaseWorker"; // eslint-disable-line import/no-webpack-loader-syntax
|
|
|
|
|
|
|
|
|
|
const worker = Comlink.wrap(new DatabaseWorker());
|
|
|
|
|
|
2021-02-13 13:21:13 +11:00
|
|
|
function ImportExportModal({ isOpen, onRequestClose }) {
|
2021-01-26 21:49:44 +11:00
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
2021-02-13 13:21:13 +11:00
|
|
|
const [error, setError] = useState();
|
2021-01-26 21:49:44 +11:00
|
|
|
|
2021-01-27 11:57:23 +11:00
|
|
|
const backgroundTaskRunningRef = useRef(false);
|
2021-01-26 21:49:44 +11:00
|
|
|
const fileInputRef = useRef();
|
|
|
|
|
|
|
|
|
|
function openFileDialog() {
|
|
|
|
|
if (fileInputRef.current) {
|
|
|
|
|
fileInputRef.current.click();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const loadingProgressRef = useRef(0);
|
|
|
|
|
|
|
|
|
|
function handleDBProgress({ completedRows, totalRows }) {
|
|
|
|
|
loadingProgressRef.current = completedRows / totalRows;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function handleImportDatabase(file) {
|
|
|
|
|
setIsLoading(true);
|
2021-01-27 11:57:23 +11:00
|
|
|
backgroundTaskRunningRef.current = true;
|
2021-02-13 13:21:13 +11:00
|
|
|
try {
|
|
|
|
|
await worker.importData(file, Comlink.proxy(handleDBProgress));
|
|
|
|
|
setIsLoading(false);
|
|
|
|
|
backgroundTaskRunningRef.current = false;
|
|
|
|
|
window.location.reload();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
setIsLoading(false);
|
|
|
|
|
backgroundTaskRunningRef.current = false;
|
|
|
|
|
if (e.message.startsWith("Max buffer length exceeded")) {
|
|
|
|
|
setError(
|
|
|
|
|
new Error(
|
|
|
|
|
"Max image size exceeded ensure your database doesn't have an image over 100MB"
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
setError(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-26 21:49:44 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function handleExportDatabase() {
|
|
|
|
|
setIsLoading(true);
|
2021-01-27 11:57:23 +11:00
|
|
|
backgroundTaskRunningRef.current = true;
|
2021-02-13 13:21:13 +11:00
|
|
|
try {
|
|
|
|
|
const blob = await worker.exportData(Comlink.proxy(handleDBProgress));
|
|
|
|
|
saveAs(blob, `${new Date().toISOString()}.owlbear`);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
setError(e);
|
|
|
|
|
}
|
2021-01-27 11:57:23 +11:00
|
|
|
setIsLoading(false);
|
2021-02-07 22:20:27 +11:00
|
|
|
backgroundTaskRunningRef.current = false;
|
2021-01-26 21:49:44 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
function handleBeforeUnload(event) {
|
2021-01-27 11:57:23 +11:00
|
|
|
if (backgroundTaskRunningRef.current) {
|
2021-01-26 21:49:44 +11:00
|
|
|
event.returnValue =
|
|
|
|
|
"Database is still processing, are you sure you want to leave?";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.addEventListener("beforeunload", handleBeforeUnload);
|
|
|
|
|
return () => {
|
|
|
|
|
window.removeEventListener("beforeunload", handleBeforeUnload);
|
|
|
|
|
};
|
2021-01-27 11:57:23 +11:00
|
|
|
}, []);
|
2021-01-26 21:49:44 +11:00
|
|
|
|
2021-02-07 22:53:47 +11:00
|
|
|
function handleClose() {
|
|
|
|
|
if (isLoading) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
onRequestClose();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-26 21:49:44 +11:00
|
|
|
return (
|
|
|
|
|
<Modal isOpen={isOpen} onRequestClose={handleClose} allowClose={!isLoading}>
|
|
|
|
|
<Flex
|
|
|
|
|
sx={{
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
maxWidth: "300px",
|
|
|
|
|
flexGrow: 1,
|
|
|
|
|
}}
|
|
|
|
|
m={2}
|
|
|
|
|
>
|
|
|
|
|
<Box>
|
|
|
|
|
<Label>Import / Export Database</Label>
|
|
|
|
|
<Text as="p" mb={2} variant="caption">
|
|
|
|
|
Importing a database will overwrite your current data.
|
|
|
|
|
</Text>
|
|
|
|
|
<input
|
|
|
|
|
onChange={(event) => handleImportDatabase(event.target.files[0])}
|
|
|
|
|
type="file"
|
2021-01-27 12:03:10 +11:00
|
|
|
accept=".owlbear"
|
2021-01-26 21:49:44 +11:00
|
|
|
style={{ display: "none" }}
|
|
|
|
|
ref={fileInputRef}
|
|
|
|
|
/>
|
|
|
|
|
<Flex>
|
|
|
|
|
<Button mr={1} sx={{ flexGrow: 1 }} onClick={openFileDialog}>
|
|
|
|
|
Import
|
|
|
|
|
</Button>
|
|
|
|
|
<Button ml={1} sx={{ flexGrow: 1 }} onClick={handleExportDatabase}>
|
|
|
|
|
Export
|
|
|
|
|
</Button>
|
|
|
|
|
</Flex>
|
|
|
|
|
</Box>
|
|
|
|
|
{isLoading && (
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
width: "100%",
|
|
|
|
|
height: "100%",
|
|
|
|
|
position: "absolute",
|
|
|
|
|
top: 0,
|
|
|
|
|
left: 0,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<LoadingOverlay />
|
|
|
|
|
<LoadingBar
|
|
|
|
|
isLoading={isLoading}
|
|
|
|
|
loadingProgressRef={loadingProgressRef}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
2021-02-13 13:21:13 +11:00
|
|
|
<Banner isOpen={!!error} onRequestClose={() => setError()}>
|
|
|
|
|
<Box p={1}>
|
|
|
|
|
<Text as="p" variant="body2">
|
|
|
|
|
Error: {error && error.message}
|
|
|
|
|
</Text>
|
|
|
|
|
</Box>
|
|
|
|
|
</Banner>
|
2021-01-26 21:49:44 +11:00
|
|
|
</Flex>
|
|
|
|
|
</Modal>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-13 13:21:13 +11:00
|
|
|
export default ImportExportModal;
|