Compare commits

...

9 Commits

Author SHA1 Message Date
filipriec
e31138c250 DONE we have now intro state separate from others completely 2025-04-14 20:00:02 +02:00
filipriec
7cbe5ce8be intro movement fixed 2025-04-14 16:25:54 +02:00
filipriec
1c31d4cd1c moved introstate into the state folder 2025-04-14 16:24:21 +02:00
filipriec
990ec9317f admin panel tiny improvement. STARTING OF ADMIN PANEL BUILDING 2025-04-14 15:27:26 +02:00
filipriec
718ceac17e cargo fix 2025-04-14 15:04:24 +02:00
filipriec
d154ba6b89 we compiled for now 2025-04-14 14:28:36 +02:00
filipriec
f2a63476b3 continuation of the fixes 2025-04-14 14:05:20 +02:00
filipriec
adcd3b37fa fixing this 2025-04-14 13:23:09 +02:00
filipriec
71dabc1e37 very bald changes, still destroyed 2025-04-14 12:07:15 +02:00
23 changed files with 287 additions and 312 deletions

View File

@@ -1,118 +1,108 @@
// src/components/admin/admin_panel.rs
use crate::config::colors::themes::Theme;
use crate::state::pages::admin::AdminState; // Import the persistent state
use common::proto::multieko2::table_definition::ProfileTreeResponse;
use ratatui::{
widgets::{Block, BorderType, Borders, List, ListItem, ListState, Paragraph},
style::Style,
text::{Line, Span, Text},
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::Style, // Added Modifier
text::{Line, Span, Text},
widgets::{Block, BorderType, Borders, List, ListItem, Paragraph, Wrap},
Frame,
};
use common::proto::multieko2::table_definition::ProfileTreeResponse;
use crate::config::colors::themes::Theme;
pub struct AdminPanelState {
pub list_state: ListState,
pub profiles: Vec<String>,
}
// Renamed from AdminPanelState::render and made a standalone function
pub fn render_admin_panel(
f: &mut Frame,
admin_state: &AdminState, // Accept the persistent state (immutable borrow is enough for rendering)
area: Rect,
theme: &Theme,
profile_tree: &ProfileTreeResponse,
selected_profile: &Option<String>, // The globally selected profile for the checkmark
) {
let block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(theme.accent))
.style(Style::default().bg(theme.bg));
impl AdminPanelState {
pub fn new(profiles: Vec<String>) -> Self {
let mut list_state = ListState::default();
if !profiles.is_empty() {
list_state.select(Some(0));
}
Self { list_state, profiles }
}
let inner_area = block.inner(area);
f.render_widget(block, area);
pub fn next(&mut self) {
let i = self.list_state.selected().map_or(0, |i|
if i >= self.profiles.len() - 1 { 0 } else { i + 1 });
self.list_state.select(Some(i));
}
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Length(3), Constraint::Min(1)])
.split(inner_area);
pub fn previous(&mut self) {
let i = self.list_state.selected().map_or(0, |i|
if i == 0 { self.profiles.len() - 1 } else { i - 1 });
self.list_state.select(Some(i));
}
// Title
let title = Line::from(Span::styled("Admin Panel", Style::default().fg(theme.highlight)));
let title_widget = Paragraph::new(title).alignment(Alignment::Center);
f.render_widget(title_widget, chunks[0]);
pub fn render(
&mut self,
f: &mut Frame,
area: Rect,
theme: &Theme,
profile_tree: &ProfileTreeResponse,
selected_profile: &Option<String>,
) {
let block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(theme.accent))
.style(Style::default().bg(theme.bg));
// Content
let content_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
.split(chunks[1]);
let inner_area = block.inner(area);
f.render_widget(block, area);
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Length(3), Constraint::Min(1)])
.split(inner_area);
// Title
let title = Line::from(Span::styled("Admin Panel", Style::default().fg(theme.highlight)));
let title_widget = Paragraph::new(title).alignment(Alignment::Center);
f.render_widget(title_widget, chunks[0]);
// Content
let content_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
.split(chunks[1]);
// Profile list
let items: Vec<ListItem> = self.profiles.iter()
.map(|p| ListItem::new(Line::from(vec![
// Profile list - Use data from admin_state
let items: Vec<ListItem> = admin_state
.profiles // Use profiles from the persistent state
.iter()
.map(|p| {
ListItem::new(Line::from(vec![
Span::styled(
// Check against the globally selected profile for the checkmark
if Some(p) == selected_profile.as_ref() { "" } else { " " },
Style::default().fg(theme.accent)
Style::default().fg(theme.accent),
),
Span::styled(p, Style::default().fg(theme.fg)),
])))
.collect();
]))
})
.collect();
let list = List::new(items)
.block(Block::default().title("Profiles"))
.highlight_style(Style::default().bg(theme.highlight).fg(theme.bg));
f.render_stateful_widget(list, content_chunks[0], &mut self.list_state);
let list = List::new(items)
.block(Block::default().title("Profiles"))
.highlight_style(Style::default().bg(theme.highlight).fg(theme.bg));
// Profile details
if let Some(profile) = self.list_state.selected()
.and_then(|i| profile_tree.profiles.get(i))
{
let mut text = Text::default();
text.lines.push(Line::from(vec![
Span::styled("Profile: ", Style::default().fg(theme.accent)),
Span::styled(&profile.name, Style::default().fg(theme.highlight)),
]));
// Render statefully using a CLONE of the list_state from persistent AdminState
// Cloning is necessary because render_stateful_widget needs `&mut ListState`
// but we only have `&AdminState`. This is a common pattern in Ratatui.
let mut list_state_clone = admin_state.list_state.clone();
f.render_stateful_widget(list, content_chunks[0], &mut list_state_clone);
text.lines.push(Line::from(""));
text.lines.push(Line::from(Span::styled("Tables:", Style::default().fg(theme.accent))));
for table in &profile.tables {
let mut line = vec![Span::styled(format!("├─ {}", table.name), theme.fg)];
if !table.depends_on.is_empty() {
line.push(Span::styled(
format!("{}", table.depends_on.join(", ")),
Style::default().fg(theme.secondary)
));
}
text.lines.push(Line::from(line));
// Profile details - Use selection info from admin_state
if let Some(profile) = admin_state
.get_selected_index() // Use the method from persistent state
.and_then(|i| profile_tree.profiles.get(i))
{
let mut text = Text::default();
text.lines.push(Line::from(vec![
Span::styled("Profile: ", Style::default().fg(theme.accent)),
Span::styled(&profile.name, Style::default().fg(theme.highlight)),
]));
text.lines.push(Line::from(""));
text.lines.push(Line::from(Span::styled(
"Tables:",
Style::default().fg(theme.accent),
)));
for table in &profile.tables {
let mut line = vec![Span::styled(format!("├─ {}", table.name), theme.fg)];
if !table.depends_on.is_empty() {
line.push(Span::styled(
format!("{}", table.depends_on.join(", ")),
Style::default().fg(theme.secondary),
));
}
let details_widget = Paragraph::new(text)
.block(Block::default().title("Details"));
f.render_widget(details_widget, content_chunks[1]);
text.lines.push(Line::from(line));
}
let details_widget = Paragraph::new(text)
.block(Block::default().title("Details"))
.wrap(Wrap { trim: true }); // Add wrapping
f.render_widget(details_widget, content_chunks[1]);
}
}

View File

@@ -77,7 +77,7 @@ pub fn render_login(
// Login Button
let login_button_index = 0;
let login_active = if app_state.ui.focus_outside_canvas {
app_state.general.selected_item == login_button_index
app_state.focused_button_index== login_button_index
} else {
false
};
@@ -104,7 +104,7 @@ pub fn render_login(
// Return Button
let return_button_index = 1; // Assuming Return is the second general element
let return_active = if app_state.ui.focus_outside_canvas {
app_state.general.selected_item == return_button_index
app_state.focused_button_index== return_button_index
} else {
false // Not active if focus is in canvas or other modes
};

View File

@@ -11,7 +11,7 @@ use crate::{
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect, Margin},
style::{Style, Modifier, Color},
widgets::{Block, BorderType, Borders, Paragraph, Wrap},
widgets::{Block, BorderType, Borders, Paragraph},
Frame,
};
@@ -92,7 +92,7 @@ pub fn render_register(
// Register Button
let register_button_index = 0;
let register_active = if app_state.ui.focus_outside_canvas {
app_state.general.selected_item == register_button_index
app_state.focused_button_index== register_button_index
} else {
false
};
@@ -119,7 +119,7 @@ pub fn render_register(
// Return Button (logic remains similar)
let return_button_index = 1;
let return_active = if app_state.ui.focus_outside_canvas {
app_state.general.selected_item == return_button_index
app_state.focused_button_index== return_button_index
} else {
false
};

View File

@@ -9,8 +9,6 @@ use ratatui::{
};
use crate::config::colors::themes::Theme;
use crate::state::pages::canvas_state::CanvasState;
use crate::components::common::autocomplete;
use crate::components::render_autocomplete_dropdown;
pub fn render_canvas(
f: &mut Frame,

View File

@@ -8,103 +8,80 @@ use ratatui::{
Frame,
};
use crate::config::colors::themes::Theme;
use crate::state::pages::intro::IntroState;
pub struct IntroState {
pub selected_option: usize,
pub fn render_intro(f: &mut Frame, intro_state: &IntroState, area: Rect, theme: &Theme) {
let block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(theme.accent))
.style(Style::default().bg(theme.bg));
let inner_area = block.inner(area);
f.render_widget(block, area);
// Center layout
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Percentage(40),
Constraint::Length(5),
Constraint::Percentage(40),
])
.split(inner_area);
// Title
let title = Line::from(vec![
Span::styled("multieko2", Style::default().fg(theme.highlight)),
Span::styled(" v", Style::default().fg(theme.fg)),
Span::styled(env!("CARGO_PKG_VERSION"), Style::default().fg(theme.secondary)),
]);
let title_para = Paragraph::new(title)
.alignment(Alignment::Center);
f.render_widget(title_para, chunks[1]);
// Buttons - now with 4 options
let button_area = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Percentage(25),
Constraint::Percentage(25),
Constraint::Percentage(25),
Constraint::Percentage(25),
])
.split(chunks[1].inner(Margin {
horizontal: 1,
vertical: 1
}));
let buttons = ["Continue", "Admin", "Login", "Register"];
for (i, &text) in buttons.iter().enumerate() {
render_button(f, button_area[i], text, intro_state.selected_option == i, theme);
}
}
impl IntroState {
pub fn new() -> Self {
Self { selected_option: 0 }
}
fn render_button(f: &mut Frame, area: Rect, text: &str, selected: bool, theme: &Theme) {
let button_style = Style::default()
.fg(if selected { theme.highlight } else { theme.fg })
.bg(theme.bg)
.add_modifier(if selected {
ratatui::style::Modifier::BOLD
} else {
ratatui::style::Modifier::empty()
});
pub fn render(&self, f: &mut Frame, area: Rect, theme: &Theme) {
let block = Block::default()
let border_style = Style::default()
.fg(if selected { theme.accent } else { theme.border });
let button = Paragraph::new(text)
.style(button_style)
.alignment(Alignment::Center)
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(theme.accent))
.style(Style::default().bg(theme.bg));
.border_type(BorderType::Double)
.border_style(border_style),
);
let inner_area = block.inner(area);
f.render_widget(block, area);
// Center layout
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Percentage(40),
Constraint::Length(5),
Constraint::Percentage(40),
])
.split(inner_area);
// Title
let title = Line::from(vec![
Span::styled("multieko2", Style::default().fg(theme.highlight)),
Span::styled(" v", Style::default().fg(theme.fg)),
Span::styled(env!("CARGO_PKG_VERSION"), Style::default().fg(theme.secondary)),
]);
let title_para = Paragraph::new(title)
.alignment(Alignment::Center);
f.render_widget(title_para, chunks[1]);
// Buttons - now with 4 options
let button_area = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Percentage(25),
Constraint::Percentage(25),
Constraint::Percentage(25),
Constraint::Percentage(25),
])
.split(chunks[1].inner(Margin {
horizontal: 1,
vertical: 1
}));
let buttons = ["Continue", "Admin", "Login", "Register"];
for (i, &text) in buttons.iter().enumerate() {
self.render_button(
f,
button_area[i],
text,
self.selected_option == i,
theme,
);
}
}
fn render_button(&self, f: &mut Frame, area: Rect, text: &str, selected: bool, theme: &Theme) {
let button_style = Style::default()
.fg(if selected { theme.highlight } else { theme.fg })
.bg(theme.bg)
.add_modifier(if selected {
ratatui::style::Modifier::BOLD
} else {
ratatui::style::Modifier::empty()
});
let border_style = Style::default()
.fg(if selected { theme.accent } else { theme.border });
let button = Paragraph::new(text)
.style(button_style)
.alignment(Alignment::Center)
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Double)
.border_style(border_style),
);
f.render_widget(button, area);
}
pub fn next_option(&mut self) {
self.selected_option = (self.selected_option + 1) % 4;
}
pub fn previous_option(&mut self) {
self.selected_option = if self.selected_option == 0 { 3 } else { self.selected_option - 1 };
}
f.render_widget(button, area);
}

View File

@@ -6,4 +6,3 @@ pub mod navigation;
pub use read_only::*;
pub use edit::*;
pub use navigation::*;

View File

@@ -1,3 +1,3 @@
// src/functions/modes/navigation.rs
pub mod admin_nav;
// pub mod admin_nav;

View File

@@ -1,36 +1 @@
// src/functions/modes/navigation/admin_nav.rs
use crate::state::app::state::AppState;
use crate::state::pages::admin::AdminState;
/// Handles moving the selection up in the admin profile list.
pub fn move_admin_list_up(app_state: &AppState, admin_state: &mut AdminState) {
// Read profile count directly from app_state where the source data lives
let profile_count = app_state.profile_tree.profiles.len();
if profile_count == 0 {
admin_state.list_state.select(None); // Ensure nothing selected if empty
return;
}
let current_index = admin_state.get_selected_index().unwrap_or(0);
let new_index = if current_index == 0 {
profile_count - 1 // Wrap to end
} else {
current_index.saturating_sub(1) // Move up
};
admin_state.list_state.select(Some(new_index));
}
/// Handles moving the selection down in the admin profile list.
pub fn move_admin_list_down(app_state: &AppState, admin_state: &mut AdminState) {
// Read profile count directly from app_state
let profile_count = app_state.profile_tree.profiles.len();
if profile_count == 0 {
admin_state.list_state.select(None); // Ensure nothing selected if empty
return;
}
let current_index = admin_state.get_selected_index().unwrap_or(0);
let new_index = (current_index + 1) % profile_count; // Wrap around
admin_state.list_state.select(Some(new_index));
}

View File

@@ -60,7 +60,7 @@ pub async fn execute_action<S: CanvasState>(
if current_field == last_field_index {
// Already on the last field, move focus outside
app_state.ui.focus_outside_canvas = true;
app_state.general.selected_item = 0; // Focus first general item (e.g., Login button)
app_state.focused_button_index= 0;
key_sequence_tracker.reset();
Ok("Focus moved below canvas".to_string())
} else {

View File

@@ -4,7 +4,6 @@ use crate::config::binds::config::Config;
use crate::config::binds::key_sequences::KeySequenceTracker;
use crate::services::grpc_client::GrpcClient;
use crate::state::pages::{canvas_state::CanvasState, auth::RegisterState};
use crate::state::pages::auth::AuthState;
use crate::state::pages::auth::LoginState;
use crate::state::pages::form::FormState;
use crate::state::app::state::AppState;

View File

@@ -7,7 +7,6 @@ use crate::state::app::state::AppState;
use crate::state::pages::auth::AuthState;
use crate::state::pages::auth::LoginState;
use crate::state::pages::auth::RegisterState;
use crate::services::auth::AuthClient;
use crate::modes::handlers::event::EventOutcome;
use crate::tui::functions::common::{login, register};

View File

@@ -6,11 +6,11 @@ use crate::state::app::state::AppState;
use crate::state::pages::form::FormState;
use crate::state::pages::auth::LoginState;
use crate::state::pages::auth::RegisterState;
use crate::state::pages::intro::IntroState;
use crate::state::pages::admin::AdminState;
use crate::state::pages::canvas_state::CanvasState;
use crate::ui::handlers::context::UiContext;
use crate::modes::handlers::event::EventOutcome;
use crate::functions::modes::navigation::admin_nav;
pub async fn handle_navigation_event(
key: KeyEvent,
@@ -19,6 +19,7 @@ pub async fn handle_navigation_event(
app_state: &mut AppState,
login_state: &mut LoginState,
register_state: &mut RegisterState,
intro_state: &mut IntroState,
admin_state: &mut AdminState,
command_mode: &mut bool,
command_input: &mut String,
@@ -27,19 +28,19 @@ pub async fn handle_navigation_event(
if let Some(action) = config.get_general_action(key.code, key.modifiers) {
match action {
"move_up" => {
move_up(app_state, login_state, register_state, admin_state);
move_up(app_state, login_state, register_state, intro_state, admin_state);
return Ok(EventOutcome::Ok(String::new()));
}
"move_down" => {
move_down(app_state, admin_state);
move_down(app_state, intro_state, admin_state);
return Ok(EventOutcome::Ok(String::new()));
}
"next_option" => {
next_option(app_state);
next_option(app_state, intro_state);
return Ok(EventOutcome::Ok(String::new()));
}
"previous_option" => {
previous_option(app_state);
previous_option(app_state, intro_state);
return Ok(EventOutcome::Ok(String::new()));
}
"toggle_sidebar" => {
@@ -62,17 +63,16 @@ pub async fn handle_navigation_event(
}
"select" => {
let (context, index) = if app_state.ui.show_intro {
(UiContext::Intro, app_state.ui.intro_state.selected_option)
(UiContext::Intro, intro_state.selected_option)
} else if app_state.ui.show_login && app_state.ui.focus_outside_canvas {
(UiContext::Login, app_state.general.selected_item)
(UiContext::Login, app_state.focused_button_index)
} else if app_state.ui.show_register && app_state.ui.focus_outside_canvas {
(UiContext::Register, app_state.general.selected_item)
(UiContext::Register, app_state.focused_button_index)
} else if app_state.ui.show_admin {
(UiContext::Admin, app_state.general.selected_item)
(UiContext::Admin, admin_state.get_selected_index().unwrap_or(0))
} else if app_state.ui.dialog.dialog_show {
(UiContext::Dialog, app_state.ui.dialog.dialog_active_button_index)
} else {
// Handle cases where select is pressed but no button context applies
return Ok(EventOutcome::Ok("Select (No Action)".to_string()));
};
return Ok(EventOutcome::ButtonSelected { context, index });
@@ -83,9 +83,9 @@ pub async fn handle_navigation_event(
Ok(EventOutcome::Ok(String::new()))
}
pub fn move_up(app_state: &mut AppState, login_state: &mut LoginState, register_state: &mut RegisterState, admin_state: &mut AdminState) {
pub fn move_up(app_state: &mut AppState, login_state: &mut LoginState, register_state: &mut RegisterState, intro_state: &mut IntroState, admin_state: &mut AdminState) {
if app_state.ui.focus_outside_canvas && app_state.ui.show_login || app_state.ui.show_register{
if app_state.general.selected_item == 0 {
if app_state.focused_button_index == 0 {
app_state.ui.focus_outside_canvas = false;
if app_state.ui.show_login {
let last_field_index = login_state.fields().len().saturating_sub(1);
@@ -95,53 +95,47 @@ pub fn move_up(app_state: &mut AppState, login_state: &mut LoginState, register_
register_state.set_current_field(last_field_index);
}
} else {
app_state.general.selected_item = app_state.general.selected_item.saturating_sub(1);
app_state.focused_button_index = app_state.focused_button_index.saturating_sub(1);
}
} else if app_state.ui.show_intro {
app_state.ui.intro_state.previous_option();
intro_state.previous_option();
} else if app_state.ui.show_admin {
admin_nav::move_admin_list_up(app_state, admin_state);
admin_state.previous();
}
}
pub fn move_down(app_state: &mut AppState, admin_state: &mut AdminState) {
pub fn move_down(app_state: &mut AppState, intro_state: &mut IntroState, admin_state: &mut AdminState) {
if app_state.ui.focus_outside_canvas && app_state.ui.show_login || app_state.ui.show_register {
let num_general_elements = 2;
if app_state.general.selected_item < num_general_elements - 1 {
app_state.general.selected_item += 1;
if app_state.focused_button_index < num_general_elements - 1 {
app_state.focused_button_index += 1;
}
} else if app_state.ui.show_intro {
app_state.ui.intro_state.next_option();
intro_state.next_option();
} else if app_state.ui.show_admin {
// Assuming profile_tree.profiles is the list we're navigating
let profile_count = app_state.profile_tree.profiles.len();
if profile_count == 0 {
return;
}
admin_nav::move_admin_list_down(app_state, admin_state);
admin_state.next();
}
}
pub fn next_option(app_state: &mut AppState) { // Remove option_count parameter
pub fn next_option(app_state: &mut AppState, intro_state: &mut IntroState) {
if app_state.ui.show_intro {
app_state.ui.intro_state.next_option();
intro_state.next_option();
} else {
// Get option count from state instead of parameter
let option_count = app_state.profile_tree.profiles.len();
app_state.general.current_option = (app_state.general.current_option + 1) % option_count;
app_state.focused_button_index = (app_state.focused_button_index + 1) % option_count;
}
}
pub fn previous_option(app_state: &mut AppState) {
pub fn previous_option(app_state: &mut AppState, intro_state: &mut IntroState) {
if app_state.ui.show_intro {
app_state.ui.intro_state.previous_option();
intro_state.previous_option();
} else {
let option_count = app_state.profile_tree.profiles.len();
app_state.general.current_option = if app_state.general.current_option == 0 {
option_count.saturating_sub(1) // Wrap to last option
app_state.focused_button_index = if app_state.focused_button_index == 0 {
option_count.saturating_sub(1)
} else {
app_state.general.current_option - 1
app_state.focused_button_index - 1
};
}
}

View File

@@ -10,6 +10,7 @@ use crate::state::pages::form::FormState;
use crate::state::pages::auth::AuthState;
use crate::state::pages::auth::LoginState;
use crate::state::pages::auth::RegisterState;
use crate::state::pages::intro::IntroState;
use crate::state::pages::admin::AdminState;
use crate::state::app::state::AppState;
use crate::state::pages::canvas_state::CanvasState;
@@ -70,6 +71,7 @@ impl EventHandler {
auth_state: &mut AuthState,
login_state: &mut LoginState,
register_state: &mut RegisterState,
intro_state: &mut IntroState,
admin_state: &mut AdminState,
app_state: &mut AppState,
total_count: u64,
@@ -109,6 +111,7 @@ impl EventHandler {
app_state,
login_state,
register_state,
intro_state,
admin_state,
&mut self.command_mode,
&mut self.command_input,
@@ -119,7 +122,13 @@ impl EventHandler {
let mut message = String::from("Selected"); // Default message
match context {
UiContext::Intro => {
intro::handle_intro_selection(app_state, index); // Pass index
intro::handle_intro_selection(app_state, index);
if app_state.ui.show_admin {
let profile_names = app_state.profile_tree.profiles.iter()
.map(|p| p.name.clone())
.collect();
admin_state.set_profiles(profile_names);
}
message = format!("Intro Option {} selected", index);
}
UiContext::Login => {
@@ -137,7 +146,7 @@ impl EventHandler {
};
}
UiContext::Admin => {
admin::handle_admin_selection(app_state);
admin::handle_admin_selection(app_state, admin_state);
message = format!("Admin Option {} selected", index);
}

View File

@@ -2,7 +2,6 @@
use std::env;
use common::proto::multieko2::table_definition::ProfileTreeResponse;
use crate::components::IntroState;
use crate::modes::handlers::mode_manager::AppMode;
use crate::ui::handlers::context::DialogPurpose;
@@ -22,7 +21,6 @@ pub struct UiState {
pub show_form: bool,
pub show_login: bool,
pub show_register: bool,
pub intro_state: IntroState,
pub focus_outside_canvas: bool,
pub dialog: DialogState,
}
@@ -35,6 +33,7 @@ pub struct AppState {
pub profile_tree: ProfileTreeResponse,
pub selected_profile: Option<String>,
pub current_mode: AppMode,
pub focused_button_index: usize,
// UI preferences
pub ui: UiState,
@@ -52,6 +51,7 @@ impl AppState {
profile_tree: ProfileTreeResponse::default(),
selected_profile: None,
current_mode: AppMode::General,
focused_button_index: 0,
ui: UiState::default(),
})
}
@@ -136,7 +136,6 @@ impl Default for UiState {
show_form: false,
show_login: false,
show_register: false,
intro_state: IntroState::new(),
focus_outside_canvas: false,
dialog: DialogState::default(),
}

View File

@@ -3,4 +3,5 @@
pub mod form;
pub mod auth;
pub mod admin;
pub mod intro;
pub mod canvas_state;

View File

@@ -34,5 +34,32 @@ impl AdminState {
self.list_state.select(new_selection);
}
}
/// Selects the next profile in the list, wrapping around.
pub fn next(&mut self) {
if self.profiles.is_empty() {
self.list_state.select(None);
return;
}
let i = match self.list_state.selected() {
Some(i) => if i >= self.profiles.len() - 1 { 0 } else { i + 1 },
None => 0,
};
self.list_state.select(Some(i));
}
/// Selects the previous profile in the list, wrapping around.
pub fn previous(&mut self) {
if self.profiles.is_empty() {
self.list_state.select(None);
return;
}
let i = match self.list_state.selected() {
Some(i) => if i == 0 { self.profiles.len() - 1 } else { i - 1 },
None => self.profiles.len() - 1,
};
self.list_state.select(Some(i));
}
}

View File

@@ -0,0 +1,25 @@
// src/state/pages/intro.rs
#[derive(Default, Clone, Debug)]
pub struct IntroState {
pub selected_option: usize,
}
impl IntroState {
pub fn new() -> Self {
Self::default()
}
pub fn next_option(&mut self) {
if self.selected_option < 3 {
self.selected_option += 1;
}
}
pub fn previous_option(&mut self) {
if self.selected_option > 0 {
self.selected_option -= 1
}
}
}

View File

@@ -1,8 +1,11 @@
use crate::state::app::state::AppState;
use crate::state::pages::admin::AdminState;
pub fn handle_admin_selection(app_state: &mut AppState) {
pub fn handle_admin_selection(app_state: &mut AppState, admin_state: &AdminState) {
let profiles = &app_state.profile_tree.profiles;
if !profiles.is_empty() && app_state.general.selected_item < profiles.len() {
app_state.selected_profile = Some(profiles[app_state.general.selected_item].name.clone());
if let Some(selected_index) = admin_state.get_selected_index() {
if let Some(profile) = profiles.get(selected_index) {
app_state.selected_profile = Some(profile.name.clone());
}
}
}

View File

@@ -103,7 +103,7 @@ pub async fn back_to_main(
// Reset focus state
app_state.ui.focus_outside_canvas = false;
app_state.general.selected_item = 0;
app_state.focused_button_index= 0;
"Returned to main menu".to_string()
}

View File

@@ -147,7 +147,7 @@ pub async fn back_to_main(
// Reset focus state
app_state.ui.focus_outside_canvas = false;
app_state.general.selected_item = 0; // Reset intro selection
app_state.focused_button_index = 0;
"Returned to main menu".to_string()
}

View File

@@ -1,6 +1,7 @@
// src/tui/functions/intro.rs
use crate::state::app::state::AppState;
pub fn handle_intro_selection(app_state: &mut AppState, index: usize) { // Add index parameter
pub fn handle_intro_selection(app_state: &mut AppState, index: usize) {
match index { // Use index directly
0 => { // Continue
app_state.ui.show_form = true;
@@ -21,7 +22,7 @@ pub fn handle_intro_selection(app_state: &mut AppState, index: usize) { // Add i
app_state.ui.show_intro = false;
app_state.ui.show_register = true;
app_state.ui.focus_outside_canvas = false;
app_state.general.selected_item = 0;
app_state.focused_button_index = 0;
}
_ => {}
}

View File

@@ -4,9 +4,9 @@ use crate::components::{
render_background,
render_command_line,
render_status_line,
intro::intro::render_intro,
handlers::sidebar::{self, calculate_sidebar_layout},
form::form::render_form,
admin::{admin_panel::AdminPanelState},
auth::{login::render_login, register::render_register},
};
use crate::config::colors::themes::Theme;
@@ -16,6 +16,7 @@ use crate::state::pages::form::FormState;
use crate::state::pages::auth::AuthState;
use crate::state::pages::auth::LoginState;
use crate::state::pages::auth::RegisterState;
use crate::state::pages::intro::IntroState;
use crate::state::app::state::AppState;
use crate::state::pages::admin::AdminState;
@@ -25,6 +26,8 @@ pub fn render_ui(
auth_state: &mut AuthState,
login_state: &LoginState,
register_state: &RegisterState,
intro_state: &IntroState,
admin_state: &mut AdminState,
theme: &Theme,
is_edit_mode: bool,
total_count: u64,
@@ -48,7 +51,7 @@ pub fn render_ui(
let main_content_area = root[0];
if app_state.ui.show_intro {
app_state.ui.intro_state.render(f, main_content_area, theme);
render_intro(f, intro_state, main_content_area, theme);
} else if app_state.ui.show_register {
render_register(
f,
@@ -68,24 +71,9 @@ pub fn render_ui(
login_state.current_field < 2
);
} else if app_state.ui.show_admin {
// Create temporary AdminPanelState for rendering
let mut admin_state = AdminPanelState::new(
app_state.profile_tree.profiles
.iter()
.map(|p| p.name.clone())
.collect()
);
// Set the selected item - FIXED
if !admin_state.profiles.is_empty() {
let selected_index = admin_state.get_selected_index()
.unwrap_or(0)
.min(admin_state.profiles.len() - 1);
admin_state.list_state.select(Some(selected_index));
}
admin_state.render(
crate::components::admin::admin_panel::render_admin_panel(
f,
admin_state,
main_content_area,
theme,
&app_state.profile_tree,

View File

@@ -13,6 +13,7 @@ use crate::state::pages::auth::AuthState;
use crate::state::pages::auth::LoginState;
use crate::state::pages::auth::RegisterState;
use crate::state::pages::admin::AdminState;
use crate::state::pages::intro::IntroState;
use crate::state::app::state::AppState;
// Import SaveOutcome
use crate::tui::terminal::{EventReader, TerminalCore};
@@ -21,30 +22,27 @@ use crossterm::cursor::SetCursorStyle;
pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
let config = Config::load()?;
let theme = Theme::from_str(&config.colors.theme);
let mut terminal = TerminalCore::new()?;
let mut grpc_client = GrpcClient::new().await?;
let mut command_handler = CommandHandler::new();
let theme = Theme::from_str(&config.colors.theme);
let mut auth_state = AuthState::default();
let mut register_state = RegisterState::default();
let mut login_state = LoginState::default();
let mut admin_state = AdminState::default();
// Initialize app_state first
let mut event_handler = EventHandler::new().await?;
let event_reader = EventReader::new();
let mut auth_state = AuthState::default();
let mut login_state = LoginState::default();
let mut register_state = RegisterState::default();
let mut intro_state = IntroState::default();
let mut admin_state = AdminState::default();
let mut app_state = AppState::new()?;
// Initialize app state with profile tree and table structure
let column_names =
UiService::initialize_app_state(&mut grpc_client, &mut app_state)
.await?;
// Initialize FormState with dynamic fields
let mut form_state = FormState::new(column_names);
// Initialize EventHandler (which now contains AuthClient)
let mut event_handler = EventHandler::new().await?;
let event_reader = EventReader::new();
// Fetch the total count of Adresar entries
UiService::initialize_adresar_count(&mut grpc_client, &mut app_state)
.await?;
@@ -61,6 +59,8 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
&mut auth_state,
&login_state,
&register_state,
&intro_state,
&mut admin_state,
&theme,
is_edit_mode,
app_state.total_count,
@@ -120,6 +120,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
&mut auth_state,
&mut login_state,
&mut register_state,
&mut intro_state,
&mut admin_state,
&mut app_state,
total_count,