//! Collections management screen — list collections. use makeover_tui::makeover_layout::{Column, Priority, Width}; use makeover_tui::table::{self, Cell, Sizing, TableStyle}; use ratatui::Frame; use ratatui::layout::{Constraint, Layout}; use ratatui::style::{Modifier, Style}; use ratatui::text::{Line, Span}; use ratatui::widgets::{Block, Borders, Paragraph, TableState}; use super::App; /// The collection columns. The title names the collection; whether it is public /// is the next thing asked of it. The slug and the count drop first. const COLLECTION_COLUMNS: [Column<'static>; 4] = [ Column { name: "Title", width: Width::Fill, priority: Priority::Essential, sortable: false, sorted: None, }, Column { name: "Slug", width: Width::Fixed, priority: Priority::Optional, sortable: false, sorted: None, }, Column { name: "Status", width: Width::Fixed, priority: Priority::Secondary, sortable: false, sorted: None, }, Column { name: "Items", width: Width::Fixed, priority: Priority::Optional, sortable: false, sorted: None, }, ]; /// The tracks the hand-written `Constraint`s carried. const COLLECTION_SIZING: Sizing<'static> = Sizing { lengths: &[("Title", 20), ("Slug", 20), ("Status", 8), ("Items", 6)], fallback: 8, }; pub(crate) fn render(frame: &mut Frame, app: &App) { let area = frame.area(); let title = Line::from(vec![ Span::styled( " Makenot.work ", Style::default().add_modifier(Modifier::BOLD), ), Span::raw(" -- "), Span::styled("Collections", Style::default().add_modifier(Modifier::BOLD)), Span::raw(" "), ]); let block = Block::default() .title(title) .borders(Borders::ALL) .border_style(Style::default().fg(app.theme.line_border)); let inner = block.inner(area); frame.render_widget(block, area); let chunks = Layout::vertical([ Constraint::Length(1), // spacer Constraint::Length(1), // section header Constraint::Min(3), // list Constraint::Length(1), // status line Constraint::Length(1), // keybindings ]) .split(inner); let count = app.collections.len(); let header = Paragraph::new(Line::from(vec![ Span::raw(" "), Span::styled("Collections", Style::default().add_modifier(Modifier::BOLD)), if count == 0 { Span::raw("") } else { Span::raw(format!(" ({count})")) }, ])); frame.render_widget(header, chunks[1]); if app.loading { let loading = Paragraph::new(" Loading..."); frame.render_widget(loading, chunks[2]); } else if app.collections.is_empty() { let empty = Paragraph::new(" No collections. Manage collections at makenot.work/dashboard"); frame.render_widget(empty, chunks[2]); } else { let area = super::indent(chunks[2], 2); let rows: Vec> = app .collections .iter() .map(|c| { vec![ Cell::new("Title", c.title.as_str()), Cell::new("Slug", c.slug.as_str()), Cell::new("Status", if c.is_public { "public" } else { "draft" }), Cell::new("Items", c.item_count.to_string()), ] }) .collect(); let style = TableStyle::from_theme(&app.theme); let table = table::table( &COLLECTION_COLUMNS, &rows, &COLLECTION_SIZING, &style, area.width, ); let mut state = TableState::default().with_selected(Some(app.selected_index)); frame.render_stateful_widget(table, area, &mut state); } if let Some(ref status) = app.collections_status { let style = if status.starts_with("Error") { Style::default().fg(app.theme.status_danger) } else { Style::default().fg(app.theme.status_success) }; let status_line = Paragraph::new(format!(" {status}")).style(style); frame.render_widget(status_line, chunks[3]); } let key_spans = vec![ Span::raw(" "), Span::styled("[j/k]", Style::default().add_modifier(Modifier::BOLD)), Span::raw(" Nav "), Span::styled("[r]", Style::default().add_modifier(Modifier::BOLD)), Span::raw(" Refresh "), Span::styled("[Esc]", Style::default().add_modifier(Modifier::BOLD)), Span::raw(" Back"), ]; let keys = Paragraph::new(Line::from(key_spans)).style(Style::default().fg(app.theme.content_muted)); frame.render_widget(keys, chunks[4]); }