Skip to content

Commit

Permalink
Show used storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
soup-bowl committed Jul 6, 2022
1 parent 772078b commit cefa036
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
### Added
- Strip URL smells from an input to the DNS inspector.
- Show used local storage in about page.

### Fixed
- Crypto password box causing mobile display issues.
Expand Down
26 changes: 26 additions & 0 deletions src/pages/help.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,27 @@ export function HelpPage() {
);
}

interface storage {
quota: number;
usage: number;
}

// https://stackoverflow.com/a/35696506
function formatBytes(bytes: number, decimals: number = 2) {
if (bytes === 0) return '0 Bytes';

const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

const i = Math.floor(Math.log(bytes) / Math.log(k));

return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}

export function AboutPage() {
const [apiVersion, setApiVersion] = useState('');
const [storageInfo, setStorageInfo] = useState<storage>({quota: 0, usage: 0});

useEffect(() => {
agent.Details.stats().then(response => {
Expand All @@ -49,6 +68,10 @@ export function AboutPage() {
.catch((err: any) => {
setApiVersion('Comm error');
});

navigator.storage.estimate().then(({usage, quota}) => {
setStorageInfo({ usage: usage ?? 0, quota: quota ?? 0 });
});
}, []);

return(
Expand All @@ -62,6 +85,9 @@ export function AboutPage() {
<Stack my={2}>
<Typography>App version: <Box component="span" fontWeight='700'>{process.env.REACT_APP_VERSION}</Box></Typography>
<Typography>API version: <Box component="span" fontWeight='700'>{apiVersion}</Box></Typography>
<Typography>
Using <Box component="span" fontWeight='700'>{formatBytes(storageInfo.usage)}</Box> of&nbsp;
<Box component="span" fontWeight='700'>{formatBytes(storageInfo.quota)}</Box> available local storage.</Typography>
</Stack>
<Button href="https://github.com/soup-bowl/whatsth.is" variant="outlined"><GitHubIcon />&nbsp;Source Code</Button>
</Box>
Expand Down

0 comments on commit cefa036

Please sign in to comment.