|
6 | 6 | use crate::app::App; |
7 | 7 | use crate::theme::styles; |
8 | 8 | use navigator_core::proto::PolicyChunk; |
9 | | -use ratatui::Frame; |
10 | 9 | use ratatui::layout::{Constraint, Direction, Layout, Rect}; |
11 | 10 | use ratatui::style::{Color, Modifier, Style}; |
12 | 11 | use ratatui::text::{Line, Span}; |
13 | 12 | use ratatui::widgets::{Block, Borders, Clear, Padding, Paragraph, Wrap}; |
| 13 | +use ratatui::Frame; |
14 | 14 |
|
15 | 15 | /// Draw the draft recommendations panel (list view with highlight bar). |
16 | 16 | pub fn draw(frame: &mut Frame<'_>, app: &mut App, area: Rect) { |
@@ -267,6 +267,118 @@ pub fn draw_detail_popup(frame: &mut Frame<'_>, chunk: &PolicyChunk, area: Rect) |
267 | 267 | ); |
268 | 268 | } |
269 | 269 |
|
| 270 | +// --------------------------------------------------------------------------- |
| 271 | +// Approve-all confirmation popup ([A] key) |
| 272 | +// --------------------------------------------------------------------------- |
| 273 | + |
| 274 | +pub fn draw_approve_all_popup(frame: &mut Frame<'_>, chunks: &[PolicyChunk], area: Rect) { |
| 275 | + let count = chunks.len(); |
| 276 | + // Height: header(1) + blank(1) + chunks(count, capped at 12) + blank(1) + hints(1) + borders(2) + padding(1) |
| 277 | + let list_lines = count.min(12); |
| 278 | + let popup_height = (7 + list_lines) as u16; |
| 279 | + let popup_height = popup_height.min(area.height.saturating_sub(4)); |
| 280 | + let popup_width = (area.width * 4 / 5).min(area.width.saturating_sub(4)); |
| 281 | + let popup_area = centered_rect(popup_width, popup_height, area); |
| 282 | + |
| 283 | + frame.render_widget(Clear, popup_area); |
| 284 | + |
| 285 | + let block = Block::default() |
| 286 | + .title(Span::styled( |
| 287 | + " Approve All ", |
| 288 | + Style::default() |
| 289 | + .fg(Color::Yellow) |
| 290 | + .add_modifier(Modifier::BOLD), |
| 291 | + )) |
| 292 | + .borders(Borders::ALL) |
| 293 | + .border_style(styles::ACCENT) |
| 294 | + .padding(Padding::new(1, 1, 0, 0)); |
| 295 | + |
| 296 | + // Usable width inside borders + padding. |
| 297 | + let inner_width = popup_width.saturating_sub(4) as usize; |
| 298 | + |
| 299 | + let mut lines: Vec<Line<'_>> = Vec::new(); |
| 300 | + |
| 301 | + lines.push(Line::from(vec![ |
| 302 | + Span::styled("Approve ", styles::TEXT), |
| 303 | + Span::styled( |
| 304 | + format!("{count}"), |
| 305 | + Style::default() |
| 306 | + .fg(Color::Yellow) |
| 307 | + .add_modifier(Modifier::BOLD), |
| 308 | + ), |
| 309 | + Span::styled( |
| 310 | + format!( |
| 311 | + " pending policy request{}?", |
| 312 | + if count == 1 { "" } else { "s" } |
| 313 | + ), |
| 314 | + styles::TEXT, |
| 315 | + ), |
| 316 | + ])); |
| 317 | + lines.push(Line::from("")); |
| 318 | + |
| 319 | + for (i, chunk) in chunks.iter().enumerate() { |
| 320 | + if i >= 12 { |
| 321 | + lines.push(Line::from(Span::styled( |
| 322 | + format!(" ... and {} more", count - 12), |
| 323 | + styles::MUTED, |
| 324 | + ))); |
| 325 | + break; |
| 326 | + } |
| 327 | + let endpoint_str = chunk |
| 328 | + .proposed_rule |
| 329 | + .as_ref() |
| 330 | + .and_then(|r| r.endpoints.first()) |
| 331 | + .map(|ep| format!("{}:{}", ep.host, ep.port)) |
| 332 | + .unwrap_or_default(); |
| 333 | + |
| 334 | + // Truncate to fit within the popup width. |
| 335 | + // " -> " (5) + rule_name + " " (2) + endpoint |
| 336 | + let prefix_len = 5; |
| 337 | + let sep_len = 2; |
| 338 | + let budget = inner_width.saturating_sub(prefix_len + sep_len); |
| 339 | + let (name_str, ep_str) = if chunk.rule_name.len() + endpoint_str.len() > budget { |
| 340 | + let ep_budget = endpoint_str.len().min(budget / 2); |
| 341 | + let name_budget = budget.saturating_sub(ep_budget); |
| 342 | + ( |
| 343 | + truncate_str(&chunk.rule_name, name_budget), |
| 344 | + truncate_str(&endpoint_str, ep_budget), |
| 345 | + ) |
| 346 | + } else { |
| 347 | + (chunk.rule_name.clone(), endpoint_str) |
| 348 | + }; |
| 349 | + |
| 350 | + lines.push(Line::from(vec![ |
| 351 | + Span::styled(" -> ", styles::MUTED), |
| 352 | + Span::styled(name_str, styles::TEXT), |
| 353 | + Span::styled(" ", styles::MUTED), |
| 354 | + Span::styled(ep_str, styles::ACCENT), |
| 355 | + ])); |
| 356 | + } |
| 357 | + |
| 358 | + lines.push(Line::from("")); |
| 359 | + lines.push(Line::from(vec![ |
| 360 | + Span::styled("[y/Enter]", styles::KEY_HINT), |
| 361 | + Span::styled(" Approve all ", styles::TEXT), |
| 362 | + Span::styled("[n/Esc]", styles::KEY_HINT), |
| 363 | + Span::styled(" Cancel", styles::TEXT), |
| 364 | + ])); |
| 365 | + |
| 366 | + frame.render_widget(Paragraph::new(lines).block(block), popup_area); |
| 367 | +} |
| 368 | + |
| 369 | +/// Truncate a string to `max_len` chars, appending "..." if truncated. |
| 370 | +fn truncate_str(s: &str, max_len: usize) -> String { |
| 371 | + if s.len() <= max_len { |
| 372 | + s.to_string() |
| 373 | + } else if max_len <= 3 { |
| 374 | + s.chars().take(max_len).collect() |
| 375 | + } else { |
| 376 | + let mut out: String = s.chars().take(max_len - 3).collect(); |
| 377 | + out.push_str("..."); |
| 378 | + out |
| 379 | + } |
| 380 | +} |
| 381 | + |
270 | 382 | fn format_short_time(epoch_ms: i64) -> String { |
271 | 383 | if epoch_ms <= 0 { |
272 | 384 | return String::from("--:--:--"); |
|
0 commit comments