diff --git a/client/src/modes/handlers/event.rs b/client/src/modes/handlers/event.rs index 5dfac30..dfd7499 100644 --- a/client/src/modes/handlers/event.rs +++ b/client/src/modes/handlers/event.rs @@ -385,8 +385,8 @@ impl EventHandler { key_event, config, app_state, - admin_state, buffer_state, + router, &mut self.command_message, )? { return Ok(EventOutcome::Ok(self.command_message.clone())); diff --git a/client/src/pages/admin/admin/event.rs b/client/src/pages/admin/admin/event.rs index d60593f..52ec956 100644 --- a/client/src/pages/admin/admin/event.rs +++ b/client/src/pages/admin/admin/event.rs @@ -4,9 +4,9 @@ use crossterm::event::KeyEvent; use crate::buffer::state::BufferState; use crate::config::binds::config::Config; -use crate::pages::admin::AdminState; use crate::pages::admin::main::logic::handle_admin_navigation; use crate::state::app::state::AppState; +use crate::pages::routing::{Router, Page}; /// Handle all Admin page-specific key events (movement + actions). /// Returns true if the key was handled (so the caller should stop propagation). @@ -14,46 +14,51 @@ pub fn handle_admin_event( key_event: KeyEvent, config: &Config, app_state: &mut AppState, - admin_state: &mut AdminState, buffer_state: &mut BufferState, + router: &mut Router, command_message: &mut String, ) -> Result { - // 1) Map general action to MovementAction (same mapping used in event.rs) - let movement_action = if let Some(act) = - config.get_general_action(key_event.code, key_event.modifiers) - { - use crate::movement::MovementAction; - match act { - "up" => Some(MovementAction::Up), - "down" => Some(MovementAction::Down), - "left" => Some(MovementAction::Left), - "right" => Some(MovementAction::Right), - "next" => Some(MovementAction::Next), - "previous" => Some(MovementAction::Previous), - "select" => Some(MovementAction::Select), - "esc" => Some(MovementAction::Esc), - _ => None, - } - } else { - None - }; + if let Page::Admin(admin_state) = &mut router.current { + // 1) Map general action to MovementAction (same mapping used in event.rs) + let movement_action = if let Some(act) = + config.get_general_action(key_event.code, key_event.modifiers) + { + use crate::movement::MovementAction; + match act { + "up" => Some(MovementAction::Up), + "down" => Some(MovementAction::Down), + "left" => Some(MovementAction::Left), + "right" => Some(MovementAction::Right), + "next" => Some(MovementAction::Next), + "previous" => Some(MovementAction::Previous), + "select" => Some(MovementAction::Select), + "esc" => Some(MovementAction::Esc), + _ => None, + } + } else { + None + }; - if let Some(ma) = movement_action { - if admin_state.handle_movement(app_state, ma) { + if let Some(ma) = movement_action { + if admin_state.handle_movement(app_state, ma) { + return Ok(true); + } + } + + // 2) Rich Admin navigation (buttons, selections, etc.) + if handle_admin_navigation( + key_event, + config, + app_state, + buffer_state, + router, + command_message, + ) { return Ok(true); } - } - // 2) Rich Admin navigation (buttons, selections, etc.) - if handle_admin_navigation( - key_event, - config, - app_state, - admin_state, - buffer_state, - command_message, - ) { - return Ok(true); + // If we reached here, nothing was handled + return Ok(false); } Ok(false) diff --git a/client/src/pages/admin/main/logic.rs b/client/src/pages/admin/main/logic.rs index 89b716e..8052bcb 100644 --- a/client/src/pages/admin/main/logic.rs +++ b/client/src/pages/admin/main/logic.rs @@ -5,7 +5,8 @@ use crate::config::binds::config::Config; use crate::buffer::state::{BufferState, AppView}; use crate::pages::admin_panel::add_table::state::{AddTableState, LinkDefinition}; use ratatui::widgets::ListState; -use crate::pages::admin_panel::add_logic::state::{AddLogicState, AddLogicFocus}; +use crate::pages::admin_panel::add_logic::state::{AddLogicState, AddLogicFocus, AddLogicFormState}; +use crate::pages::routing::{Page, Router}; // Helper functions list_select_next and list_select_previous remain the same fn list_select_next(list_state: &mut ListState, item_count: usize) { @@ -36,11 +37,15 @@ pub fn handle_admin_navigation( key: crossterm::event::KeyEvent, config: &Config, app_state: &mut AppState, - admin_state: &mut AdminState, buffer_state: &mut BufferState, + router: &mut Router, command_message: &mut String, ) -> bool { let action = config.get_general_action(key.code, key.modifiers).map(String::from); + + let Page::Admin(admin_state) = &mut router.current else { + return false; + }; let current_focus = admin_state.current_focus; let profile_count = app_state.profile_tree.profiles.len(); let mut handled = false; @@ -230,20 +235,20 @@ pub fn handle_admin_navigation( if let Some(profile) = app_state.profile_tree.profiles.get(p_idx) { if let Some(t_idx) = admin_state.selected_table_index { if let Some(table) = profile.tables.get(t_idx) { - // Both profile and table are selected, proceed - admin_state.add_logic_state = AddLogicState { - profile_name: profile.name.clone(), - selected_table_name: Some(table.name.clone()), - selected_table_id: Some(table.id), // If you have table IDs - editor_keybinding_mode: config.editor.keybinding_mode.clone(), - current_focus: AddLogicFocus::default(), - ..AddLogicState::default() - }; - + // Create AddLogic page with selected profile & table + let add_logic_form = AddLogicFormState::new_with_table( + &config.editor, + profile.name.clone(), + Some(table.id), + table.name.clone(), + ); + + // Route to AddLogic + router.current = Page::AddLogic(add_logic_form); // Store table info for later fetching app_state.pending_table_structure_fetch = Some(( profile.name.clone(), - table.name.clone() + table.name.clone(), )); buffer_state.update_history(AppView::AddLogic); diff --git a/client/src/pages/admin_panel/add_logic/state.rs b/client/src/pages/admin_panel/add_logic/state.rs index cd7705d..c71ffce 100644 --- a/client/src/pages/admin_panel/add_logic/state.rs +++ b/client/src/pages/admin_panel/add_logic/state.rs @@ -358,6 +358,24 @@ impl AddLogicFormState { } } + pub fn new_with_table( + editor_config: &EditorConfig, + profile_name: String, + table_id: Option, + table_name: String, + ) -> Self { + let mut state = AddLogicState::new(editor_config); + state.profile_name = profile_name; + state.selected_table_id = table_id; + state.selected_table_name = Some(table_name); + let editor = FormEditor::new(state.clone()); + Self { + state, + editor, + focus_outside_canvas: false, + } + } + pub fn from_state(state: AddLogicState) -> Self { let editor = FormEditor::new(state.clone()); Self {