fix: break the import cycle that put useCalculators in the TDZ

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>
This commit is contained in:
Roland 2026-08-02 12:23:10 -06:00
parent b28fd400d7
commit 32f73cc3c9
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
9 changed files with 192 additions and 25 deletions

View file

@ -9,6 +9,7 @@ import {
Textarea as MTextarea,
TextInput as MTextInput,
} from '@mantine/core';
import {Label, LabelProps} from './Feedback';
/*
* Form controls.
@ -165,6 +166,55 @@ export const ActionInput = forwardRef<HTMLInputElement, ActionInputProps>((
});
ActionInput.displayName = 'ActionInput';
export interface ColoredInputProps
extends Omit<React.ComponentProps<typeof MTextInput>, 'color' | 'style'> {
/** Shown as a Label beside the field. Omit it and this is a plain TextInput. */
label?: React.ReactNode;
/*
* Applied to the ROW, not the field — every call site uses it for width or margins
* around the pair. Narrowed from Mantine's own `style`, which is a union that also
* accepts a function and an array and so cannot be spread into an object literal.
*/
style?: React.CSSProperties;
/*
* A token colour name for the label, and the SAME union Label accepts rather than a
* loose `string`: the point of the union is that a theme must have a token by that
* name, and widening it here would let a call site name a colour that resolves to
* nothing in every theme.
*/
color?: LabelProps['color'];
labelPosition?: 'left' | 'right';
/** Fill the row rather than sizing to content. */
fluid?: boolean;
}
/**
* A text field with a Label welded to one side — the calculators' unit markers ("ft", "Ω",
* "gal"), which is where all ten of them get their shape.
*
* It lives here rather than beside the calculators because it used to live in `Apps.js`,
* the module that ROUTES to the calculators. Five calculators imported it, so
* `Calculators.js` -> a calculator -> `Apps.js` -> `Calculators.js` was a cycle around the
* hub, and `useCalculators` is a `const`: webpack compiles that to a getter that throws
* rather than returning undefined when read too early. The dashboard threw on arrival for
* a whole editing session. Nothing in `components/ui` imports from `components/`, so a
* control kept here cannot close a loop like that again.
*/
export function ColoredInput({
name, value, label, color, labelPosition = 'left', fluid, style, ...props
}: ColoredInputProps) {
const labelNode = label ? <Label color={color || 'grey'}>{label}</Label> : null;
return <div style={{
display: 'flex', alignItems: 'stretch', gap: 6,
width: fluid ? '100%' : undefined, ...style,
}}>
{labelNode && labelPosition === 'left' && labelNode}
<MTextInput name={name} value={value} style={{flex: fluid ? 1 : undefined}} {...props}/>
{labelNode && labelPosition !== 'left' && labelNode}
</div>
}
export {
MTextInput as TextInput,
MTextarea as Textarea,