role restricted admin panel

This commit is contained in:
filipriec
2025-04-14 20:50:02 +02:00
parent e31138c250
commit d892d1cfa0
2 changed files with 62 additions and 57 deletions

View File

@@ -1,24 +1,25 @@
// src/components/admin/admin_panel.rs
use crate::config::colors::themes::Theme;
use crate::state::pages::admin::AdminState; // Import the persistent state
use crate::state::pages::auth::AuthState;
use crate::state::pages::admin::AdminState;
use common::proto::multieko2::table_definition::ProfileTreeResponse;
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::Style, // Added Modifier
style::Style,
text::{Line, Span, Text},
widgets::{Block, BorderType, Borders, List, ListItem, Paragraph, Wrap},
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)
auth_state: &AuthState,
admin_state: &AdminState,
area: Rect,
theme: &Theme,
profile_tree: &ProfileTreeResponse,
selected_profile: &Option<String>, // The globally selected profile for the checkmark
selected_profile: &Option<String>,
) {
let block = Block::default()
.borders(Borders::ALL)
@@ -45,64 +46,67 @@ pub fn render_admin_panel(
.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();
if auth_state.role.as_deref() != Some("admin") {
// Profile list - Use data from admin_state
let items: Vec<ListItem> = admin_state
.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));
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);
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)),
]));
// Profile details - Use selection info from admin_state
if let Some(profile) = admin_state
.get_selected_index()
.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),
)));
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),
));
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));
}
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]);
let details_widget = Paragraph::new(text)
.block(Block::default().title("Details"))
.wrap(Wrap { trim: true });
f.render_widget(details_widget, content_chunks[1]);
}
} else {
// Admin-specific view
let admin_message = Paragraph::new("Admin-specific view. Profile selection not applicable.")
.style(Style::default().fg(theme.fg))
.alignment(Alignment::Center);
f.render_widget(admin_message, content_chunks[1]);
}
}