Skip to content

refactor(frontend): split the CanvasPage god component#55

Merged
Derssa merged 8 commits into
mainfrom
othmane/canvas-page-split
Jul 14, 2026
Merged

refactor(frontend): split the CanvasPage god component#55
Derssa merged 8 commits into
mainfrom
othmane/canvas-page-split

Conversation

@OthmaneZ05

Copy link
Copy Markdown
Collaborator

Summary

CanvasPage.tsx had grown to ~1,800 lines, mixing drag-and-drop geometry, IP allocation, firewall-rule generation, edge derivation, persistence and the orchestration of 13 modals in one component. This PR splits it into focused modules while keeping behavior identical — CanvasPage is now a ~690-line orchestrator.

Extracted pure modules (each with direct unit tests)

  • utils/canvasGeometry.ts — grid math, subnet sizing, point-in-subnet hit-testing, absolute coordinates, cell snapping/collision search (was duplicated inline ~6×).
  • utils/securityRules.ts — default deny/allow rules, connection→rule creation, edge-id parsing, rule removal, and buildFirewallEdges (the rules→edges projection).
  • utils/networkConfigOps.tsautoGrowContainers, IP allocation, assignNodeToSubnet (dedupes the drop and drag-reparent paths), delete cascade, subnet creation (CIDR derived from the VPC CIDR).

Extracted hooks

  • hooks/useNetworkConfig.ts — config state, backend fetch with localStorage fallback, the save path (applies the server's corrected config), and the architecture-audit toasts.
  • hooks/useCanvasDragDrop.ts — all React Flow drag/drop/delete handlers, now thin glue over the pure modules.

Extracted components

  • components/CanvasModals.tsx + components/CreateNodeModal.tsx — the 13 modal blocks relocated verbatim; the nested title/placeholder/prefix ternaries replaced by a per-type copy record.

Other changes

  • Canonical network types in shared/types/network.ts; architectureValidator, VpcModal, SecurityGroupsModal and CanvasPage now share them instead of duplicating.
  • The six inspecting* modal states consolidated into one InspectorState union (they were mutually exclusive).

Test coverage

This closes the jsdom coverage gaps documented in CanvasPage.test.tsx: drag/reparent geometry, onConnect rule creation and edge derivation are now unit-tested directly (57 new tests). Existing tests pass without assertion changes.

Test plan

  • npm test (frontend) — 168 passed (111 before)
  • npm run build + npm run lint (frontend) — clean
  • npm test + npm run build (backend, untouched) — 55 passed
  • Manual Docker pass: subnet + node drop, drag between subnets (IP reallocation), drag outside (revert), connection edge create/delete, node/subnet delete, reload persistence

@OthmaneZ05
OthmaneZ05 requested a review from Derssa as a code owner July 14, 2026 04:04
@Derssa
Derssa merged commit 3ccfeb4 into main Jul 14, 2026
2 checks passed
@Derssa
Derssa deleted the othmane/canvas-page-split branch July 14, 2026 15:32
OthmaneZ05 added a commit that referenced this pull request Jul 18, 2026
* refactor(frontend): unify network topology types in shared/types/network.ts

* refactor(frontend): extract canvas grid and hit-test geometry into pure canvasGeometry module

* refactor(frontend): extract security-rule and firewall-edge logic into pure securityRules module

* refactor(frontend): extract network config transforms into pure networkConfigOps module

* refactor(frontend): move network config state, save path and audit into useNetworkConfig hook

* refactor(frontend): move drag/drop/delete canvas handlers into useCanvasDragDrop hook

* refactor(frontend): consolidate the six inspector modal states into one InspectorState union

* refactor(frontend): relocate the canvas modal blocks into CanvasModals and CreateNodeModal
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.

2 participants