From 42b62c363117c85486405135835ba56bbfc235c0 Mon Sep 17 00:00:00 2001 From: Qijun Date: Thu, 1 Oct 2026 22:55:55 -0700 Subject: [PATCH 1/4] feat: show what a project keeps in browser storage Project info in the project menu lists the open project's IndexedDB database, the record count of every table, the library size against the 1 GiB limit, and the browser's origin-wide usage and persistence. --- .../web/src/data/storage-summary.test.ts | 34 +++++++ ui-packages/web/src/data/storage-summary.ts | 49 +++++++++++ .../features/projects/project-info-dialog.tsx | 88 +++++++++++++++++++ .../features/projects/project-switcher.tsx | 14 +++ .../src/features/projects/style.module.scss | 35 ++++++++ 5 files changed, 220 insertions(+) create mode 100644 ui-packages/web/src/data/storage-summary.test.ts create mode 100644 ui-packages/web/src/data/storage-summary.ts create mode 100644 ui-packages/web/src/features/projects/project-info-dialog.tsx diff --git a/ui-packages/web/src/data/storage-summary.test.ts b/ui-packages/web/src/data/storage-summary.test.ts new file mode 100644 index 0000000..1986726 --- /dev/null +++ b/ui-packages/web/src/data/storage-summary.test.ts @@ -0,0 +1,34 @@ +import { describe, expect, it } from 'vitest' +import { emptyConversationDraft } from '../core/conversations' +import { rootSources } from '../core/files' +import { saveStoredConversationDraft } from './conversation-store' +import { importStoredFiles, listStoredFiles } from './file-store' +import { readStorageSummary } from './storage-summary' + +describe('storage summary', () => { + it('counts every table of the open project and sums the stored file bytes', async () => { + await importStoredFiles( + rootSources([new File(['# Notes'], 'Notes.md', { type: 'text/markdown' })]), + 'keep', + ) + await saveStoredConversationDraft({ + id: 'conversation', + title: null, + draft: emptyConversationDraft(), + createdAt: 1, + lastActiveAt: 1, + }) + const files = await listStoredFiles() + + const summary = await readStorageSummary() + + expect(Object.fromEntries(summary.stores.map(store => [store.name, store.records]))).toEqual({ + contents: files.length, + conversations: 1, + files: files.length, + folderExports: 0, + messages: 0, + }) + expect(summary.fileBytes).toBe(files.reduce((total, file) => total + file.size, 0)) + }) +}) diff --git a/ui-packages/web/src/data/storage-summary.ts b/ui-packages/web/src/data/storage-summary.ts new file mode 100644 index 0000000..8dad588 --- /dev/null +++ b/ui-packages/web/src/data/storage-summary.ts @@ -0,0 +1,49 @@ +import { listStoredFiles } from './file-store' +import { openWorkspaceDatabase } from './workspace-database' + +export type StoreSummary = { name: string; records: number } + +export type BrowserStorage = { usage: number; quota: number; persisted: boolean } + +export type StorageSummary = { + databaseName: string + stores: readonly StoreSummary[] + /** The same sum the library limit checks: every stored file, attachments included. */ + fileBytes: number + browser: BrowserStorage | null +} + +// Origin-wide, so it covers every project in this browser, not just the open one. +const readBrowserStorage = async (): Promise => { + if (!navigator.storage?.estimate || !navigator.storage.persisted) return null + try { + const [{ usage = 0, quota = 0 }, persisted] = await Promise.all([ + navigator.storage.estimate(), + navigator.storage.persisted(), + ]) + return { usage, quota, persisted } + } catch (error) { + console.error('Unable to read browser storage usage', error) + return null + } +} + +export const readStorageSummary = async (): Promise => { + const database = await openWorkspaceDatabase() + const [stores, files, browser] = await Promise.all([ + Promise.all( + Array.from(database.objectStoreNames, async name => ({ + name, + records: await database.count(name), + })), + ), + listStoredFiles(), + readBrowserStorage(), + ]) + return { + databaseName: database.name, + stores, + fileBytes: files.reduce((total, file) => total + file.size, 0), + browser, + } +} diff --git a/ui-packages/web/src/features/projects/project-info-dialog.tsx b/ui-packages/web/src/features/projects/project-info-dialog.tsx new file mode 100644 index 0000000..d933fac --- /dev/null +++ b/ui-packages/web/src/features/projects/project-info-dialog.tsx @@ -0,0 +1,88 @@ +import { useEffect, useState } from 'react' +import { ConfirmationDialog as Modal } from '../../components/confirmation-dialog' +import { formatBytes } from '../../core/files' +import type { Project } from '../../core/projects' +import { + type BrowserStorage, + readStorageSummary, + type StorageSummary, +} from '../../data/storage-summary' +import styles from './style.module.scss' + +const percent = new Intl.NumberFormat('en-US', { style: 'percent', maximumFractionDigits: 1 }) +// A fresh library is a sliver of the quota; rounding it to 0% would read as nothing stored. +const share = (ratio: number) => (ratio < 0.001 ? 'under 0.1%' : percent.format(ratio)) + +const BrowserStorageNote = ({ usage, quota, persisted }: BrowserStorage) => ( +

+ This browser, all projects: {formatBytes(usage)} used + {quota > 0 && `, ${share(usage / quota)} of what it allows`}.{' '} + {persisted + ? 'Storage is persistent.' + : 'Storage is not persistent; the browser may clear it under disk pressure.'} +

+) + +export const ProjectInfoDialog = ({ + project, + onClose, +}: { + project: Project + onClose: () => void +}) => { + const [summary, setSummary] = useState(null) + const [failed, setFailed] = useState(false) + useEffect(() => { + let current = true + readStorageSummary().then( + result => { + if (current) setSummary(result) + }, + cause => { + console.error('Unable to read project storage', cause) + if (current) setFailed(true) + }, + ) + return () => { + current = false + } + }, []) + return ( + +

{project.name}

+ {failed &&

Unable to read this project's storage.

} + {summary && ( + <> +
+
Database
+
{summary.databaseName}
+
Library
+
{formatBytes(summary.fileBytes)} of 1 GiB
+
+ + + + + + + + + {summary.stores.map(store => ( + + + + + ))} + +
TableRecords
{store.name}{store.records.toLocaleString('en-US')}
+ {summary.browser && } + + )} +
+ +
+
+ ) +} diff --git a/ui-packages/web/src/features/projects/project-switcher.tsx b/ui-packages/web/src/features/projects/project-switcher.tsx index 20a41f2..9807383 100644 --- a/ui-packages/web/src/features/projects/project-switcher.tsx +++ b/ui-packages/web/src/features/projects/project-switcher.tsx @@ -6,6 +6,7 @@ import { listProjects, renameProject } from '../../data/project-store' import type { FileExportController } from '../resources/use-file-export' import { DeleteProjectDialog } from './delete-project-dialog' import { NewProjectForm } from './new-project-form' +import { ProjectInfoDialog } from './project-info-dialog' import { ProjectLink } from './project-link' import styles from './style.module.scss' @@ -72,6 +73,7 @@ export const ProjectSwitcher = (props: ProjectSwitcherProps) => { const [projects, setProjects] = useState(null) const [listError, setListError] = useState(null) const [deleting, setDeleting] = useState(false) + const [showingInfo, setShowingInfo] = useState(false) const others = (projects ?? []).filter(project => project.id !== current?.id) const loadProjects = () => { @@ -168,6 +170,15 @@ export const ProjectSwitcher = (props: ProjectSwitcherProps) => { + {current && ( <> +
+