3.5 KiB
UserPageEdit Refactoring Plan
The UserPageEdit.tsx component has grown too large (~900 lines) and handles too many responsibilities. This plan outlines the steps to decompose it into manageable, single-purpose components and custom hooks, leveraging the existing Action System for cleaner communication.
1. Goal
Split UserPageEdit.tsx to improve maintainability, verify separation of concerns, and fully utilize the src/actions/ system to decouple UI components (like the Ribbon) from the logic.
2. Proposed Architecture
2.1. Directories
src/pages/editor/
components/hooks/UserPageEdit.tsx(The main entry point, simplified)
2.2. Custom Hooks & Logic
Move state and logic into src/pages/editor/hooks/. Crucially, we will use useActions to register capabilities.
-
usePageEditorState.ts- Manages UI state:
isSidebarCollapsed,showHierarchy,showTypeFields,selectedWidgetId. - Action Registration: Registers actions like
View/ToggleSidebar,View/ToggleHierarchy,View/ToggleTypeFields.
- Manages UI state:
-
usePageTemplates.ts- Manages template state.
- Action Registration: Registers
File/LoadTemplate,File/SaveTemplate.
-
useEmailActions.ts- Manages email state.
- Action Registration: Registers
Email/SendTest,Email/TogglePreview.
-
useEditorActions.ts(Core Logic)- Wraps
useLayoutanduseLayoutscontext methods. - Action Registration:
Edit/UndoEdit/RedoFile/SaveFile/ImportLayoutFile/ExportLayoutEdit/AddContainerEdit/AddWidgetEdit/DeletePage
- Wraps
2.3. Components (UI Extraction)
Move UI sections into src/pages/editor/components/:
-
EditorSidebar.tsx- Subscribes to UI state via context or props (managed by
UserPageEdit). - Handlers can trigger Actions.
- Renders
HierarchyTree.
- Subscribes to UI state via context or props (managed by
-
EditorMainArea.tsx- The central workspace.
- Renders
GenericCanvas.
-
EditorRightPanel.tsx- The properties panel.
-
PageRibbonBar.tsx(Refactor)- Change: Instead of accepting 30+ props, it will use
useActions()to retrieve registered actions (Save,Undo,Redo,ToggleVisibility, etc.) and bind them to buttons. - Props will be minimized to just
page(for context) and layout specific data.
- Change: Instead of accepting 30+ props, it will use
-
EditorDialogs.tsx- A container component that renders all global dialogs (Email, Settings, Templates) based on state.
3. Implementation Steps
- Setup Directory: Create
src/pages/editor/structure. - RefactorHooks:
- Implement
useEditorActionsto register core actions. - Implement
usePageEditorStatefor UI toggles.
- Implement
- Refactor
PageRibbonBar:- Update it to use
useActions().getActionsByGroup('History')etc. - Remove prop drilling for
onUndo,onRedo,onSave.
- Update it to use
- Extract Components:
- Move JSX to
EditorSidebar,EditorMainArea,EditorRightPanel.
- Move JSX to
- Reassemble
UserPageEdit:- Initialize hooks.
- Render
ActionProvider(if not at top level) or ensure hooks run inside it. - Pass minimal props to children.
4. Verification
- Ribbon Functionality: Verify buttons (Undo, Redo, Save) are active/disabled correctly via Action state.
- Shortcuts: Verify Ctrl+Z/Y work via the Action registry.
- Layout: Verify UI allows adding widgets/containers.