mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-04-25 14:25:02 +00:00
7.9 KiB
7.9 KiB
Phase 2 Refactoring Plan: Hooks, Components & JSON Orchestration
Goals
- All components <150 LOC - Break down large feature components into composable pieces
- Comprehensive Hook Library - Extract all business logic into reusable hooks
- JSON-Based Page Orchestration - Define page layouts and feature configurations in JSON, loaded from database
- Database-Driven UI - Store UI configurations, feature flags, and layouts in KV store
Hook Library Architecture
Core Hooks (/src/hooks/core/)
use-kv-state.ts- Enhanced KV wrapper with validationuse-debounced-save.ts- Debounced auto-save to KVuse-undo-redo.ts- Undo/redo state managementuse-clipboard.ts- Copy/paste operationsuse-hotkeys.ts- Keyboard shortcut management
Feature Hooks (/src/hooks/features/)
use-file-manager.ts- File CRUD operationsuse-model-manager.ts- Prisma model operationsuse-component-manager.ts- Component tree operationsuse-workflow-manager.ts- Workflow node operationsuse-lambda-manager.ts- Lambda function operationsuse-test-manager.ts- Test suite operationsuse-theme-manager.ts- Theme variant operationsuse-project-manager.ts- Project save/load/exportuse-idea-manager.ts- Feature idea cloud operationsuse-connection-manager.ts- Idea connections (1:1 mapping)
AI Hooks (/src/hooks/ai/)
use-ai-generation.ts- Generic AI generationuse-ai-code-improvement.ts- Code enhancementuse-ai-explanation.ts- Code explanationuse-ai-test-generation.ts- Test generationuse-ai-model-suggestion.ts- Model field suggestions
UI Hooks (/src/hooks/ui/)
use-dialog.ts- Dialog state managementuse-toast.ts- Toast notifications wrapperuse-confirmation.ts- Confirmation dialogsuse-selection.ts- Multi-select stateuse-drag-drop.ts- Drag and drop operationsuse-canvas-drawing.ts- Canvas drawing toolsuse-zoom-pan.ts- Canvas zoom/pan
Validation Hooks (/src/hooks/validation/)
use-form-validation.ts- Form validationuse-code-validation.ts- Code syntax validationuse-schema-validation.ts- Schema validation
Component Size Breakdown
Large Components to Split (Current → Target LOC)
FeatureIdeaCloud.tsx (829 LOC → <150 each)
Split into:
IdeaCanvas.tsx(80 LOC) - Canvas renderingIdeaCard.tsx(70 LOC) - Individual idea nodeIdeaConnection.tsx(60 LOC) - Arrow renderingIdeaToolbar.tsx(50 LOC) - Tool controlsIdeaColorPicker.tsx(40 LOC) - Color selectionIdeaGroupBoundary.tsx(60 LOC) - Group container
App.tsx (828 LOC → <150 each)
Split into:
AppShell.tsx(100 LOC) - Main layoutPageRouter.tsx(80 LOC) - Tab routing logicExportDialog.tsx(120 LOC) - Export functionality- Hooks:
use-app-state.ts,use-export.ts
CodeEditor.tsx (~400 LOC → <150 each)
Split into:
EditorTabs.tsx(80 LOC) - Tab barEditorMonaco.tsx(100 LOC) - Monaco wrapperEditorToolbar.tsx(60 LOC) - Action buttonsEditorAIPanel.tsx(90 LOC) - AI features
ModelDesigner.tsx (~350 LOC → <150 each)
Split into:
ModelList.tsx(80 LOC) - Model listModelEditor.tsx(120 LOC) - Field editorModelGraph.tsx(90 LOC) - Visual graph
WorkflowDesigner.tsx (~500 LOC → <150 each)
Split into:
WorkflowCanvas.tsx(120 LOC) - React Flow canvasWorkflowNodePalette.tsx(70 LOC) - Node typesWorkflowNodeEditor.tsx(100 LOC) - Node configWorkflowToolbar.tsx(60 LOC) - Toolbar
JSON-Based Page Orchestration
Page Configuration Schema
interface PageConfig {
id: string
title: string
description: string
icon: string
component: string
layout: LayoutConfig
features: FeatureConfig[]
permissions?: string[]
shortcuts?: KeyboardShortcut[]
}
interface LayoutConfig {
type: 'single' | 'split' | 'grid' | 'tabs'
panels?: PanelConfig[]
direction?: 'horizontal' | 'vertical'
defaultSizes?: number[]
}
interface PanelConfig {
id: string
component: string
props?: Record<string, any>
minSize?: number
maxSize?: number
}
interface FeatureConfig {
id: string
enabled: boolean
config?: Record<string, any>
}
Example Page Definitions
{
"pages": [
{
"id": "code-editor",
"title": "Code Editor",
"description": "Edit project files with AI assistance",
"icon": "Code",
"component": "CodeEditorPage",
"layout": {
"type": "split",
"direction": "horizontal",
"defaultSizes": [20, 80],
"panels": [
{
"id": "file-tree",
"component": "FileExplorer",
"minSize": 15,
"maxSize": 40
},
{
"id": "editor",
"component": "CodeEditor",
"minSize": 60
}
]
},
"features": [
{ "id": "ai-improve", "enabled": true },
{ "id": "ai-explain", "enabled": true },
{ "id": "syntax-validation", "enabled": true }
],
"shortcuts": [
{ "key": "2", "ctrl": true, "action": "navigate" }
]
},
{
"id": "idea-cloud",
"title": "Feature Ideas",
"description": "Brainstorm and connect ideas",
"icon": "Lightbulb",
"component": "IdeaCloudPage",
"layout": {
"type": "single"
},
"features": [
{ "id": "connections", "enabled": true },
{ "id": "groups", "enabled": true },
{ "id": "colors", "enabled": true }
]
}
]
}
Database-Driven UI
KV Store Structure
ui-config:{page-id} → PageConfig
feature-toggles:{feature-id} → boolean
layout-state:{page-id} → LayoutState
user-preferences → UserPreferences
Implementation Files
/src/config/
├── page-registry.ts # Component name → React component mapping
├── default-pages.json # Default page configurations
└── page-schema.ts # Zod schemas for validation
/src/hooks/config/
├── use-page-config.ts # Load page config from KV
├── use-layout-state.ts # Persist layout state
└── use-feature-flags.ts # Feature flag management
/src/components/layout/
├── PageOrchestrator.tsx # Renders pages from JSON config
├── DynamicLayout.tsx # Renders layouts from config
└── DynamicPanel.tsx # Renders panels from config
Migration Strategy
Phase 2.1: Hook Extraction (Week 1)
- Create hook library structure
- Extract business logic from top 5 largest components
- Add comprehensive tests for hooks
- Update components to use hooks
Phase 2.2: Component Splitting (Week 2)
- Split FeatureIdeaCloud into 6 components
- Split App.tsx into 3 components + hooks
- Split CodeEditor into 4 components
- Ensure all new components <150 LOC
Phase 2.3: JSON Orchestration (Week 3)
- Create page config schema and defaults
- Build PageOrchestrator and DynamicLayout
- Create component registry
- Migrate 3 pages to JSON config
Phase 2.4: Database Integration (Week 4)
- Store page configs in KV
- Add UI for editing page layouts
- Feature flag management UI
- User preference persistence
Success Criteria
- All components <150 LOC
- 30+ hooks in organized library
- 5+ pages driven by JSON config
- Page configs stored in KV database
- Zero business logic in components (all in hooks)
- All tests passing
- Documentation for hook library
- Migration guide for developers
Benefits
- Maintainability: Smaller components easier to understand and modify
- Reusability: Hooks can be used across multiple components
- Testability: Hooks and small components easier to test in isolation
- Flexibility: JSON configs allow runtime UI changes without code deploys
- Scalability: Easy to add new pages/features via JSON
- Safety: Smaller changes reduce risk of breaking existing features