Skip to content

refactor: split oversized modules into focused domain files - #116

Merged
h4yfans merged 9 commits into
mainfrom
worktree-major-refactor
Mar 29, 2026
Merged

refactor: split oversized modules into focused domain files#116
h4yfans merged 9 commits into
mainfrom
worktree-major-refactor

Conversation

@h4yfans

@h4yfans h4yfans commented Mar 29, 2026

Copy link
Copy Markdown
Collaborator

What

Split 4 oversized files (1040-2089 LOC each) into 17 focused modules, reducing the largest file from 2089 → 265 LOC.

Why

Files exceeded the project's 800 LOC limit. Monolithic query/handler/component files made navigation, testing, and code review difficult. Duplicate logic (PDF/HTML export, activity-level thresholds, getDisplayName) increased maintenance burden.

How

Phase 1 — Pure utility extraction: Moved non-React functions (tree building, wiki-link processing, Zod schemas, serialization helpers) into standalone modules. Zero behavioral change.

Phase 2 — Query module split: Split queries/notes.ts (1590 LOC) into 6 domain modules (tag, link, property, snapshot, journal + shared helpers). All re-exported from notes.ts for backward compatibility — no consumer changes needed.

Phase 3 — IPC handler split: Extracted property definition and export handlers. Consolidated duplicate PDF/HTML export into shared exportNote() with format parameter. Created extractError() utility replacing 16 identical inline patterns.

Phase 4 — React hooks: Extracted useTreeDragDrop, useTreeRename, useTreeDelete from 2089-LOC NotesTree component. Skipped ContentArea drag-drop extraction (too intertwined with editor API — would be a "bucket hook").

Key decisions:

  • Re-export pattern (export * from './sub-module') preserves all existing import paths
  • ACTIVITY_THRESHOLDS constants shared between TypeScript function and SQL CASE expression
  • DrizzleDb type defined per-module to avoid circular deps (1 line each)

Type

  • refactor — restructure without behavior change

Test plan

  • Unit tests added/updated
  • Manual testing (describe below)

All 5,550 existing tests pass (5,211 desktop + 339 sync-server). No new tests needed — this is a pure restructuring with identical behavior, verified by the full existing test suite.

Checklist

  • Self-reviewed the diff
  • No hardcoded secrets or credentials
  • Files stay under ~500 LOC
  • Follows immutable data patterns

h4yfans added 6 commits March 29, 2026 21:11
1590-LOC monolith → 265 LOC core + 6 focused modules:
- tag-queries.ts (tags + definitions)
- link-queries.ts (wiki-link resolution)
- property-queries.ts (properties + definitions)
- snapshot-queries.ts (version history)
- journal-queries.ts (heatmap, streaks, stats)
- query-helpers.ts (serialization, activity level constants)

All re-exported from notes.ts for backward compatibility.
1040-LOC handler → 525 LOC + 4 focused modules:
- notes-schemas.ts (shared Zod schemas with DRY property type enum)
- export-handlers.ts (consolidated PDF/HTML export, was ~90% duplicated)
- property-definition-handlers.ts (property definition CRUD)
- handler-utils.ts (shared extractError utility)
…al hooks

notes-tree.tsx: 2089 → 1192 LOC via 3 custom hooks:
- useTreeDragDrop (move/reorder logic)
- useTreeRename (note + folder rename state)
- useTreeDelete (delete state + confirmation)

ContentArea.tsx: 1469 → 1122 LOC via wiki-link-utils.ts extraction.
Deduplicated getDisplayName between notes-tree and virtualized-notes-tree.
- Add PREVIEW_BY_TITLE handler to notes-handlers.ts
- Add STATUS/SELECT/MULTISELECT types to notes-schemas.ts
- Add property option CRUD handlers (ADD_PROPERTY_OPTION, ADD_STATUS_OPTION,
  REMOVE/RENAME/UPDATE_OPTION_COLOR, DELETE_PROPERTY_DEFINITION, ENSURE)
- Add TreeActionsExposer + collapseAll/expandAll to NotesTreeActions
- Use generalSettings.createInSelectedFolder instead of isTreeFocusedRef
@h4yfans
h4yfans force-pushed the worktree-major-refactor branch from 4a9c718 to 6ac6a8d Compare March 29, 2026 18:30
h4yfans added 3 commits March 29, 2026 21:36
Notes-tree.tsx and ContentArea.tsx reverted to main's versions during
rebase conflict resolution. Hook files (use-tree-drag-drop, use-tree-rename,
use-tree-delete) remain as extracted but unwired — available for a follow-up
PR that properly integrates them with main's updated component code.

The query split, IPC handler split, and utility extractions are intact.
@h4yfans
h4yfans merged commit 25eef4f into main Mar 29, 2026
4 checks passed
@h4yfans
h4yfans deleted the worktree-major-refactor branch April 2, 2026 20:27
h4yfans added a commit that referenced this pull request May 6, 2026
* refactor(queries): split notes.ts into domain-specific query modules

1590-LOC monolith → 265 LOC core + 6 focused modules:
- tag-queries.ts (tags + definitions)
- link-queries.ts (wiki-link resolution)
- property-queries.ts (properties + definitions)
- snapshot-queries.ts (version history)
- journal-queries.ts (heatmap, streaks, stats)
- query-helpers.ts (serialization, activity level constants)

All re-exported from notes.ts for backward compatibility.

* refactor(ipc): extract schemas, export, and property handlers

1040-LOC handler → 525 LOC + 4 focused modules:
- notes-schemas.ts (shared Zod schemas with DRY property type enum)
- export-handlers.ts (consolidated PDF/HTML export, was ~90% duplicated)
- property-definition-handlers.ts (property definition CRUD)
- handler-utils.ts (shared extractError utility)

* refactor(renderer): extract tree utils, wiki-link utils, and behavioral hooks

notes-tree.tsx: 2089 → 1192 LOC via 3 custom hooks:
- useTreeDragDrop (move/reorder logic)
- useTreeRename (note + folder rename state)
- useTreeDelete (delete state + confirmation)

ContentArea.tsx: 1469 → 1122 LOC via wiki-link-utils.ts extraction.
Deduplicated getDisplayName between notes-tree and virtualized-notes-tree.

* docs: add Major Refactor changelog entry

* chore: regenerate IPC invoke map

* fix: resolve rebase conflicts and port main's new features

- Add PREVIEW_BY_TITLE handler to notes-handlers.ts
- Add STATUS/SELECT/MULTISELECT types to notes-schemas.ts
- Add property option CRUD handlers (ADD_PROPERTY_OPTION, ADD_STATUS_OPTION,
  REMOVE/RENAME/UPDATE_OPTION_COLOR, DELETE_PROPERTY_DEFINITION, ENSURE)
- Add TreeActionsExposer + collapseAll/expandAll to NotesTreeActions
- Use generalSettings.createInSelectedFolder instead of isTreeFocusedRef

* chore: regenerate IPC invoke map

* fix: restore main-compatible component versions after rebase

Notes-tree.tsx and ContentArea.tsx reverted to main's versions during
rebase conflict resolution. Hook files (use-tree-drag-drop, use-tree-rename,
use-tree-delete) remain as extracted but unwired — available for a follow-up
PR that properly integrates them with main's updated component code.

The query split, IPC handler split, and utility extractions are intact.

* fix: remove duplicate export/property-definition handlers causing IPC map conflict
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