2021-02-06 13:32:38 +11:00
|
|
|
import React, { useState, useEffect } from "react";
|
2020-11-26 13:17:53 +11:00
|
|
|
import {
|
|
|
|
|
Label,
|
|
|
|
|
Flex,
|
|
|
|
|
Button,
|
|
|
|
|
useColorMode,
|
|
|
|
|
Checkbox,
|
|
|
|
|
Divider,
|
|
|
|
|
Text,
|
|
|
|
|
} from "theme-ui";
|
|
|
|
|
import prettyBytes from "pretty-bytes";
|
2020-04-26 18:24:05 +10:00
|
|
|
|
|
|
|
|
import Modal from "../components/Modal";
|
2020-11-06 13:35:11 +11:00
|
|
|
import Slider from "../components/Slider";
|
2020-04-26 18:24:05 +10:00
|
|
|
|
2021-02-06 13:32:38 +11:00
|
|
|
import { useAuth } from "../contexts/AuthContext";
|
|
|
|
|
import { useDatabase } from "../contexts/DatabaseContext";
|
2020-04-26 18:24:05 +10:00
|
|
|
|
2021-02-04 15:06:34 +11:00
|
|
|
import useSetting from "../hooks/useSetting";
|
2020-09-08 15:06:15 +10:00
|
|
|
|
2020-10-10 15:32:59 +11:00
|
|
|
import ConfirmModal from "./ConfirmModal";
|
2021-01-26 21:49:44 +11:00
|
|
|
import ImportExportModal from "./ImportExportModal";
|
2020-10-10 15:32:59 +11:00
|
|
|
|
2020-04-26 18:24:05 +10:00
|
|
|
function SettingsModal({ isOpen, onRequestClose }) {
|
2021-02-06 13:32:38 +11:00
|
|
|
const { database, databaseStatus } = useDatabase();
|
|
|
|
|
const { userId } = useAuth();
|
2020-04-26 18:24:05 +10:00
|
|
|
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
|
2020-09-08 15:06:15 +10:00
|
|
|
const [labelSize, setLabelSize] = useSetting("map.labelSize");
|
2020-11-27 19:36:44 +11:00
|
|
|
const [storageEstimate, setStorageEstimate] = useState();
|
2021-01-26 21:49:44 +11:00
|
|
|
const [isImportExportModalOpen, setIsImportExportModalOpen] = useState(false);
|
2020-11-26 13:17:53 +11:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-11-27 21:00:08 +11:00
|
|
|
async function estimateStorage() {
|
|
|
|
|
// Persisted data on firefox doesn't count towards the usage quota so ignore it
|
|
|
|
|
const persisted = await navigator.storage.persisted();
|
|
|
|
|
const isFirefox =
|
|
|
|
|
navigator.userAgent.toLowerCase().indexOf("firefox") > -1;
|
|
|
|
|
if (persisted && isFirefox) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const estimate = await navigator.storage.estimate();
|
|
|
|
|
setStorageEstimate(estimate);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 19:36:44 +11:00
|
|
|
if (isOpen && navigator.storage) {
|
2020-11-27 21:00:08 +11:00
|
|
|
estimateStorage();
|
2020-11-26 13:17:53 +11:00
|
|
|
}
|
|
|
|
|
}, [isOpen]);
|
2020-04-26 18:24:05 +10:00
|
|
|
|
|
|
|
|
async function handleEraseAllData() {
|
2020-08-12 10:37:28 +10:00
|
|
|
localStorage.clear();
|
2020-05-03 18:22:09 +10:00
|
|
|
await database.delete();
|
2020-04-26 18:24:05 +10:00
|
|
|
window.location.reload();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-30 14:24:47 +10:00
|
|
|
async function handleClearCache() {
|
2020-08-12 10:37:28 +10:00
|
|
|
// Clear saved settings
|
|
|
|
|
localStorage.clear();
|
|
|
|
|
// Clear map cache
|
2020-05-03 18:22:09 +10:00
|
|
|
await database.table("maps").where("owner").notEqual(userId).delete();
|
2020-05-20 11:35:14 +10:00
|
|
|
// Find all other peoples tokens who aren't benig used in a map state and delete them
|
|
|
|
|
const tokens = await database
|
|
|
|
|
.table("tokens")
|
|
|
|
|
.where("owner")
|
|
|
|
|
.notEqual(userId)
|
|
|
|
|
.toArray();
|
|
|
|
|
const states = await database.table("states").toArray();
|
|
|
|
|
for (let token of tokens) {
|
|
|
|
|
let inUse = false;
|
|
|
|
|
for (let state of states) {
|
|
|
|
|
for (let tokenState of Object.values(state.tokens)) {
|
|
|
|
|
if (token.id === tokenState.tokenId) {
|
|
|
|
|
inUse = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!inUse) {
|
|
|
|
|
database.table("tokens").delete(token.id);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-30 14:24:47 +10:00
|
|
|
window.location.reload();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-26 19:04:18 +10:00
|
|
|
const [colorMode, setColorMode] = useColorMode();
|
|
|
|
|
|
2020-04-26 18:24:05 +10:00
|
|
|
return (
|
2020-04-26 19:04:18 +10:00
|
|
|
<>
|
|
|
|
|
<Modal isOpen={isOpen} onRequestClose={onRequestClose}>
|
|
|
|
|
<Flex sx={{ flexDirection: "column" }}>
|
|
|
|
|
<Label py={2}>Settings</Label>
|
2020-09-08 15:06:15 +10:00
|
|
|
<Divider bg="text" />
|
2020-09-23 15:12:52 +10:00
|
|
|
<Label py={2}>Accessibility:</Label>
|
2020-04-26 19:04:18 +10:00
|
|
|
<Label py={2}>
|
2020-09-08 15:06:15 +10:00
|
|
|
<span style={{ marginRight: "4px" }}>Light theme</span>
|
2020-04-26 19:04:18 +10:00
|
|
|
<Checkbox
|
|
|
|
|
checked={colorMode === "light"}
|
|
|
|
|
onChange={(e) =>
|
|
|
|
|
setColorMode(e.target.checked ? "light" : "default")
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</Label>
|
2020-09-08 15:06:15 +10:00
|
|
|
<Label py={2}>
|
|
|
|
|
Token Label Size
|
|
|
|
|
<Slider
|
|
|
|
|
step={0.5}
|
|
|
|
|
min={1}
|
|
|
|
|
max={3}
|
|
|
|
|
ml={1}
|
|
|
|
|
sx={{ width: "initial" }}
|
|
|
|
|
value={labelSize}
|
|
|
|
|
onChange={(e) => setLabelSize(parseFloat(e.target.value))}
|
2020-11-06 13:35:11 +11:00
|
|
|
labelFunc={(value) => `${value}x`}
|
2020-09-08 15:06:15 +10:00
|
|
|
/>
|
|
|
|
|
</Label>
|
|
|
|
|
<Divider bg="text" />
|
2020-04-30 14:24:47 +10:00
|
|
|
<Flex py={2}>
|
|
|
|
|
<Button sx={{ flexGrow: 1 }} onClick={handleClearCache}>
|
|
|
|
|
Clear cache
|
|
|
|
|
</Button>
|
|
|
|
|
</Flex>
|
2020-04-26 19:04:18 +10:00
|
|
|
<Flex py={2}>
|
|
|
|
|
<Button
|
|
|
|
|
sx={{ flexGrow: 1 }}
|
|
|
|
|
onClick={() => setIsDeleteModalOpen(true)}
|
|
|
|
|
>
|
|
|
|
|
Erase all content and reset
|
|
|
|
|
</Button>
|
|
|
|
|
</Flex>
|
2021-01-26 21:49:44 +11:00
|
|
|
<Flex py={2}>
|
|
|
|
|
<Button
|
|
|
|
|
sx={{ flexGrow: 1 }}
|
|
|
|
|
onClick={() => setIsImportExportModalOpen(true)}
|
2021-01-27 12:06:37 +11:00
|
|
|
disabled={databaseStatus !== "loaded"}
|
2021-01-26 21:49:44 +11:00
|
|
|
>
|
|
|
|
|
Import / Export Database
|
|
|
|
|
</Button>
|
|
|
|
|
</Flex>
|
2020-11-27 19:36:44 +11:00
|
|
|
{storageEstimate && (
|
|
|
|
|
<Flex sx={{ justifyContent: "center" }}>
|
|
|
|
|
<Text variant="caption">
|
|
|
|
|
Storage Used: {prettyBytes(storageEstimate.usage)} of{" "}
|
|
|
|
|
{prettyBytes(storageEstimate.quota)} (
|
|
|
|
|
{Math.round(
|
|
|
|
|
(storageEstimate.usage / Math.max(storageEstimate.quota, 1)) *
|
|
|
|
|
100
|
|
|
|
|
)}
|
|
|
|
|
%)
|
|
|
|
|
</Text>
|
|
|
|
|
</Flex>
|
|
|
|
|
)}
|
2020-04-26 18:24:05 +10:00
|
|
|
</Flex>
|
2020-04-26 19:04:18 +10:00
|
|
|
</Modal>
|
2020-10-10 15:32:59 +11:00
|
|
|
<ConfirmModal
|
2020-04-26 19:04:18 +10:00
|
|
|
isOpen={isDeleteModalOpen}
|
|
|
|
|
onRequestClose={() => setIsDeleteModalOpen(false)}
|
2020-10-10 15:32:59 +11:00
|
|
|
onConfirm={handleEraseAllData}
|
|
|
|
|
label="Erase All Content?"
|
|
|
|
|
description="This will remove all data including saved maps and tokens."
|
|
|
|
|
confirmText="Erase"
|
|
|
|
|
/>
|
2021-01-26 21:49:44 +11:00
|
|
|
<ImportExportModal
|
|
|
|
|
isOpen={isImportExportModalOpen}
|
|
|
|
|
onRequestClose={() => setIsImportExportModalOpen(false)}
|
|
|
|
|
/>
|
2020-04-26 19:04:18 +10:00
|
|
|
</>
|
2020-04-26 18:24:05 +10:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default SettingsModal;
|