working admin panel, needs to do buttons for navigation next

This commit is contained in:
filipriec
2025-04-16 21:22:34 +02:00
parent 51ab73014f
commit 6505e18b0b
3 changed files with 118 additions and 72 deletions

View File

@@ -19,19 +19,27 @@ pub fn render_admin_panel_admin(
admin_state: &mut AdminState,
theme: &Theme,
) {
// Split the area into three panes: Profiles | Tables | Dependencies
let chunks = Layout::default()
// Split vertically: Top for panes, Bottom for buttons
let main_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(0), Constraint::Length(1)].as_ref()) // 1 line for buttons
.split(area);
let panes_area = main_chunks[0];
let buttons_area = main_chunks[1];
// Split the top area into three panes: Profiles | Tables | Dependencies
let pane_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Percentage(25), // Profiles
Constraint::Percentage(40), // Tables
Constraint::Percentage(35), // Dependencies
].as_ref())
.split(area); // Use the whole area directly
.split(panes_area); // Use the whole area directly
let profiles_pane = chunks[0];
let tables_pane = chunks[1];
let deps_pane = chunks[2];
let profiles_pane = pane_chunks[0];
let tables_pane = pane_chunks[1];
let deps_pane = pane_chunks[2];
// --- Profiles Pane (Left) ---
let profile_focus = admin_state.current_focus == AdminFocus::Profiles;
@@ -56,10 +64,11 @@ pub fn render_admin_panel_admin(
.profiles
.iter()
.enumerate()
.map(|(i, profile)| {
// THIS line checks the selection state for the profile list
let is_selected = admin_state.profile_list_state.selected() == Some(i);
let prefix = if is_selected { "[*] " } else { "[ ] " }; // Use [*] for selected
.map(|(idx, profile)| {
// Check persistent selection for prefix, navigation state for style/highlight
let is_selected = admin_state.selected_profile_index == Some(idx); // Use persistent state for [*]
let is_navigated = admin_state.profile_list_state.selected() == Some(idx); // Use nav state for highlight/>
let prefix = if is_selected { "[*] " } else { "[ ] " };
let style = if is_selected { // Style based on selection too
Style::default().fg(theme.highlight).add_modifier(ratatui::style::Modifier::BOLD)
} else {
@@ -74,7 +83,8 @@ pub fn render_admin_panel_admin(
// Build and render profile list inside the block's inner area
let profile_list = List::new(profile_list_items)
.highlight_style(if profile_focus {
// Highlight style depends on focus AND navigation state
.highlight_style(if profile_focus { // Use focus state
Style::default().add_modifier(ratatui::style::Modifier::REVERSED)
} else {
Style::default()
@@ -92,11 +102,11 @@ pub fn render_admin_panel_admin(
};
// Get selected profile information
let selected_profile_idx = admin_state.profile_list_state.selected();
let navigated_profile_idx = admin_state.profile_list_state.selected(); // Use nav state for display
let selected_profile_name = app_state
.profile_tree
.profiles
.get(selected_profile_idx.unwrap_or(usize::MAX)) // Use index, provide default if None
.get(navigated_profile_idx.unwrap_or(usize::MAX)) // Use nav state for title
.map_or("None", |p| &p.name);
// Block for the tables pane
@@ -110,17 +120,18 @@ pub fn render_admin_panel_admin(
// Create table list items and get dependencies for the selected table
let (table_list_items, selected_table_deps): (Vec<ListItem>, Vec<String>) = if let Some(
profile,
) = selected_profile_idx.and_then(|idx| app_state.profile_tree.profiles.get(idx)) {
profile, // Get profile based on NAVIGATED profile index
) = navigated_profile_idx.and_then(|idx| app_state.profile_tree.profiles.get(idx)) {
let items: Vec<ListItem> = profile
.tables
.iter()
.enumerate()
.map(|(idx, table)| {
let is_navigated = admin_state.table_list_state.selected() == Some(idx);
let prefix = if admin_state.is_table_selected(idx) { "[*] " } else { "[ ] " };
// Don't show dependencies inline anymore
let style = if is_navigated {
.map(|(idx, table)| { // Renamed i to idx for clarity
// Check persistent selection for prefix, navigation state for style/highlight
let is_selected = admin_state.selected_table_index == Some(idx); // Use persistent state for [*]
let is_navigated = admin_state.table_list_state.selected() == Some(idx); // Use nav state for highlight/>
let prefix = if is_selected { "[*] " } else { "[ ] " };
let style = if is_navigated { // Style based on navigation highlight
Style::default().fg(theme.highlight).add_modifier(ratatui::style::Modifier::BOLD)
} else {
Style::default().fg(theme.fg)
@@ -132,10 +143,12 @@ pub fn render_admin_panel_admin(
})
.collect();
// Get dependencies only for the currently selected/highlighted table
let deps = admin_state.table_list_state.selected()
.and_then(|idx| profile.tables.get(idx))
.map_or(vec![], |t| t.depends_on.clone());
// Get dependencies only for the PERSISTENTLY selected table in the PERSISTENTLY selected profile
let chosen_profile_idx = admin_state.selected_profile_index; // Use persistent profile selection
let deps = chosen_profile_idx // Start with the chosen profile index
.and_then(|p_idx| app_state.profile_tree.profiles.get(p_idx)) // Get the chosen profile
.and_then(|p| admin_state.selected_table_index.and_then(|t_idx| p.tables.get(t_idx))) // Get the chosen table using its index
.map_or(Vec::new(), |t| t.depends_on.clone()); // If found, clone its depends_on, otherwise return empty Vec
(items, deps)
} else {
@@ -145,7 +158,8 @@ pub fn render_admin_panel_admin(
// Build and render table list inside the block's inner area
let table_list = List::new(table_list_items)
.highlight_style(if table_focus {
// Highlight style depends on focus AND navigation state
.highlight_style(if table_focus { // Use focus state
Style::default().add_modifier(ratatui::style::Modifier::REVERSED)
} else {
Style::default()
@@ -155,9 +169,11 @@ pub fn render_admin_panel_admin(
f.render_stateful_widget(table_list, tables_inner_area, &mut admin_state.table_list_state);
// --- Dependencies Pane (Right) ---
let selected_table_name = selected_profile_idx
// Get name based on PERSISTENT selections
let chosen_profile_idx = admin_state.selected_profile_index; // Use persistent profile selection
let selected_table_name = chosen_profile_idx
.and_then(|p_idx| app_state.profile_tree.profiles.get(p_idx))
.and_then(|p| admin_state.table_list_state.selected().and_then(|t_idx| p.tables.get(t_idx)))
.and_then(|p| admin_state.selected_table_index.and_then(|t_idx| p.tables.get(t_idx))) // Use persistent table selection
.map_or("N/A", |t| &t.name); // Get name of the selected table
// Block for the dependencies pane
@@ -189,5 +205,22 @@ pub fn render_admin_panel_admin(
// Build and render dependencies paragraph inside the block's inner area
let deps_paragraph = Paragraph::new(deps_content);
f.render_widget(deps_paragraph, deps_inner_area);
}
// --- Buttons Row ---
let button_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Percentage(33),
Constraint::Percentage(34),
Constraint::Percentage(33),
].as_ref())
.split(buttons_area);
let btn_style = Style::default().fg(theme.secondary); // Style for button text
let btn1 = Paragraph::new("Add Logic").style(btn_style).alignment(Alignment::Center);
let btn2 = Paragraph::new("Add Table").style(btn_style).alignment(Alignment::Center);
let btn3 = Paragraph::new("Change Table").style(btn_style).alignment(Alignment::Center);
f.render_widget(btn1, button_chunks[0]);
f.render_widget(btn2, button_chunks[1]);
f.render_widget(btn3, button_chunks[2]);
}