router2, needs bug fixes

This commit is contained in:
Priec
2025-08-22 22:52:20 +02:00
parent 957f5bf9f0
commit 5d97e63f93
7 changed files with 337 additions and 335 deletions

View File

@@ -30,6 +30,7 @@ use crate::state::{
intro::IntroState,
},
};
use crate::pages::routing::{Router, Page};
use crate::search::state::SearchState;
use crate::tui::functions::common::login::LoginResult;
use crate::tui::functions::common::register::RegisterResult;
@@ -127,90 +128,69 @@ impl EventHandler {
// Helper functions - replace the removed event_helper functions
fn get_current_field_for_state(
app_state: &AppState,
login_state: &LoginState,
register_state: &RegisterState,
form_state: &FormState,
router: &Router,
) -> usize {
if app_state.ui.show_login {
login_state.current_field()
} else if app_state.ui.show_register {
register_state.current_field()
} else {
form_state.current_field()
match &router.current {
Page::Login(state) => state.current_field(),
Page::Register(state) => state.current_field(),
Page::Form(state) => state.current_field(),
_ => 0,
}
}
fn get_current_cursor_pos_for_state(
app_state: &AppState,
login_state: &LoginState,
register_state: &RegisterState,
form_state: &FormState,
router: &Router,
) -> usize {
if app_state.ui.show_login {
login_state.current_cursor_pos()
} else if app_state.ui.show_register {
register_state.current_cursor_pos()
} else {
form_state.current_cursor_pos()
match &router.current {
Page::Login(state) => state.current_cursor_pos(),
Page::Register(state) => state.current_cursor_pos(),
Page::Form(state) => state.current_cursor_pos(),
_ => 0,
}
}
fn get_has_unsaved_changes_for_state(
app_state: &AppState,
login_state: &LoginState,
register_state: &RegisterState,
form_state: &FormState,
router: &Router,
) -> bool {
if app_state.ui.show_login {
login_state.has_unsaved_changes()
} else if app_state.ui.show_register {
register_state.has_unsaved_changes()
} else {
form_state.has_unsaved_changes()
match &router.current {
Page::Login(state) => state.has_unsaved_changes(),
Page::Register(state) => state.has_unsaved_changes(),
Page::Form(state) => state.has_unsaved_changes(),
_ => false,
}
}
fn get_current_input_for_state<'a>(
app_state: &AppState,
login_state: &'a LoginState,
register_state: &'a RegisterState,
form_state: &'a FormState,
router: &'a Router,
) -> &'a str {
if app_state.ui.show_login {
login_state.get_current_input()
} else if app_state.ui.show_register {
register_state.get_current_input()
} else {
form_state.get_current_input()
match &router.current {
Page::Login(state) => state.get_current_input(),
Page::Register(state) => state.get_current_input(),
Page::Form(state) => state.get_current_input(),
_ => "",
}
}
fn set_current_cursor_pos_for_state(
app_state: &AppState,
login_state: &mut LoginState,
register_state: &mut RegisterState,
form_state: &mut FormState,
router: &mut Router,
pos: usize,
) {
if app_state.ui.show_login {
login_state.set_current_cursor_pos(pos);
} else if app_state.ui.show_register {
register_state.set_current_cursor_pos(pos);
} else {
form_state.set_current_cursor_pos(pos);
match &mut router.current {
Page::Login(state) => state.set_current_cursor_pos(pos),
Page::Register(state) => state.set_current_cursor_pos(pos),
Page::Form(state) => state.set_current_cursor_pos(pos),
_ => {},
}
}
fn get_cursor_pos_for_mixed_state(
app_state: &AppState,
login_state: &LoginState,
form_state: &FormState,
router: &Router,
) -> usize {
if app_state.ui.show_login || app_state.ui.show_register {
login_state.current_cursor_pos()
} else {
form_state.current_cursor_pos()
match &router.current {
Page::Login(state) => state.current_cursor_pos(),
Page::Register(state) => state.current_cursor_pos(),
Page::Form(state) => state.current_cursor_pos(),
_ => 0,
}
}
@@ -222,12 +202,9 @@ impl EventHandler {
terminal: &mut TerminalCore,
command_handler: &mut CommandHandler,
auth_state: &mut AuthState,
login_state: &mut LoginState,
register_state: &mut RegisterState,
intro_state: &mut IntroState,
admin_state: &mut AdminState,
buffer_state: &mut BufferState,
app_state: &mut AppState,
router: &mut Router,
) -> Result<EventOutcome> {
if app_state.ui.show_search_palette {
if let Event::Key(key_event) = event {
@@ -244,7 +221,7 @@ impl EventHandler {
}
let mut current_mode =
ModeManager::derive_mode(app_state, self, admin_state);
ModeManager::derive_mode(app_state, self, router);
if current_mode == AppMode::General && self.navigation_state.active {
if let Event::Key(key_event) = event {
@@ -258,7 +235,7 @@ impl EventHandler {
if !self.navigation_state.active {
self.command_message = outcome.get_message_if_ok();
current_mode =
ModeManager::derive_mode(app_state, self, admin_state);
ModeManager::derive_mode(app_state, self, router);
}
app_state.update_mode(current_mode);
return Ok(outcome);
@@ -269,25 +246,14 @@ impl EventHandler {
app_state.update_mode(current_mode);
let current_view = {
let ui = &app_state.ui;
if ui.show_intro {
AppView::Intro
} else if ui.show_login {
AppView::Login
} else if ui.show_register {
AppView::Register
} else if ui.show_admin {
AppView::Admin
} else if ui.show_add_logic {
AppView::AddLogic
} else if ui.show_add_table {
AppView::AddTable
} else if ui.show_form {
AppView::Form
} else {
AppView::Scratch
}
let current_view = match &router.current {
Page::Intro(_) => AppView::Intro,
Page::Login(_) => AppView::Login,
Page::Register(_) => AppView::Register,
Page::Admin(_) => AppView::Admin,
Page::AddLogic(_) => AppView::AddLogic,
Page::AddTable(_) => AppView::AddTable,
Page::Form(_) => AppView::Form,
};
buffer_state.update_history(current_view);
@@ -297,10 +263,8 @@ impl EventHandler {
&Event::Key(key_event),
config,
app_state,
login_state,
register_state,
buffer_state,
admin_state,
router,
)
.await
{
@@ -386,7 +350,7 @@ impl EventHandler {
config.get_general_action(key_code, modifiers)
{
if action == "open_search" {
if app_state.ui.show_form {
if let Page::Form(_) = &router.current {
if let Some(table_name) =
app_state.current_view_table_name.clone()
{
@@ -405,31 +369,31 @@ impl EventHandler {
match current_mode {
AppMode::General => {
if app_state.ui.show_admin
&& auth_state.role.as_deref() == Some("admin")
{
if admin_nav::handle_admin_navigation(
key_event,
config,
app_state,
admin_state,
buffer_state,
&mut self.command_message,
) {
return Ok(EventOutcome::Ok(
self.command_message.clone(),
));
if let Page::Admin(admin_state) = &router.current {
if auth_state.role.as_deref() == Some("admin") {
if admin_nav::handle_admin_navigation(
key_event,
config,
app_state,
admin_state,
buffer_state,
&mut self.command_message,
) {
return Ok(EventOutcome::Ok(
self.command_message.clone(),
));
}
}
}
if app_state.ui.show_add_logic {
if let Page::AddLogic(add_logic_state) = &mut router.current {
let client_clone = self.grpc_client.clone();
let sender_clone = self.save_logic_result_sender.clone();
if add_logic_nav::handle_add_logic_navigation(
key_event,
config,
app_state,
&mut admin_state.add_logic_state,
add_logic_state,
&mut self.is_edit_mode,
buffer_state,
client_clone,
@@ -442,14 +406,14 @@ impl EventHandler {
}
}
if app_state.ui.show_add_table {
if let Page::AddTable(add_table_state) = &mut router.current {
let client_clone = self.grpc_client.clone();
let sender_clone = self.save_table_result_sender.clone();
if add_table_nav::handle_add_table_navigation(
key_event,
config,
app_state,
&mut admin_state.add_table_state,
add_table_state,
client_clone,
sender_clone,
&mut self.command_message,
@@ -464,10 +428,7 @@ impl EventHandler {
key_event,
config,
app_state,
login_state,
register_state,
intro_state,
admin_state,
router,
&mut self.command_mode,
&mut self.command_input,
&mut self.command_message,
@@ -482,53 +443,68 @@ impl EventHandler {
buffer_state,
index,
);
if app_state.ui.show_admin
&& !app_state
if let Page::Admin(admin_state) = &router.current {
if !app_state
.profile_tree
.profiles
.is_empty()
{
admin_state
.profile_list_state
.select(Some(0));
{
admin_state
.profile_list_state
.select(Some(0));
}
}
format!("Intro Option {} selected", index)
}
UiContext::Login => match index {
0 => login::initiate_login(
login_state,
app_state,
self.auth_client.clone(),
self.login_result_sender.clone(),
),
1 => login::back_to_main(
login_state,
app_state,
buffer_state,
)
.await,
_ => "Invalid Login Option".to_string(),
},
UiContext::Register => match index {
0 => register::initiate_registration(
register_state,
app_state,
self.auth_client.clone(),
self.register_result_sender.clone(),
),
1 => register::back_to_login(
register_state,
app_state,
buffer_state,
)
.await,
_ => "Invalid Login Option".to_string(),
},
UiContext::Login => {
if let Page::Login(login_state) = &router.current {
match index {
0 => login::initiate_login(
login_state,
app_state,
self.auth_client.clone(),
self.login_result_sender.clone(),
),
1 => login::back_to_main(
login_state,
app_state,
buffer_state,
)
.await,
_ => "Invalid Login Option".to_string(),
}
} else {
"Invalid state".to_string()
}
}
UiContext::Register => {
if let Page::Register(register_state) = &router.current {
match index {
0 => register::initiate_registration(
register_state,
app_state,
self.auth_client.clone(),
self.register_result_sender.clone(),
),
1 => register::back_to_login(
register_state,
app_state,
buffer_state,
)
.await,
_ => "Invalid Login Option".to_string(),
}
} else {
"Invalid state".to_string()
}
}
UiContext::Admin => {
admin::handle_admin_selection(
app_state,
admin_state,
);
if let Page::Admin(admin_state) = &router.current {
admin::handle_admin_selection(
app_state,
admin_state,
);
}
format!("Admin Option {} selected", index)
}
UiContext::Dialog => "Internal error: Unexpected dialog state"
@@ -542,7 +518,7 @@ impl EventHandler {
AppMode::ReadOnly => {
// First let the canvas editor try to handle the key
if app_state.ui.show_form {
if let Page::Form(_) = &router.current {
if let Some(editor) = &mut app_state.form_editor {
let outcome = editor.handle_key_event(key_event);
let new_mode = AppMode::from(editor.mode());
@@ -588,10 +564,9 @@ impl EventHandler {
.handle_core_action(
action,
auth_state,
login_state,
register_state,
terminal,
app_state,
router,
)
.await;
}
@@ -603,7 +578,7 @@ impl EventHandler {
}
AppMode::Highlight => {
if app_state.ui.show_form {
if let Page::Form(_) = &router.current {
if let Some(editor) = &mut app_state.form_editor {
let outcome = editor.handle_key_event(key_event);
let new_mode = AppMode::from(editor.mode());
@@ -640,10 +615,9 @@ impl EventHandler {
.handle_core_action(
action,
auth_state,
login_state,
register_state,
terminal,
app_state,
router,
)
.await;
}
@@ -652,7 +626,7 @@ impl EventHandler {
}
// Let the canvas editor handle edit-mode keys
if app_state.ui.show_form {
if let Page::Form(_) = &router.current {
if let Some(editor) = &mut app_state.form_editor {
let outcome = editor.handle_key_event(key_event);
let new_mode = AppMode::from(editor.mode());
@@ -696,7 +670,7 @@ impl EventHandler {
}
if config.is_command_execute(key_code, modifiers) {
let (mut current_position, total_count) = if let Some(fs) = app_state.form_state() {
let (mut current_position, total_count) = if let Page::Form(fs) = &router.current {
(fs.current_position, fs.total_count)
} else {
(1, 0)
@@ -706,8 +680,7 @@ impl EventHandler {
key_event,
config,
app_state,
login_state,
register_state,
router,
&mut self.command_input,
&mut self.command_message,
&mut self.grpc_client,
@@ -716,7 +689,7 @@ impl EventHandler {
&mut current_position,
total_count,
).await?;
if let Some(fs) = app_state.form_state_mut() {
if let Page::Form(fs) = &mut router.current {
fs.current_position = current_position;
}
self.command_mode = false;
@@ -724,7 +697,7 @@ impl EventHandler {
let new_mode = ModeManager::derive_mode(
app_state,
self,
admin_state,
router,
);
app_state.update_mode(new_mode);
return Ok(outcome);
@@ -746,9 +719,7 @@ impl EventHandler {
&sequence,
) == Some("find_file_palette_toggle")
{
if app_state.ui.show_form
|| app_state.ui.show_intro
{
if matches!(&router.current, Page::Form(_) | Page::Intro(_)) {
let mut all_table_paths: Vec<String> =
app_state
.profile_tree
@@ -830,14 +801,13 @@ impl EventHandler {
&mut self,
action: &str,
auth_state: &mut AuthState,
login_state: &mut LoginState,
register_state: &mut RegisterState,
terminal: &mut TerminalCore,
app_state: &mut AppState,
router: &mut Router,
) -> Result<EventOutcome> {
match action {
"save" => {
if app_state.ui.show_login {
if let Page::Login(login_state) = &router.current {
let message = crate::tui::functions::common::login::save(
auth_state,
login_state,
@@ -847,7 +817,7 @@ impl EventHandler {
.await?;
Ok(EventOutcome::Ok(message))
} else {
let save_outcome = if let Some(fs) = app_state.form_state_mut() {
let save_outcome = if let Page::Form(_) = &router.current {
crate::tui::functions::common::form::save(
app_state,
&mut self.grpc_client,
@@ -874,7 +844,7 @@ impl EventHandler {
))
}
"save_and_quit" => {
let message = if app_state.ui.show_login {
let message = if let Page::Login(login_state) = &router.current {
crate::tui::functions::common::login::save(
auth_state,
login_state,
@@ -903,17 +873,17 @@ impl EventHandler {
)))
}
"revert" => {
let message = if app_state.ui.show_login {
let message = if let Page::Login(login_state) = &router.current {
crate::tui::functions::common::login::revert(login_state, app_state)
.await
} else if app_state.ui.show_register {
} else if let Page::Register(register_state) = &router.current {
crate::tui::functions::common::register::revert(
register_state,
app_state,
)
.await
} else {
if let Some(fs) = app_state.form_state_mut() {
if let Page::Form(_) = &router.current {
crate::tui::functions::common::form::revert(
app_state,
&mut self.grpc_client,