From 0917654361e3a45ee638fb0342b407ae1c42fed6 Mon Sep 17 00:00:00 2001 From: filipriec Date: Mon, 14 Apr 2025 21:04:55 +0200 Subject: [PATCH] admin panel now distinguish admin and other roles of the user --- client/src/components/admin/admin_panel.rs | 145 ++++++++++++--------- 1 file changed, 85 insertions(+), 60 deletions(-) diff --git a/client/src/components/admin/admin_panel.rs b/client/src/components/admin/admin_panel.rs index 59fac14..7074f21 100644 --- a/client/src/components/admin/admin_panel.rs +++ b/client/src/components/admin/admin_panel.rs @@ -12,6 +12,16 @@ use ratatui::{ Frame, }; +/// Renders the view specific to admin users. +fn render_admin_panel_admin(f: &mut Frame, content_chunks: &[Rect], theme: &Theme) { + // Admin-specific view placeholder + let admin_message = Paragraph::new("Admin-specific view. Profile selection not applicable.") + .style(Style::default().fg(theme.fg)) + .alignment(Alignment::Center); + // Render only in the right pane for now, leaving left empty + f.render_widget(admin_message, content_chunks[1]); +} + pub fn render_admin_panel( f: &mut Frame, auth_state: &AuthState, @@ -47,66 +57,81 @@ pub fn render_admin_panel( .split(chunks[1]); if auth_state.role.as_deref() != Some("admin") { - // Profile list - Use data from admin_state - let items: Vec = 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 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() - .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")) - .wrap(Wrap { trim: true }); - f.render_widget(details_widget, content_chunks[1]); - } + render_admin_panel_non_admin( + f, + admin_state, + &content_chunks, + theme, + profile_tree, + selected_profile, + ); } 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]); + render_admin_panel_admin(f, &content_chunks, theme); + } +} + +/// Renders the view for non-admin users (profile list and details). +fn render_admin_panel_non_admin( + f: &mut Frame, + admin_state: &AdminState, + content_chunks: &[Rect], + theme: &Theme, + profile_tree: &ProfileTreeResponse, + selected_profile: &Option, +) { + // Profile list - Use data from admin_state + let items: Vec = 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 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() + .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")) + .wrap(Wrap { trim: true }); + f.render_widget(details_widget, content_chunks[1]); } }