import React, { useState, useEffect } from "react"; import { Label, Flex, Button, useColorMode, Checkbox, Divider, Text, } from "theme-ui"; import prettyBytes from "pretty-bytes"; import Modal from "../components/Modal"; import Slider from "../components/Slider"; import { useAuth } from "../contexts/AuthContext"; import { useDatabase } from "../contexts/DatabaseContext"; import useSetting from "../hooks/useSetting"; import ConfirmModal from "./ConfirmModal"; import ImportExportModal from "./ImportExportModal"; function SettingsModal({ isOpen, onRequestClose }) { const { database, databaseStatus } = useDatabase(); const { userId } = useAuth(); const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false); 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]); async function handleEraseAllData() { localStorage.clear(); await database.delete(); window.location.reload(); } 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); } } window.location.reload(); } const [colorMode, setColorMode] = useColorMode(); return ( <> {storageEstimate && ( Storage Used: {prettyBytes(storageEstimate.usage)} of{" "} {prettyBytes(storageEstimate.quota)} ( {Math.round( (storageEstimate.usage / Math.max(storageEstimate.quota, 1)) * 100 )} %) )} setIsDeleteModalOpen(false)} onConfirm={handleEraseAllData} label="Erase All Content?" description="This will remove all data including saved maps and tokens." confirmText="Erase" /> setIsImportExportModalOpen(false)} /> ); } export default SettingsModal;