2021-01-26 21:49:44 +11:00
|
|
|
import React, { useRef, useState, useContext, useEffect } from "react";
|
|
|
|
|
import { Box, Label, Text, Button, Flex } from "theme-ui";
|
|
|
|
|
import streamSaver from "streamsaver";
|
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";
|
|
|
|
|
|
|
|
|
|
import DatabaseContext from "../contexts/DatabaseContext";
|
|
|
|
|
|
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-01-26 21:49:44 +11:00
|
|
|
function ImportDatabaseModal({ isOpen, onRequestClose }) {
|
|
|
|
|
const { database } = useContext(DatabaseContext);
|
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
|
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-01-26 21:49:44 +11:00
|
|
|
await database.delete();
|
2021-01-27 11:57:23 +11:00
|
|
|
await worker.importData(file, Comlink.proxy(handleDBProgress));
|
2021-01-26 21:49:44 +11:00
|
|
|
setIsLoading(false);
|
2021-01-27 11:57:23 +11:00
|
|
|
backgroundTaskRunningRef.current = false;
|
2021-01-26 21:49:44 +11:00
|
|
|
window.location.reload();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fileStreamRef = useRef();
|
|
|
|
|
|
|
|
|
|
async function handleExportDatabase() {
|
|
|
|
|
setIsLoading(true);
|
2021-01-27 11:57:23 +11:00
|
|
|
backgroundTaskRunningRef.current = true;
|
|
|
|
|
|
|
|
|
|
await worker.exportData(Comlink.proxy(handleDBProgress));
|
|
|
|
|
const blob = await worker.data;
|
|
|
|
|
|
|
|
|
|
setIsLoading(false);
|
|
|
|
|
|
2021-01-26 21:49:44 +11:00
|
|
|
const fileStream = streamSaver.createWriteStream(
|
2021-01-27 12:03:10 +11:00
|
|
|
`${new Date().toISOString()}.owlbear`,
|
2021-01-26 21:49:44 +11:00
|
|
|
{
|
|
|
|
|
size: blob.size,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
fileStreamRef.current = fileStream;
|
|
|
|
|
|
|
|
|
|
const readableStream = blob.stream();
|
|
|
|
|
if (window.WritableStream && readableStream.pipeTo) {
|
|
|
|
|
await readableStream.pipeTo(fileStream);
|
2021-01-27 11:57:23 +11:00
|
|
|
backgroundTaskRunningRef.current = false;
|
2021-01-26 21:49:44 +11:00
|
|
|
} else {
|
|
|
|
|
const writer = fileStream.getWriter();
|
|
|
|
|
const reader = readableStream.getReader();
|
|
|
|
|
async function pump() {
|
|
|
|
|
const res = await reader.read();
|
|
|
|
|
if (res.done) {
|
|
|
|
|
writer.close();
|
|
|
|
|
} else {
|
|
|
|
|
writer.write(res.value).then(pump);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
await pump();
|
2021-01-27 11:57:23 +11:00
|
|
|
backgroundTaskRunningRef.current = false;
|
2021-01-26 21:49:44 +11:00
|
|
|
}
|
|
|
|
|
fileStreamRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleClose() {
|
|
|
|
|
if (isLoading) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
onRequestClose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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?";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleUnload() {
|
|
|
|
|
if (fileStreamRef.current) {
|
|
|
|
|
fileStreamRef.current.abort();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.addEventListener("beforeunload", handleBeforeUnload);
|
|
|
|
|
window.addEventListener("unload", handleUnload);
|
|
|
|
|
return () => {
|
|
|
|
|
window.removeEventListener("beforeunload", handleBeforeUnload);
|
|
|
|
|
window.removeEventListener("unload", handleUnload);
|
|
|
|
|
};
|
2021-01-27 11:57:23 +11:00
|
|
|
}, []);
|
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>
|
|
|
|
|
)}
|
|
|
|
|
</Flex>
|
|
|
|
|
</Modal>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ImportDatabaseModal;
|