Files
grungnet/src/modals/SettingsModal.js

120 lines
3.3 KiB
JavaScript
Raw Normal View History

2020-04-30 14:24:47 +10:00
import React, { useState, useContext } from "react";
2020-09-08 15:06:15 +10:00
import {
Label,
Flex,
Button,
useColorMode,
Checkbox,
Slider,
Divider,
} from "theme-ui";
2020-04-26 18:24:05 +10:00
import Modal from "../components/Modal";
2020-04-30 14:24:47 +10:00
import AuthContext from "../contexts/AuthContext";
import DatabaseContext from "../contexts/DatabaseContext";
2020-04-26 18:24:05 +10:00
2020-09-08 15:06:15 +10:00
import useSetting from "../helpers/useSetting";
import ConfirmModal from "./ConfirmModal";
2020-04-26 18:24:05 +10:00
function SettingsModal({ isOpen, onRequestClose }) {
const { database } = useContext(DatabaseContext);
2020-04-30 14:24:47 +10:00
const { userId } = useContext(AuthContext);
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-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))}
/>
</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>
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"
/>
2020-04-26 19:04:18 +10:00
</>
2020-04-26 18:24:05 +10:00
);
}
export default SettingsModal;