toggle('dark-mode')}
/>
```
## Component Size Guidelines
### Rules
1. **Maximum 150 LOC** per component file
2. **Extract all business logic** into hooks
3. **Pure presentation** in components
4. **Compose smaller components** instead of conditional rendering
### Example: Breaking Down a Large Component
**Before (300+ LOC):**
```typescript
// ModelDesigner.tsx - 350 LOC
export function ModelDesigner() {
const [models, setModels] = useKV('models', [])
const [selectedModel, setSelectedModel] = useState(null)
const [showDialog, setShowDialog] = useState(false)
const handleAddModel = async () => {
// 50 lines of logic
}
const handleGenerateWithAI = async () => {
// 50 lines of AI logic
}
return (
{/* 200 lines of JSX */}
)
}
```
**After (<150 LOC each):**
```typescript
// hooks/features/use-model-manager.ts - 80 LOC
export function useModelManager() {
const [models, setModels] = useKV('models', [])
const [selectedModel, setSelectedModel] = useState(null)
const addModel = useCallback(async (model) => {
// Business logic
}, [])
const generateWithAI = useCallback(async (description) => {
// AI logic
}, [])
return { models, selectedModel, setSelectedModel, addModel, generateWithAI }
}
// components/ModelList.tsx - 80 LOC
export function ModelList({ models, onSelect, selected }) {
return (
{models.map(model => (
onSelect(model)}
/>
))}
)
}
// components/ModelEditor.tsx - 120 LOC
export function ModelEditor({ model, onChange }) {
return (
{/* Editing UI */}
)
}
// ModelDesigner.tsx - 90 LOC
export function ModelDesigner() {
const { models, selectedModel, setSelectedModel, addModel, generateWithAI } =
useModelManager()
const { open, openDialog, closeDialog } = useDialog()
return (
{selectedModel && (
)}
)
}
```
## JSON-Based Page Orchestration
### Page Config Schema
Pages can be defined in JSON and stored in the KV database:
```json
{
"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 }
],
"shortcuts": [
{ "key": "2", "ctrl": true, "action": "navigate" }
]
}
```
### Benefits
1. **Runtime Configuration**: Change page layouts without code changes
2. **User Customization**: Users can configure their own layouts
3. **Feature Flags**: Enable/disable features per page
4. **A/B Testing**: Test different layouts easily
5. **Persistence**: Layouts save automatically to KV store
## Migration Checklist
When refactoring a large component:
- [ ] Identify business logic vs presentation
- [ ] Extract business logic into hooks (`/hooks/features/`)
- [ ] Break JSX into smaller components (<150 LOC each)
- [ ] Create atomic components where applicable
- [ ] Add component to page config JSON
- [ ] Update component registry
- [ ] Test isolated components
- [ ] Update documentation
## Best Practices
1. **One Hook, One Responsibility**: Each hook should manage one concern
2. **Hooks Return Objects**: Return `{ value, actions }` for clarity
3. **Memoize Callbacks**: Use `useCallback` for functions passed as props
4. **Type Everything**: Full TypeScript types for all hooks
5. **Test Hooks**: Write unit tests for hook logic
6. **Document Hooks**: Add JSDoc comments explaining purpose and usage
7. **Validate with Zod**: Use schemas for complex data structures
8. **Handle Loading/Error States**: Return `{ data, loading, error }` pattern
## Next Steps
1. Extract business logic from top 5 largest components
2. Break down FeatureIdeaCloud (829 LOC) into 6 smaller components
3. Create remaining feature hooks (file-manager, workflow-manager, etc.)
4. Build PageOrchestrator component to render from JSON configs
5. Add UI for editing page layouts
6. Migrate all pages to JSON configuration system
## Resources
- [REFACTOR_PHASE2.md](./REFACTOR_PHASE2.md) - Complete refactoring plan
- [React Hooks Documentation](https://react.dev/reference/react/hooks)
- [Zod Documentation](https://zod.dev/)
- [Atomic Design Methodology](https://bradfrost.com/blog/post/atomic-web-design/)