Skip to content

refactor(react): build <PivotReport> from hooks and small components - #734

Merged
eizedev merged 6 commits into
mainfrom
refactor/report-hooks
Nov 22, 2026
Merged

eizedev merged 6 commits into
mainfrom
refactor/report-hooks

Conversation

@eizedev

@eizedev eizedev commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

TL;DR: Report in PivotReport.jsx was one 1,270-line function. It now composes custom hooks and small components, one per concern. Props, events, markup, test ids and behaviour stay the same. The first load stays within its 181 KiB budget.

Growth numbers (node scripts/growth.js)

PivotReport.jsx Report Before After
Lines (without blanks and comments) 1270 111 (under the 120 limit, out of the baseline)
Complexity 149 34 (still on the ratchet)

Other changes to the baseline:

  • The baseline is lowered with --update.
  • No new offender. The largest new function is watchSize at 75 lines.
  • Five unused exports of PivotReport.jsx are gone from the unused list: sizeOf, compactAt, zonesStart, COMPACT_HEIGHT and COMPACT_HEIGHT_NARROW.

Report's complexity stays at 34. It is the composition root, and its conditional render branches (a drawer, a drill-down, a filter, the import UI, the root's attributes) are the report's own.

New structure

File Responsibility
PivotReport.jsx PivotReport (skeleton, deferred texts) and Report, which composes the hooks and parts below
use-host-props.js Props compared by value (labels, limits, fieldDefaults, chart options), and the console warnings about them
use-report-size.js Width and height classes from the report's own box, the least height, Enlarge and Full screen
use-drawer.js The field list as a drawer on a narrow report
use-zone-fold.js Zones folded or open: the user's choice, the host's zones, the height
use-shown-data.js The host's data encoded, or a loaded file's; data steps; saved keys; useKeepData keeps a file over a new mount
use-import.js The import UI's state (useImport) and the object it works on (useImportApi)
use-report-labels.js The texts with the fields' display names and the view's own names
use-pivot-result.js What the result shows; the pivot, grid, chart plan and series; the first error
use-fields.js What the parts ask about a field: type, names, description, defaults, zones
use-steps.js commit(), Undo, Redo, Reset and their status notes
use-unused-notes.js The warning above the result: what a loaded or host view left out
use-view-loading.js Views loaded from outside: file, link, saved view, New; a view waiting for its sheet
use-share-link.js The tab's stored view and a shared link at start and on hashchange; the Share menu's context
link-open.js Reading a link's view against the data, loaded with pivotkit-core/link in the link chunk
use-dismissed.js Tips dismissed, explanations, the field list's rail, per storageKey
use-tips.js What the user did, for the tips (useActivity); the tips shown and their actions
use-menus.js The menus' module, loaded after the first render; the help texts
use-saved-views.js Saved views (host and browser store), the Views menu's actions, unsaved changes
use-drill.js Rows behind a cell or a chart point; the point menu
use-placing.js Placing fields: field list, "+ Add", drag and drop, header sort, the filter popover
use-chart-state.js The drawn chart's instance, whether it is ready, its reference-line checks, legend clicks
result-steps.js The steps of the tabs' controls (display, chart, totals, expand and collapse) and Swap
report-keys.js Ctrl/Cmd+Z and Y, and Escape for Enlarge
report-toolbar.jsx The toolbar wired to the hooks
above-result.jsx The link check, the unused warning, the tips, the cells' hint, the enlarged summary
result-area.jsx The tabs, the result section (table, chart, both, skeleton, error, start screen) and the status line
status-line.jsx, empty-state.jsx The status line; the start screen
FieldSlot (fields.jsx), DragChip (zones.jsx) The field list's slot with the scrim; the chip under the pointer while dragging
warn-once.js, measure.js Console warnings said once (export-scope.js uses it too); performance measures

unusedNotes moved to unused-keep.js, and layerOpen and useFormPrefix to dom.js. The hooks follow the repository's use-<concern>.js file naming.

First load and lazy chunks (npm run size -w pivotkit-element, gzip, local zlib)

Group Main This PR Budget
initial 178.6 (13 files) 180.2 (12 files) 181
link 7.2 7.3 7.5
menus 58.7 58.7 59.1
export 12.3 12.3 12.6
import 21.8 21.8 22
report 23.6 23.6 24
chart 258.0 258.0 259.5
texts, unused, template, lang, tips 8.3, 5.8, 2.5, 6.0, 1.6 unchanged

CI measured main's initial at 179.0, so it should measure this PR at about 180.6.

How the first load got back under budget:

  • The hook boundaries cost about 2.6 KiB gzip. Property names of options and results cannot be minified.
  • link-open.js takes reading a link's view (decode, normalize, the check for other data) out of the first load, into the chunk that loadLink() loads anyway. Measured gain: about 1 KiB.
  • linkNow() and loadLink() return that module. It re-exports pivotkit-core/link.
  • Opening the view stays in use-share-link.js. With it in the link chunk too, CI measured the link group just over its 7.5 KiB budget.

Behaviour

  • Effect order shifts only where nothing reads the other effect's result:
    • The prop warnings now run first.
    • The size layout effect now runs before the stored-view and link layout effect.
    • runAfterSheet now runs before the import UI's api ref is refreshed.
  • New keeps a dismissed warning dismissed, as before.
  • Tests:
    • All existing tests pass unchanged: unit, element, repository checks, and the browser tests in Chromium with --workers=3.
    • No test pinned internals.
    • New unit tests cover unusedNotes, report-keys.js and the two copies of the data steps.

What does not fit

  • NO_DATA_STEPS and withoutFuture exist twice: in data-history.js (import chunk) and in use-shown-data.js (first load). A module shared by both would be a file of its own in the first load. The duplication existed before as well. test/data-steps.test.js keeps the two the same.
  • Report's complexity stays at 34 (see above).
  • A link opened at mount when the link module is already loaded may throw: latest.current.loaded is still null in that layout effect. This is unchanged from main and unverified. Issue to follow.

Checked for the new state:

  • packages/pivotkit-react/AGENTS.md ("Where things go", the deferred.js rule)
  • The header of PivotReport.jsx
  • Comments pointing at PivotReport in data-keep.js, share-actions.js, drill-table.js, toolbar-panels.jsx, names.js, notices.js, tips.js, dom.js and use-big-result.js
  • docs/reference/react.md (COMPACT_HEIGHT)
  • The CHANGELOG

🤖 Generated with Claude Code

eizedev and others added 6 commits November 22, 2026 18:34
warnOnce, measured, the first-load part of the data steps, unusedNotes
(with a test), layerOpen and useFormPrefix move out of PivotReport.jsx
into modules the new hooks can import.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Report kept its state, effects and callbacks in one 1,270-line function.
Each concern now lives in a hook of its own file (use-*.js) and the
stand-alone parts of the markup in components; Report composes them.
Props, events, markup and behaviour stay the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… baseline

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The hooks cost the first load about 2.6 KiB gzip in property names that
cannot be minified. Opening a link's view (link-open.js) now loads with
pivotkit-core/link, which a link needs anyway, so the first load is back
under its budget. data-steps.js is gone again: as a module shared with the
import UI it was a file of its own in the first load.

Also from the review: New keeps a dismissed warning dismissed, as before;
export-scope.js uses warn-once.js; the header lists the hooks by concern;
a test for the report's keys.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With it in the link chunk, CI measured the link group just over its
budget. Reading the link (decode, normalize, the check for other data)
stays in link-open.js; opening the view is use-share-link.js's again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@eizedev
eizedev merged commit cc79044 into main Nov 22, 2026
25 checks passed
@eizedev
eizedev deleted the refactor/report-hooks branch November 22, 2026 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant