Files
grungnet/src/modals/SettingsModal.js

171 lines
5.1 KiB
JavaScript
Raw Normal View History

import React, { useState, useEffect } from "react";
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
import { useAuth } from "../contexts/AuthContext";
import { useDatabase } from "../contexts/DatabaseContext";
2020-04-26 18:24:05 +10:00
import useSetting from "../hooks/useSetting";
2020-09-08 15:06:15 +10:00
import ConfirmModal from "./ConfirmModal";
import ImportExportModal from "./ImportExportModal";
2020-04-26 18:24:05 +10:00
function SettingsModal({ isOpen, onRequestClose }) {
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");
const [storageEstimate, setStorageEstimate] = useState();
const [isImportExportModalOpen, setIsImportExportModalOpen] = useState(false);
useEffect(() => {
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);
}
if (isOpen && navigator.storage) {
estimateStorage();
}
}, [isOpen]);
2020-04-26 18:24:05 +10:00
async function handleEraseAllData() {
localStorage.clear();
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() {
// Clear saved settings
localStorage.clear();
// Clear map cache
await database.table("maps").where("owner").notEqual(userId).delete();
// 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>
<Flex py={2}>
<Button
sx={{ flexGrow: 1 }}
onClick={() => setIsImportExportModalOpen(true)}
disabled={databaseStatus !== "loaded"}
>
Import / Export Database
</Button>
</Flex>
{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>
<ConfirmModal
2020-04-26 19:04:18 +10:00
isOpen={isDeleteModalOpen}
onRequestClose={() => setIsDeleteModalOpen(false)}
onConfirm={handleEraseAllData}
label="Erase All Content?"
description="This will remove all data including saved maps and tokens."
confirmText="Erase"
/>
<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;