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

@@ -28,16 +28,11 @@ use ratatui::{
layout::{Constraint, Direction, Layout},
Frame,
};
use crate::pages::routing::{Router, Page};
#[allow(clippy::too_many_arguments)]
pub fn render_ui(
f: &mut Frame,
form_state: &mut FormState,
auth_state: &mut AuthState,
login_state: &LoginState,
register_state: &RegisterState,
intro_state: &IntroState,
admin_state: &mut AdminState,
router: &mut Router,
buffer_state: &BufferState,
theme: &Theme,
is_event_handler_edit_mode: bool,
@@ -78,101 +73,97 @@ pub fn render_ui(
let main_content_area = root_chunks[chunk_idx];
chunk_idx += 1;
if app_state.ui.show_intro {
render_intro(f, intro_state, main_content_area, theme);
} else if app_state.ui.show_register {
render_register(
// ✅ Instead of checking app_state.ui.show_*, just match router.current
match &mut router.current {
Page::Intro(state) => render_intro(f, state, main_content_area, theme),
Page::Login(state) => render_login(
f,
main_content_area,
theme,
register_state,
state,
app_state,
register_state.current_field() < 4, // Now using CanvasState trait method
);
} else if app_state.ui.show_add_table {
render_add_table(
state.current_field() < 2,
),
Page::Register(state) => render_register(
f,
main_content_area,
theme,
state,
app_state,
&mut admin_state.add_table_state,
is_event_handler_edit_mode,
);
} else if app_state.ui.show_add_logic {
render_add_logic(
f,
main_content_area,
theme,
app_state,
&mut admin_state.add_logic_state,
is_event_handler_edit_mode,
);
} else if app_state.ui.show_login {
render_login(
f,
main_content_area,
theme,
login_state,
app_state,
login_state.current_field() < 2, // Now using CanvasState trait method
);
} else if app_state.ui.show_admin {
crate::components::admin::admin_panel::render_admin_panel(
state.current_field() < 4,
),
Page::Admin(state) => crate::components::admin::admin_panel::render_admin_panel(
f,
app_state,
auth_state,
admin_state,
&mut AuthState::default(), // TODO: later move AuthState into Router
state,
main_content_area,
theme,
&app_state.profile_tree,
&app_state.selected_profile,
);
} else if app_state.ui.show_form {
let (sidebar_area, form_actual_area) =
calculate_sidebar_layout(app_state.ui.show_sidebar, main_content_area);
if let Some(sidebar_rect) = sidebar_area {
render_sidebar(
),
Page::AddLogic(state) => render_add_logic(
f,
main_content_area,
theme,
app_state,
state,
is_event_handler_edit_mode,
),
Page::AddTable(state) => render_add_table(
f,
main_content_area,
theme,
app_state,
state,
is_event_handler_edit_mode,
),
Page::Form(state) => {
let (sidebar_area, form_actual_area) =
calculate_sidebar_layout(app_state.ui.show_sidebar, main_content_area);
if let Some(sidebar_rect) = sidebar_area {
render_sidebar(
f,
sidebar_rect,
theme,
&app_state.profile_tree,
&app_state.selected_profile,
);
}
let available_width = form_actual_area.width;
let form_render_area = if available_width >= 80 {
Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Min(0), Constraint::Length(80), Constraint::Min(0)])
.split(form_actual_area)[1]
} else {
Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Min(0),
Constraint::Length(available_width),
Constraint::Min(0),
])
.split(form_actual_area)[1]
};
render_form(
f,
sidebar_rect,
form_render_area,
app_state,
state,
app_state.current_view_table_name.as_deref().unwrap_or(""),
theme,
&app_state.profile_tree,
&app_state.selected_profile,
state.total_count,
state.current_position,
);
}
let available_width = form_actual_area.width;
let form_render_area = if available_width >= 80 {
Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Min(0), Constraint::Length(80), Constraint::Min(0)])
.split(form_actual_area)[1]
} else {
Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Min(0),
Constraint::Length(available_width),
Constraint::Min(0),
])
.split(form_actual_area)[1]
};
render_form(
f,
form_render_area,
app_state,
form_state,
app_state.current_view_table_name.as_deref().unwrap_or(""),
theme,
form_state.total_count,
form_state.current_position,
);
}
if let Some(area) = buffer_list_area {
render_buffer_list(f, area, theme, buffer_state, app_state);
}
// This block now correctly handles drawing popups over any view.
if app_state.ui.show_search_palette {
if let Some(search_state) = &app_state.search_state {
render_search_palette(f, f.area(), theme, search_state);

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,