Skip to content

Commit 135b317

Browse files
committed
Show storage usage.
1 parent 561903a commit 135b317

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

frontend/src/modal/Settings.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from "react"
1+
import { useEffect, useState } from "react"
22
import {
33
IonModal,
44
IonHeader,
@@ -12,9 +12,11 @@ import {
1212
IonNote,
1313
IonList,
1414
IonInputPasswordToggle,
15+
IonLabel,
1516
} from "@ionic/react"
1617
import { useQueryClient } from "@tanstack/react-query"
1718
import { useAuth } from "../hooks"
19+
import { formatBytes } from "../utils"
1820

1921
interface Props {
2022
open: boolean
@@ -27,6 +29,17 @@ const Settings: React.FC<Props> = ({ open, onClose, onSave }) => {
2729
const [{ username, token }, saveAuth, clearAuth] = useAuth()
2830
const [newUsername, setNewUsername] = useState<string>(username ?? "")
2931
const [newPassword, setNewPassword] = useState<string>(token ?? "")
32+
const [storageInfo, setStorageInfo] = useState<{ usage: string; quota: string } | undefined>()
33+
34+
useEffect(() => {
35+
if ("storage" in navigator && "estimate" in navigator.storage) {
36+
navigator.storage.estimate().then(({ usage, quota }) => {
37+
if (usage && quota) {
38+
setStorageInfo({ usage: formatBytes(usage), quota: formatBytes(quota) })
39+
}
40+
})
41+
}
42+
}, [])
3043

3144
const handleSave = () => {
3245
saveAuth(newUsername, newPassword)
@@ -74,6 +87,14 @@ const Settings: React.FC<Props> = ({ open, onClose, onSave }) => {
7487
<a href="https://www.discogs.com/settings/developers">visit the Developer page</a> and copy your
7588
token, or click Generate if you do not have one.
7689
</IonNote>
90+
<IonList inset={true}>
91+
<IonItem>
92+
<IonLabel>Storage Used</IonLabel>
93+
<IonLabel slot="end">
94+
{storageInfo?.usage ?? "Unknown"} of {storageInfo?.quota ?? "Unknown"}
95+
</IonLabel>
96+
</IonItem>
97+
</IonList>
7798
</IonContent>
7899
</IonModal>
79100
)

frontend/src/utils/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
11
export { splitRecordsByYear, splitRecordsByLabel, splitRecordsByArtist } from "./collectionSort"
22
export { findLocalImageById } from "./search"
3+
4+
/**
5+
* Formats a number of bytes as a human-readable string, with the size unit automatically selected based on the size of the number.
6+
*
7+
* Copied from https://github.com/soup-bowl/libwhatsthis/blob/main/src/string/index.ts
8+
*
9+
* @param bytes - The number of bytes to format.
10+
* @param decimals - The number of decimal places to include in the formatted string. Defaults to 2.
11+
* @returns A human-readable string representing the given number of bytes.
12+
*
13+
* @example
14+
* const fileSize = formatBytes(1024);
15+
* console.log(fileSize); // '1.00 KB'
16+
*/
17+
export const formatBytes = (bytes: number, decimals: number = 2): string => {
18+
if (bytes === 0) return "0 Bytes"
19+
20+
const k = 1024
21+
const dm = decimals < 0 ? 0 : decimals
22+
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
23+
24+
const i = Math.floor(Math.log(bytes) / Math.log(k))
25+
26+
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i]
27+
}

0 commit comments

Comments
 (0)