mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-10-26 19:01:39 -04:00
The dashboard threw on arrival from any other page:
ReferenceError: Cannot access 'useCalculators' before initialization
at Module.useCalculators
at DashboardCalculators
`Calculators.js` exports `useCalculators` as a `const` and imports the ten
calculators it renders. Five of those reached back up to `Apps.js` for
`ColoredInput`, and `Apps.js` imports `CalculatorsPage` from
`Calculators.js` -- so the hub was in a cycle with its own leaves:
Calculators.js -> calculators/ElectricalCalculators.js
-> Apps.js -> Calculators.js
Webpack compiles a `const` export to a live getter, and a getter read
before the module body reaches the declaration THROWS rather than
returning undefined. That is the whole mechanism behind this particular
error, and it names the const it could not reach.
`ColoredInput` is a text field with a Label welded to one edge -- the
calculators' unit markers. It has no business in the module that ROUTES
to the calculators, so it moved to `components/ui/Inputs.tsx`. Nothing in
`components/ui` imports from `components/`, so a control kept there cannot
close a loop like this again. Gallery entry added, per the standing rule.
`src/import-cycles.test.js` is the guard: a small import-graph walker,
scoped to hubs that export a `const` into their own subtree. It fails
with the cycle printed, and it self-checks against a known cycle so it
cannot pass by finding nothing. Verified red with the cycle planted back.
HONEST LIMIT: the guard is static because I could not reproduce the
RUNTIME error in a test. TDZ needs a module to be mid-evaluation when the
getter is read, and by the time any spec body runs every module has
finished; babel also papers over it, handing back undefined where webpack
throws. I tried an e2e that navigates to the dashboard and collects
window errors -- it passes with the cycle present, so it would have been a
test that cannot fail for the reason it claims, and I deleted it rather
than ship it. A react-refresh trigger did not reproduce it either. What
is checkable, and what is genuinely the defect, is the cycle.
1080 jest / 60 suites, 158 in ui-layout.cy.js, clean build.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
109 lines
3.8 KiB
JavaScript
109 lines
3.8 KiB
JavaScript
import React from 'react';
|
|
import {Link, Route, Routes} from "react-router";
|
|
import {
|
|
ErrorMessage,
|
|
humanFileSize,
|
|
humanNumber,
|
|
mimetypeColor,
|
|
PageContainer,
|
|
toLocaleString,
|
|
useTitle
|
|
} from "./Common";
|
|
import {Header, Loading, Panel, Statistic, StatisticGroup} from "./ui";
|
|
import {useStatistics} from "../hooks/customHooks";
|
|
import {CalculatorsPage} from "./Calculators";
|
|
|
|
function StatisticsPage() {
|
|
useTitle('Statistics');
|
|
|
|
const {statistics} = useStatistics();
|
|
|
|
if (statistics === undefined) {
|
|
return <>
|
|
<Header as='h1'>Statistics</Header>
|
|
<ErrorMessage>Failed to fetch statistics</ErrorMessage>
|
|
</>
|
|
}
|
|
|
|
if (statistics['global_statistics']) {
|
|
let {global_statistics, file_statistics} = statistics;
|
|
const {
|
|
archive_count,
|
|
audio_count,
|
|
ebook_count,
|
|
image_count,
|
|
pdf_count,
|
|
total_count,
|
|
video_count,
|
|
zip_count,
|
|
total_size,
|
|
tagged_files,
|
|
tagged_zims,
|
|
tags_count,
|
|
} = file_statistics;
|
|
const {db_size} = global_statistics;
|
|
return <>
|
|
<Header as='h1'>Statistics</Header>
|
|
<Header as='h2'>Files</Header>
|
|
<Panel>
|
|
<StatisticGroup>
|
|
<Statistic value={toLocaleString(total_count)} label='All Files'/>
|
|
<Statistic value={humanFileSize(total_size)} label='Total Size'/>
|
|
</StatisticGroup>
|
|
</Panel>
|
|
<Panel>
|
|
<StatisticGroup>
|
|
<Link to={'/videos/statistics'}>
|
|
<Statistic color={mimetypeColor('video/')} value={toLocaleString(video_count)}
|
|
label='Videos'/>
|
|
</Link>
|
|
<Statistic color={mimetypeColor('application/pdf')} value={toLocaleString(pdf_count)}
|
|
label='PDFs'/>
|
|
<Statistic color={mimetypeColor('application/epub')} value={toLocaleString(ebook_count)}
|
|
label='eBooks'/>
|
|
<Statistic color={mimetypeColor('text/html')} value={toLocaleString(archive_count)}
|
|
label='Archives'/>
|
|
<Statistic color={mimetypeColor('image/')} value={toLocaleString(image_count)}
|
|
label='Images'/>
|
|
</StatisticGroup>
|
|
</Panel>
|
|
<Panel>
|
|
<StatisticGroup>
|
|
<Statistic value={toLocaleString(zip_count)} label='ZIP'/>
|
|
<Statistic color={mimetypeColor('audio/')} value={toLocaleString(audio_count)} label='Audio'/>
|
|
</StatisticGroup>
|
|
</Panel>
|
|
|
|
<Header as='h2'>Tags</Header>
|
|
<Panel>
|
|
<StatisticGroup>
|
|
<Statistic value={humanNumber(tags_count)} label='Tags'/>
|
|
<Statistic value={humanNumber(tagged_files)} label='Tagged Files'/>
|
|
<Statistic value={humanNumber(tagged_zims)} label='Tagged Zims'/>
|
|
</StatisticGroup>
|
|
</Panel>
|
|
|
|
<Header as='h2'>Database</Header>
|
|
<Panel>
|
|
<StatisticGroup>
|
|
<Statistic value={humanFileSize(db_size)} label='Size'/>
|
|
</StatisticGroup>
|
|
</Panel>
|
|
</>;
|
|
}
|
|
|
|
return <>
|
|
<Header as='h1'>Statistics</Header>
|
|
<Panel><Loading/></Panel>
|
|
</>;
|
|
|
|
}
|
|
|
|
export function MoreRoute(props) {
|
|
return <PageContainer>
|
|
<Routes>
|
|
<Route path='calculators' element={<CalculatorsPage/>}/>
|
|
<Route path='statistics' exact element={<StatisticsPage/>}/>
|
|
</Routes>
|
|
</PageContainer>
|
|
}
|