add storage quota display

This commit is contained in:
marlon-erler
2026-09-24 23:27:54 +02:00
parent a63488d62a
commit c5db5290ac
11 changed files with 500 additions and 326 deletions
Binary file not shown.
+34 -3
View File
@@ -1,6 +1,5 @@
// cleanup: Phase A
import { localeCompare, parseValidObject, stringify } from "../Utility/utility";
import { bytesToMB, localeCompare, parseValidObject, stringify } from "../Utility/utility";
import { DATA_VERSION, ValidObject } from "../Utility/typeSafety";
export const PATH_COMPONENT_SEPARATOR = "\\";
@@ -144,7 +143,7 @@ export default class StorageModel {
return object;
};
// cleaning
// cleaning & usage
readonly removeJunk = (): void => {
this.recurse([], (path: string[]) => {
if (path[0] == DATA_VERSION) return;
@@ -152,6 +151,38 @@ export default class StorageModel {
});
};
readonly determineCapacity = (): number => {
const KEY = "storage-capacity-check";
let chunk = "0";
for (let i = 0; i < 1024*256; i++) {
chunk += "0";
}
localStorage.setItem(KEY, "");
while (true) {
try {
const current = localStorage.getItem(KEY);
console.log(current);
localStorage.setItem(KEY, current + chunk);
} catch (e) {
const capacity = this.calculateUsage();
localStorage.removeItem(KEY);
return capacity;
}
}
}
readonly calculateUsage = (): number => {
let data = JSON.stringify(localStorage);
const encoder = new TextEncoder();
const encoded = encoder.encode(data);
const bytes = encoded.byteLength;
const mb = bytesToMB(bytes);
const rounded = Math.round(mb * 100)/100;
return rounded;
}
// tree
readonly initializeTree = (): void => {
console.log("initializing tree");
+4
View File
@@ -177,6 +177,10 @@ export function getLocalStorageItemAndClear(key: string): string | null {
return value;
}
export function bytesToMB(bytes: number): number {
return bytes / (1024*1024);
}
// string & parsing
export function stringify(data: any): string {
return JSON.stringify(data, null, 4);