//! Blog post management screen — list, create, delete posts. use makeover_tui::makeover_layout::{Column, Priority, Width}; use makeover_tui::table::{self, Cell, Sizing, TableStyle}; use ratatui::Frame; use ratatui::layout::{Constraint, Layout, Rect}; use ratatui::style::{Modifier, Style}; use ratatui::text::{Line, Span}; use ratatui::widgets::{Block, Borders, Paragraph, TableState}; use super::App; /// The post list's columns. Status carries the scheduled time as well as /// published-or-draft, which is why it is the widest fixed track and why it /// outranks the slug and the date. const POST_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: "Created", width: Width::Fixed, priority: Priority::Optional, sortable: false, sorted: None, }, ]; /// The tracks the hand-written `Constraint`s carried. const POST_SIZING: Sizing<'static> = Sizing { lengths: &[("Title", 20), ("Slug", 20), ("Status", 22), ("Created", 12)], fallback: 12, }; pub(crate) fn render(frame: &mut Frame, app: &App) { let area = frame.area(); let project_title = app.blog_project_title.as_deref().unwrap_or("Blog"); let title = Line::from(vec![ Span::styled( " Makenot.work ", Style::default().add_modifier(Modifier::BOLD), ), Span::raw(" -- "), Span::styled(project_title, Style::default().add_modifier(Modifier::BOLD)), Span::raw(" -- Blog "), ]); 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), // post list Constraint::Length(1), // status line Constraint::Length(1), // keybindings ]) .split(inner); // Section header let count = app.blog_posts.len(); let header = Paragraph::new(Line::from(vec![ Span::raw(" "), Span::styled("Posts", Style::default().add_modifier(Modifier::BOLD)), if count == 0 { Span::raw("") } else { Span::raw(format!(" ({count})")) }, ])); frame.render_widget(header, chunks[1]); // Post list if app.loading { let loading = Paragraph::new(" Loading..."); frame.render_widget(loading, chunks[2]); } else if app.blog_posts.is_empty() { let empty = Paragraph::new(" No blog posts. Press [n] to create one."); frame.render_widget(empty, chunks[2]); } else { render_post_table(frame, app, chunks[2]); } // Status line if let Some(ref status) = app.blog_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(Line::from(vec![ Span::raw(" "), Span::styled(status.as_str(), style), ])); frame.render_widget(status_line, chunks[3]); } // Keybindings let mut key_spans = vec![ Span::raw(" "), Span::styled("[j/k]", Style::default().add_modifier(Modifier::BOLD)), Span::raw(" Nav "), Span::styled("[n]", Style::default().add_modifier(Modifier::BOLD)), Span::raw(" New "), ]; if !app.blog_posts.is_empty() { key_spans.extend([ Span::styled("[d]", Style::default().add_modifier(Modifier::BOLD)), Span::raw(" Delete "), ]); } key_spans.extend([ 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]); } fn render_post_table(frame: &mut Frame, app: &App, area: Rect) { let area = super::indent(area, 2); let rows: Vec> = app .blog_posts .iter() .map(|post| { let status = if post.is_published { "published".to_string() } else if let Some(ref pa) = post.publish_at { let scheduled = pa.get(..16).unwrap_or(pa); format!("sched {scheduled}") } else { "draft".to_string() }; vec![ Cell::new("Title", post.title.as_str()), Cell::new("Slug", post.slug.as_str()), Cell::new("Status", status), Cell::new( "Created", post.created_at.get(..10).unwrap_or(&post.created_at), ), ] }) .collect(); let style = TableStyle::from_theme(&app.theme); let table = table::table(&POST_COLUMNS, &rows, &POST_SIZING, &style, area.width); let mut state = TableState::default().with_selected(Some(app.selected_index)); frame.render_stateful_widget(table, area, &mut state); }