router implementation

This commit is contained in:
Priec
2025-08-22 22:19:59 +02:00
parent 6833ac5fad
commit 957f5bf9f0
9 changed files with 225 additions and 180 deletions

View File

@@ -15,6 +15,7 @@ use crate::state::pages::auth::RegisterState;
use crate::state::pages::admin::AdminState;
use crate::state::pages::admin::AdminFocus;
use crate::state::pages::intro::IntroState;
use crate::pages::routing::{Router, Page};
use crate::buffer::state::BufferState;
use crate::buffer::state::AppView;
use crate::state::app::state::AppState;
@@ -68,6 +69,7 @@ pub async fn run_ui() -> Result<()> {
let mut register_state = RegisterState::default();
let mut intro_state = IntroState::default();
let mut admin_state = AdminState::default();
let mut router = Router::new();
let mut buffer_state = BufferState::default();
let mut app_state = AppState::new().context("Failed to create initial app state")?;
@@ -341,17 +343,10 @@ pub async fn run_ui() -> Result<()> {
}
if let Some(active_view) = buffer_state.get_active_view() {
app_state.ui.show_intro = false;
app_state.ui.show_login = false;
app_state.ui.show_register = false;
app_state.ui.show_admin = false;
app_state.ui.show_add_table = false;
app_state.ui.show_add_logic = false;
app_state.ui.show_form = false;
match active_view {
AppView::Intro => app_state.ui.show_intro = true,
AppView::Login => app_state.ui.show_login = true,
AppView::Register => app_state.ui.show_register = true,
AppView::Intro => router.navigate(Page::Intro(intro_state.clone())),
AppView::Login => router.navigate(Page::Login(login_state.clone())),
AppView::Register => router.navigate(Page::Register(register_state.clone())),
AppView::Admin => {
info!("Active view is Admin, refreshing profile tree...");
match grpc_client.get_profile_tree().await {
@@ -360,36 +355,42 @@ pub async fn run_ui() -> Result<()> {
}
Err(e) => {
error!("Failed to refresh profile tree for Admin panel: {}", e);
event_handler.command_message = format!("Error refreshing admin data: {}", e);
event_handler.command_message =
format!("Error refreshing admin data: {}", e);
}
}
app_state.ui.show_admin = true;
let profile_names = app_state.profile_tree.profiles.iter()
.map(|p| p.name.clone())
.collect();
admin_state.set_profiles(profile_names);
if admin_state.current_focus == AdminFocus::default() ||
!matches!(admin_state.current_focus,
AdminFocus::InsideProfilesList |
AdminFocus::Tables | AdminFocus::InsideTablesList |
AdminFocus::Button1 | AdminFocus::Button2 | AdminFocus::Button3) {
if admin_state.current_focus == AdminFocus::default()
|| !matches!(admin_state.current_focus,
AdminFocus::InsideProfilesList |
AdminFocus::Tables | AdminFocus::InsideTablesList |
AdminFocus::Button1 | AdminFocus::Button2 | AdminFocus::Button3)
{
admin_state.current_focus = AdminFocus::ProfilesPane;
}
if admin_state.profile_list_state.selected().is_none() && !app_state.profile_tree.profiles.is_empty() {
if admin_state.profile_list_state.selected().is_none()
&& !app_state.profile_tree.profiles.is_empty()
{
admin_state.profile_list_state.select(Some(0));
}
router.navigate(Page::Admin(admin_state.clone()));
}
AppView::AddTable => router.navigate(Page::AddTable(admin_state.add_table_state.clone())),
AppView::AddLogic => router.navigate(Page::AddLogic(admin_state.add_logic_state.clone())),
AppView::Form => {
if let Some(form_state) = app_state.form_state().cloned() {
router.navigate(Page::Form(form_state));
}
}
AppView::AddTable => app_state.ui.show_add_table = true,
AppView::AddLogic => app_state.ui.show_add_logic = true,
AppView::Form => app_state.ui.show_form = true,
AppView::Scratch => {}
}
}
// Continue with the rest of the function...
// (The rest remains the same, but now CanvasState trait methods are available)
if app_state.ui.show_form {
let current_view_profile = app_state.current_view_profile_name.clone();
let current_view_table = app_state.current_view_table_name.clone();
@@ -654,12 +655,7 @@ pub async fn run_ui() -> Result<()> {
let mut temp_form_state = form_state_clone.clone();
render_ui(
f,
&mut temp_form_state,
&mut auth_state,
&login_state,
&register_state,
&intro_state,
&mut admin_state,
&mut router,
&buffer_state,
&theme,
event_handler.is_edit_mode,