Skip to content

Build your personal knowledge base with Trilium Notes

client/content manager/space usage: fix an issue (P2)

Adorian Dorancfb9fc5030ad
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.spec.tsx
index 3651fa1153..e10f2b627d 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.spec.tsx
@@ -18,10 +18,12 @@ interface CapturedDonutProps {
const mocks = vi.hoisted(() => ({
usage: undefined as unknown,
failed: false,
+ loading: false,
fetchedUrls: [] as string[],
donutProps: undefined as unknown,
openContextMenu: vi.fn(),
- contentChanged: vi.fn()
+ contentChanged: vi.fn(),
+ loadingChange: vi.fn()
}));
vi.mock("./context_menu", () => ({
@@ -31,7 +33,7 @@ vi.mock("./context_menu", () => ({
vi.mock("./use_space_usage_fetch", () => ({
useSpaceUsageFetch: (url: string) => {
mocks.fetchedUrls.push(url);
- return { data: mocks.usage, failed: mocks.failed };
+ return { data: mocks.usage, failed: mocks.failed, loading: mocks.loading };
}
}));
@@ -84,7 +86,15 @@ function BrowseHost() {
const [ path, setPath ] = useState([ "root" ]);
// The refresh token never changes here: navigation is what this spec is about.
- return <Browse path={path} onPathChange={setPath} refreshToken={0} onContentChanged={mocks.contentChanged} />;
+ return (
+ <Browse
+ path={path}
+ onPathChange={setPath}
+ refreshToken={0}
+ onContentChanged={mocks.contentChanged}
+ onLoadingChange={mocks.loadingChange}
+ />
+ );
}
function renderBrowse() {
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.tsx
index 67f2fa96d5..3aa4cf635b 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/browse.tsx
@@ -26,6 +26,11 @@ interface BrowseProps {
refreshToken: number;
/** Called once the menu deleted something, so the donut stops drawing what is no longer there. */
onContentChanged: ContentChangedHandler;
+ /**
+ * Reports whether this view is measuring, so the section can keep its refresh button out while
+ * it is — this view's reading is its own request, which the section cannot otherwise see.
+ */
+ onLoadingChange: (loading: boolean) => void;
}
/**
@@ -36,10 +41,17 @@ interface BrowseProps {
* The path is owned by the section rather than the view, so that "Show details" elsewhere in Space
* Usage can drop the user straight onto a note here.
*/
-export default function Browse({ path, onPathChange, refreshToken, onContentChanged }: BrowseProps) {
+export default function Browse({
+ path, onPathChange, refreshToken, onContentChanged, onLoadingChange
+}: BrowseProps) {
const noteId = path[path.length - 1];
- const { data: usage, failed } = useSpaceUsageFetch<SpaceUsageNoteResponse>(
+ const { data: usage, failed, loading } = useSpaceUsageFetch<SpaceUsageNoteResponse>(
`space-usage/note/${noteId}`, refreshToken);
+
+ useEffect(() => onLoadingChange(loading), [ loading, onLoadingChange ]);
+ // Leaving the view mid-measure would otherwise strand the section believing one is still under
+ // way, with its refresh button disabled for good.
+ useEffect(() => () => onLoadingChange(false), [ onLoadingChange ]);
const titles = useNoteTitles(path, usage);
const getTitle = useCallback((id: string) => titles.get(id) ?? id, [ titles ]);
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.spec.tsx
index db86900503..0d0f855482 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.spec.tsx
@@ -11,7 +11,8 @@ const mocks = vi.hoisted(() => ({
loading: false,
/** The token the section last asked each view to measure at. */
overviewToken: undefined as number | undefined,
- browseToken: undefined as number | undefined
+ browseToken: undefined as number | undefined,
+ reportBrowseLoading: undefined as ((loading: boolean) => void) | undefined
}));
vi.mock("./use_space_usage_fetch", () => ({
@@ -22,8 +23,12 @@ vi.mock("./use_space_usage_fetch", () => ({
}));
vi.mock("./browse", () => ({
- default: ({ refreshToken }: { refreshToken: number }) => {
+ default: ({ refreshToken, onLoadingChange }: {
+ refreshToken: number,
+ onLoadingChange: (loading: boolean) => void
+ }) => {
mocks.browseToken = refreshToken;
+ mocks.reportBrowseLoading = onLoadingChange;
return <div className="browse-stub" />;
}
}));
@@ -79,6 +84,7 @@ afterEach(() => {
mocks.loading = false;
mocks.overviewToken = undefined;
mocks.browseToken = undefined;
+ mocks.reportBrowseLoading = undefined;
});
describe("SpaceUsage section", () => {
@@ -174,4 +180,25 @@ describe("SpaceUsage section", () => {
expect(renderSection().querySelector<HTMLButtonElement>(".space-usage-refresh")?.disabled).toBe(true);
});
+
+ it("keeps it out for Browse's own reading too, which it cannot see otherwise", async () => {
+ mocks.overview = OVERVIEW;
+ const probe = renderSection();
+ const refresh = () => probe.querySelector<HTMLButtonElement>(".space-usage-refresh");
+
+ [ ...probe.querySelectorAll<HTMLButtonElement>(".content-manager-view-choice button") ][1]
+ ?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
+ await flushRender();
+ expect(refresh()?.disabled).toBe(false);
+
+ // Browse measures its note through a request of its own; a second press meanwhile would
+ // start another scan of the whole database alongside the one already running.
+ mocks.reportBrowseLoading?.(true);
+ await flushRender();
+ expect(refresh()?.disabled).toBe(true);
+
+ mocks.reportBrowseLoading?.(false);
+ await flushRender();
+ expect(refresh()?.disabled).toBe(false);
+ });
});
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.tsx
index d3f352044a..7e86834987 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/index.tsx
@@ -28,6 +28,9 @@ export default function SpaceUsage({ sectionSwitcher }: ContentManagerSectionPro
const [ refreshToken, setRefreshToken ] = useState(0);
// Stable, so handing it to the views costs them nothing on re-render.
const refresh = useCallback(() => setRefreshToken((token) => token + 1), []);
+ // Browse measures its own note through a separate request, which this component would not
+ // otherwise know about; without it the button would free up while that reading was still running.
+ const [ browseLoading, setBrowseLoading ] = useState(false);
// Browse's position lives here so that "Show details", offered on any note either view draws,
// can land the user on that note — switching the view along the way when it comes from Overview.
const [ browsePath, setBrowsePath ] = useState([ "root" ]);
@@ -57,7 +60,7 @@ export default function SpaceUsage({ sectionSwitcher }: ContentManagerSectionPro
className="space-usage-refresh"
icon="bx bx-refresh"
text={t("space_usage.refresh")}
- disabled={loading}
+ disabled={loading || browseLoading}
onClick={refresh}
/>
</div>
@@ -69,6 +72,7 @@ export default function SpaceUsage({ sectionSwitcher }: ContentManagerSectionPro
onPathChange={setBrowsePath}
refreshToken={refreshToken}
onContentChanged={refresh}
+ onLoadingChange={setBrowseLoading}
/>
)}
{view === "overview" && (overview
diff --git a/packages/trilium-core/src/services/space_usage.ts b/packages/trilium-core/src/services/space_usage.ts
index 63427e808a..e6d066edf8 100644
--- a/packages/trilium-core/src/services/space_usage.ts
+++ b/packages/trilium-core/src/services/space_usage.ts
@@ -457,10 +457,16 @@ function withBlobUsage<T>(collect: () => T): T {
//
// The `CASE` reads top-down, so the strongest claim on a blob wins and each reference set
// is built once for the whole scan. Assigning the kinds here rather than in follow-up
- // `UPDATE`s also keeps the pass to a single write — and `execute()` refuses a statement
- // starting with `UPDATE` on a read-only database, which would have left every blob
- // unclassified and quietly reported zeros.
+ // `UPDATE`s also keeps the pass to a single write.
+ //
+ // The leading comment is load-bearing: `execute()` drops any statement *starting with*
+ // `INSERT`/`UPDATE`/`DELETE` when the database is read-only (`[General] readOnly`), and
+ // dropping this one would leave the table empty and every figure silently zero. Writing to
+ // a temp table is legitimate there — SQLite keeps the temp database separate and writable
+ // even when the main one is opened read-only — so the statement must not look like a write
+ // to the main database.
const measured = timed("measuring the blobs", () => sql.execute(`
+ /* Into the temp database, which stays writable on a read-only connection. */
INSERT INTO blob_usage (blobId, size, kind)
SELECT blobs.blobId,
COALESCE(octet_length(blobs.content), 0),