Redesign the Storage Monitor as a storage-composition breakdown (snippets · datasets · app)

This commit is contained in:
2026-06-10 01:32:22 +03:00
parent e7de0cbb8a
commit 7599acd25e
12 changed files with 438 additions and 481 deletions
+13 -30
View File
@@ -1,39 +1,22 @@
/**
* Storage estimate adapter (spec §02 → Storage Monitor; docs/architecture/02 §6).
* Storage origin-usage adapter (spec §02 → Storage Monitor; docs/architecture/02 §6).
*
* This is the ONLY module that touches the browser's Storage Manager API
* (`navigator.storage.estimate()`). It feature-detects the API, then defers all
* presentation math to the portable core (`src/core/storage-estimate.ts`). When
* the API is unavailable — older browsers, insecure contexts — it returns an
* unavailable summary rather than throwing, so callers get a total function.
* The ONLY module that touches the Storage Manager API. We read `usage` — the bytes
* actually stored for the origin, which is reliable — and deliberately ignore
* `quota`, a padded, browser-decided approximation that is not a real free-space
* figure (web.dev → storage-for-the-web). Snippet/dataset bytes are measured in the
* component from our own stores; this just supplies the whole-origin total so the
* "App" remainder can be derived. Returns undefined when the API is absent or the
* call rejects; never throws.
*/
import { summarizeStorage, type StorageSummary } from '../../core/storage-estimate';
/** The unavailable summary, returned when the Storage Manager API is absent. */
const UNAVAILABLE: StorageSummary = {
usedBytes: 0,
quotaBytes: 0,
fraction: 0,
level: 'ok',
available: false,
};
/**
* Read the browser's storage estimate and summarize it. Returns an unavailable
* summary when `navigator.storage.estimate` is missing or the call rejects;
* never throws. All math lives in `summarizeStorage`.
*/
export async function readStorageEstimate(): Promise<StorageSummary> {
export async function readOriginUsage(): Promise<number | undefined> {
const storage = typeof navigator !== 'undefined' ? navigator.storage : undefined;
if (!storage || typeof storage.estimate !== 'function') {
return UNAVAILABLE;
}
if (!storage || typeof storage.estimate !== 'function') return undefined;
try {
const { usage, quota } = await storage.estimate();
return summarizeStorage({ usage, quota });
const { usage } = await storage.estimate();
return typeof usage === 'number' ? usage : undefined;
} catch {
return UNAVAILABLE;
return undefined;
}
}