import React, { useCallback, useEffect, useState } from 'react'; import { Badge, Box, Button, Text } from 'folds'; import { SequenceCard } from '../../../components/sequence-card'; import { SequenceCardStyle } from '../styles.css'; import { SettingTile } from '../../../components/setting-tile'; import { bytesToSize } from '../../../utils/common'; import { percentOf, readStorageUsage, requestPersistentStorage, StorageUsage as Usage, storageUsageSupported, } from '../../../utils/storageUsage'; /** * [Gitea #120] One tile: how much this device stores for Lotus, whether the * browser has promised to keep it, and a way to ask. Hidden entirely when * `navigator.storage.estimate()` is unavailable rather than showing zeros. */ export function StorageUsage() { const [usage, setUsage] = useState(); const [requesting, setRequesting] = useState(false); const refresh = useCallback(() => { readStorageUsage() .then(setUsage) .catch(() => setUsage(undefined)); }, []); useEffect(() => { if (storageUsageSupported()) refresh(); }, [refresh]); if (!usage) return null; const handlePersist = async () => { setRequesting(true); await requestPersistentStorage(); setRequesting(false); refresh(); }; const breakdown: string[] = []; if (typeof usage.indexedDB === 'number') breakdown.push( `${bytesToSize(usage.indexedDB)} in IndexedDB (sync cache, encryption keys, search index)`, ); if (typeof usage.caches === 'number') breakdown.push(`${bytesToSize(usage.caches)} offline app files`); return ( Storage {percentOf(usage.usage, usage.quota)}% of the {bytesToSize(usage.quota)} the browser allows this site. {breakdown.length > 0 && ` ${breakdown.join(' · ')}.`} Images and videos you have viewed are kept in the browser's own cache, which it manages and empties itself; they are not counted here. Encryption keys are never cleared from this page — losing them makes older encrypted messages unreadable on this device. Cached search results can be cleared from Message Search. } after={ usage.persisted === undefined ? undefined : ( {usage.persisted ? 'Protected' : 'May be evicted'} {!usage.persisted && ( )} ) } /> ); }