Compare commits

..

4 Commits

Author SHA1 Message Date
filipriec
fe2d1e4684 working perfectly well, needs bug fixes to proper shortcuts to proper windows 2025-03-22 20:10:39 +01:00
filipriec
7d4b043d63 still one error missing 2025-03-22 20:05:13 +01:00
filipriec
04b4220c76 needs bug fixing 2025-03-22 19:37:41 +01:00
filipriec
841418759b last changes 2025-03-22 16:26:38 +01:00
5 changed files with 163 additions and 57 deletions

View File

@@ -1,21 +1,49 @@
// src/components/admin/admin_panel.rs
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
widgets::{Block, BorderType, Borders, List, ListItem, ListState, Paragraph},
style::Style,
text::{Line, Span},
widgets::{Block, BorderType, Borders, Paragraph},
text::{Line, Span, Text},
layout::{Alignment, Constraint, Direction, Layout, Rect},
Frame,
};
use common::proto::multieko2::table_definition::ProfileTreeResponse;
use crate::config::colors::Theme;
pub struct AdminPanelState;
pub struct AdminPanelState {
pub list_state: ListState,
pub profiles: Vec<String>,
}
impl AdminPanelState {
pub fn new() -> Self {
Self
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 }
}
pub fn render(&self, f: &mut Frame, area: Rect, theme: &Theme) {
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));
}
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));
}
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)
@@ -27,18 +55,64 @@ impl AdminPanelState {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(3), // Title
Constraint::Min(1), // Content
])
.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_para = Paragraph::new(title)
.alignment(Alignment::Center);
f.render_widget(title_para, chunks[0]);
let title_widget = Paragraph::new(title).alignment(Alignment::Center);
f.render_widget(title_widget, chunks[0]);
let content = Paragraph::new("Admin panel content goes here");
f.render_widget(content, chunks[1]);
// 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![
Span::styled(
if Some(p) == selected_profile.as_ref() { "" } else { " " },
Style::default().fg(theme.accent)
),
Span::styled(p, Style::default().fg(theme.fg)),
])))
.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);
// 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)),
]));
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));
}
let details_widget = Paragraph::new(text)
.block(Block::default().title("Details"));
f.render_widget(details_widget, content_chunks[1]);
}
}
}

View File

@@ -26,41 +26,51 @@ pub fn calculate_sidebar_layout(show_sidebar: bool, main_content_area: Rect) ->
}
}
pub fn render_sidebar(f: &mut Frame, area: Rect, theme: &Theme, profile_tree: &ProfileTreeResponse) {
let sidebar_block = Block::default()
.style(Style::default().bg(theme.bg));
pub fn render_sidebar(
f: &mut Frame,
area: Rect,
theme: &Theme,
profile_tree: &ProfileTreeResponse,
selected_profile: &Option<String>,
) {
let sidebar_block = Block::default().style(Style::default().bg(theme.bg));
let mut items = Vec::new();
for profile in &profile_tree.profiles {
// Profile header
items.push(ListItem::new(Line::from(vec![
Span::styled("📁 ", Style::default().fg(theme.accent)),
Span::styled(&profile.name, Style::default().fg(theme.highlight)),
])));
// Profile tables
for (table_idx, table) in profile.tables.iter().enumerate() {
let is_last_table = table_idx == profile.tables.len() - 1;
let tree_prefix = if is_last_table { "└─ " } else { "├─ " };
// Table name
if let Some(profile_name) = selected_profile {
if let Some(profile) = profile_tree.profiles.iter()
.find(|p| &p.name == profile_name)
{
// Profile header
items.push(ListItem::new(Line::from(vec![
Span::styled(format!(" {}", tree_prefix), Style::default().fg(theme.fg)),
Span::styled(&table.name, Style::default().fg(theme.fg)),
Span::styled("📁 ", Style::default().fg(theme.accent)),
Span::styled(&profile.name, Style::default().fg(theme.highlight)),
])));
// Dependencies
if !table.depends_on.is_empty() {
let dep_prefix = if is_last_table { " " } else { "" };
let deps = table.depends_on.join(", ");
// Tables
for (table_idx, table) in profile.tables.iter().enumerate() {
let is_last = table_idx == profile.tables.len() - 1;
let prefix = if is_last { "└─ " } else { "├─ " };
items.push(ListItem::new(Line::from(vec![
Span::styled(format!(" {} └─ ", dep_prefix), Style::default().fg(theme.secondary)),
Span::styled(format!("{}", deps), Style::default().fg(theme.secondary)),
])));
let mut line = vec![
Span::styled(format!(" {}", prefix), Style::default().fg(theme.fg)),
Span::styled(&table.name, Style::default().fg(theme.fg)),
];
if !table.depends_on.is_empty() {
line.push(Span::styled(
format!("{}", table.depends_on.join(", ")),
Style::default().fg(theme.secondary)
));
}
items.push(ListItem::new(Line::from(line)));
}
}
} else {
items.push(ListItem::new(Span::styled(
"No profile selected",
Style::default().fg(theme.secondary)
)));
}
let list = List::new(items)

View File

@@ -16,6 +16,7 @@ pub struct AppState {
pub total_count: u64,
pub current_position: u64,
pub profile_tree: ProfileTreeResponse,
pub selected_profile: Option<String>,
// UI preferences
pub ui: UiState,
@@ -31,6 +32,7 @@ impl AppState {
total_count: 0,
current_position: 0,
profile_tree: ProfileTreeResponse::default(),
selected_profile: None,
ui: UiState::default(),
})
}

View File

@@ -4,8 +4,9 @@ use crate::components::{
render_background,
render_command_line,
render_status_line,
handlers::{sidebar::{self, calculate_sidebar_layout}, admin_panel::AdminPanelState, form::render_form},
handlers::{sidebar::{self, calculate_sidebar_layout}, form::render_form},
intro::{intro},
admin::{admin_panel::AdminPanelState},
};
use crate::config::colors::Theme;
use ratatui::layout::{Constraint, Direction, Layout};
@@ -26,7 +27,7 @@ pub fn render_ui(
command_message: &str,
app_state: &AppState,
intro_state: &intro::IntroState,
admin_panel_state: &AdminPanelState,
admin_panel_state: &mut AdminPanelState,
) {
render_background(f, f.area(), theme);
@@ -43,7 +44,13 @@ pub fn render_ui(
if app_state.ui.show_intro {
intro_state.render(f, main_content_area, theme);
} else if app_state.ui.show_admin {
admin_panel_state.render(f, main_content_area, theme);
admin_panel_state.render(
f,
main_content_area,
theme,
&app_state.profile_tree,
&app_state.selected_profile,
);
} else {
let (sidebar_area, form_area) = calculate_sidebar_layout(
app_state.ui.show_sidebar,
@@ -51,7 +58,13 @@ pub fn render_ui(
);
if let Some(sidebar_rect) = sidebar_area {
sidebar::render_sidebar(f, sidebar_rect, theme, &app_state.profile_tree);
sidebar::render_sidebar(
f,
sidebar_rect,
theme,
&app_state.profile_tree,
&app_state.selected_profile // Remove trailing comma
);
}
// This change makes the form stay stationary when toggling sidebar

View File

@@ -1,4 +1,4 @@
// src/client/ui/handlers/ui.rs
// src/ui/handlers/ui.rs
use crate::tui::terminal::TerminalCore;
use crate::tui::terminal::GrpcClient;
@@ -9,7 +9,7 @@ use crate::config::config::Config;
use crate::ui::handlers::{form::FormState, render::render_ui};
use crate::modes::handlers::event::EventHandler;
use crate::state::state::AppState;
use crate::components::handlers::{admin_panel::AdminPanelState};
use crate::components::admin::{admin_panel::AdminPanelState};
use crate::components::intro::{intro::IntroState};
pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
@@ -19,10 +19,22 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
let mut command_handler = CommandHandler::new();
let theme = Theme::from_str(&config.colors.theme);
let mut intro_state = IntroState::new();
let admin_panel_state = AdminPanelState::new();
// Initialize app_state first
let mut app_state = AppState::new()?;
// Fetch profile tree and table structure
let profile_tree = grpc_client.get_profile_tree().await?;
app_state.profile_tree = profile_tree;
// Now create admin panel with profiles from app_state
let profiles = app_state.profile_tree.profiles
.iter()
.map(|p| p.name.clone())
.collect();
let mut admin_panel_state = AdminPanelState::new(profiles);
// Fetch table structure at startup (one-time)
// TODO: Later, consider implementing a live update for table structure changes.
let table_structure = grpc_client.get_table_structure().await?;
// Extract the column names from the response
@@ -35,13 +47,9 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
// Initialize FormState with dynamic fields
let mut form_state = FormState::new(column_names);
// Fetch profile tree and table structure
let profile_tree = grpc_client.get_profile_tree().await?;
// The rest of your UI initialization remains the same
let mut event_handler = EventHandler::new();
let event_reader = EventReader::new();
let mut app_state = AppState::new()?;
app_state.profile_tree = profile_tree;
// Fetch the total count of Adresar entries
let total_count = grpc_client.get_adresar_count().await?;
@@ -67,7 +75,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
&event_handler.command_message,
&app_state,
&intro_state,
&admin_panel_state,
&mut admin_panel_state,
);
})?;
@@ -100,7 +108,6 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
};
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
// Ensure position never exceeds total_count + 1
if app_state.current_position > total_count + 1 {
app_state.current_position = total_count + 1;