Files
komp_ac/client/src/components/admin/admin_panel.rs
2025-04-14 12:07:15 +02:00

107 lines
4.1 KiB
Rust

// 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::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Style, Modifier}, // Added Modifier
text::{Line, Span, Text},
widgets::{Block, BorderType, Borders, List, ListItem, Paragraph}, // Removed ListState import here
Frame,
};
// 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));
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 - 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),
),
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));
// 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);
// 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),
));
}
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]);
}
}