pub mod inspector;
pub mod table;
use crate::app::{ActivePane, App, InputMode, InspectorView, LogTimeField, SortColumn, SortOrder};
use ratatui::{
layout::{Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style, Stylize},
text::{Line, Span},
widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph, Wrap},
Frame,
};
fn pane_border_style(is_active: bool) -> Style {
if is_active {
Style::default().fg(Color::Cyan)
} else {
Style::default()
}
}
pub fn render_ui(f: &mut Frame, app: &mut App) {
let chunks = Layout::default()
.constraints([Constraint::Min(3), Constraint::Length(3)])
.split(f.area());
let main_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(65), Constraint::Percentage(35)])
.split(chunks[0]);
let table = table::render_table(app, main_chunks[0]);
f.render_stateful_widget(table, main_chunks[0], &mut app.table_state);
let inspector_is_active = app.active_pane == ActivePane::Inspector;
let inspector_title = match (inspector_is_active, app.inspector_view) {
(true, InspectorView::MrInfo) => " MR Inspector [FOCUS] │ [P]: Pipelines ",
(false, InspectorView::MrInfo) => " MR Inspector │ [P]: Pipelines ",
(true, InspectorView::Pipelines) => " Pipelines [FOCUS] │ [P]: Time Log ",
(false, InspectorView::Pipelines) => " Pipelines │ [P]: Time Log ",
(true, InspectorView::TimeLog) => " Time Log [FOCUS] │ [P]: MR Info │ [L]: Log Time ",
(false, InspectorView::TimeLog) => " Time Log │ [P]: MR Info │ [L]: Log Time ",
};
let inspector_block = Block::default()
.borders(Borders::ALL)
.border_style(pane_border_style(inspector_is_active))
.title(inspector_title);
if let Some(selected) = app.table_state.selected() {
if let Some(mr) = app.mrs.get(selected) {
let rendered_text = match app.inspector_view {
InspectorView::MrInfo => inspector::render_safe_inspector_text(mr, &app.config),
InspectorView::Pipelines => inspector::render_pipelines_text(mr),
InspectorView::TimeLog => inspector::render_time_log_text(mr, &app.time_entries),
};
app.inspector_content_lines = rendered_text.lines.len() as u16;
app.inspector_pane_height = main_chunks[1].height.saturating_sub(2);
let inspector_paragraph = Paragraph::new(rendered_text)
.block(inspector_block)
.wrap(Wrap { trim: false })
.scroll((app.inspector_scroll, 0));
f.render_widget(inspector_paragraph, main_chunks[1]);
} else {
let inspector_paragraph =
Paragraph::new("Selected metadata unavailable.").block(inspector_block);
f.render_widget(inspector_paragraph, main_chunks[1]);
}
} else {
let inspector_paragraph = Paragraph::new(
"Select an active Merge Request row to display side inspector panels context.",
)
.block(inspector_block)
.dark_gray();
f.render_widget(inspector_paragraph, main_chunks[1]);
};
let sort_status = match (app.sort_column, app.sort_order) {
(SortColumn::UpdatedAt, SortOrder::Ascending) => "Sort: Updated ▲",
(SortColumn::UpdatedAt, SortOrder::Descending) => "Sort: Updated ▼",
(SortColumn::Id, SortOrder::Ascending) => "Sort: ID ▲",
(SortColumn::Id, SortOrder::Descending) => "Sort: ID ▼",
(SortColumn::Milestone, SortOrder::Ascending) => "Sort: Milestone ▲",
(SortColumn::Milestone, SortOrder::Descending) => "Sort: Milestone ▼",
(SortColumn::Title, SortOrder::Ascending) => "Sort: Title ▲",
(SortColumn::Title, SortOrder::Descending) => "Sort: Title ▼",
};
let pane_hint = match app.active_pane {
ActivePane::Dashboard => "Pane: Dashboard",
ActivePane::Inspector => "Pane: Inspector",
};
let (input_title, input_border_style) = match app.input_mode {
InputMode::Editing if !app.milestone_suggestions.is_empty() => (
" MILESTONE │ [↑/↓ Tab]: Navigate │ [Enter]: Bulk-add MRs │ [Esc]: Close ".to_string(),
Style::default().fg(Color::Yellow),
),
InputMode::Editing => (
" INSERT │ MR ID, branch name, or @milestone │ [Enter]: Confirm │ [Esc]: Cancel ".to_string(),
Style::default().fg(Color::Yellow),
),
InputMode::ColumnPicker => (
" COLUMNS │ [↑/↓]: Navigate │ [Space]: Toggle │ [Esc]: Close & Save ".to_string(),
Style::default().fg(Color::Cyan),
),
InputMode::Normal => (
format!(
" [i] or [/]: Insert mode │ [Tab]: {} │ [S/s]: {} │ [F]: Filter │ [Space]: Flag │ [C]: Columns │ [▲/▼]: Scroll │ [O]: Open │ [R]: Refresh │ [Del]: Delete │ [Esc]: Quit ",
pane_hint, sort_status
),
Style::default(),
),
InputMode::LogTime => (
" LOG TIME │ [Tab]: Next field │ [Enter]: Submit │ [Esc]: Cancel ".to_string(),
Style::default().fg(Color::Magenta),
),
};
let input_box = Paragraph::new(app.input.as_str()).block(
Block::default()
.borders(Borders::ALL)
.border_style(input_border_style)
.title(input_title),
);
f.render_widget(input_box, chunks[1]);
if app.input_mode == InputMode::ColumnPicker {
render_column_picker(f, app, f.area());
}
if app.input_mode == InputMode::Editing && !app.milestone_suggestions.is_empty() {
render_milestone_autocomplete(f, app, chunks[1]);
}
if app.input_mode == InputMode::LogTime {
render_log_time_popup(f, app, f.area());
}
}
fn render_log_time_popup(f: &mut Frame, app: &App, area: Rect) {
use ratatui::widgets::List;
let ticket_label = app
.table_state
.selected()
.and_then(|i| app.visible_mrs().nth(i))
.and_then(|mr| mr.linked_ticket.as_ref())
.map(|t| format!(" ⏱ Log Time — #{} ", t.id))
.unwrap_or_else(|| " ⏱ Log Time ".to_string());
let popup_width: u16 = 60;
let activity_rows = (app.activities.len() as u16).clamp(2, 6);
let popup_height: u16 = 3 + activity_rows + 3 + 2 + 2;
let popup_x = area.x + area.width.saturating_sub(popup_width) / 2;
let popup_y = area.y + area.height.saturating_sub(popup_height) / 2;
let popup_area = Rect::new(popup_x, popup_y, popup_width, popup_height);
f.render_widget(Clear, popup_area);
let outer_block = Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::Magenta))
.title(Span::styled(
ticket_label,
Style::default()
.fg(Color::Magenta)
.add_modifier(Modifier::BOLD),
));
f.render_widget(outer_block, popup_area);
let inner = Rect::new(
popup_area.x + 1,
popup_area.y + 1,
popup_area.width.saturating_sub(2),
popup_area.height.saturating_sub(2),
);
let zones = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(3), Constraint::Length(activity_rows), Constraint::Length(3), Constraint::Min(1), ])
.split(inner);
let field_style = |focused: bool| -> Style {
if focused {
Style::default().fg(Color::Yellow)
} else {
Style::default().fg(Color::DarkGray)
}
};
let duration_focused = app.log_time_form.focused_field == LogTimeField::Duration;
let duration_block = Block::default()
.borders(Borders::ALL)
.border_style(field_style(duration_focused))
.title(Span::styled(
" Duration (e.g. 1h30, 90m, 1.5h) ",
Style::default().fg(Color::White),
));
let duration_widget = Paragraph::new(app.log_time_form.duration_input.as_str())
.block(duration_block)
.style(Style::default().fg(Color::White));
f.render_widget(duration_widget, zones[0]);
let activity_focused = app.log_time_form.focused_field == LogTimeField::Activity;
let activity_block = Block::default()
.borders(Borders::ALL)
.border_style(field_style(activity_focused))
.title(Span::styled(
" Activity [↑/↓] ",
Style::default().fg(Color::White),
));
if app.activities.is_empty() {
let loading = Paragraph::new("Loading activities…")
.block(activity_block)
.style(Style::default().fg(Color::DarkGray));
f.render_widget(loading, zones[1]);
} else {
let cursor = app.log_time_form.selected_activity_idx;
let visible = activity_rows as usize;
let scroll_offset = if cursor >= visible {
cursor + 1 - visible
} else {
0
};
let items: Vec<ListItem> = app
.activities
.iter()
.enumerate()
.skip(scroll_offset)
.take(visible)
.map(|(i, act)| {
let selected = i == cursor;
let style = if selected {
Style::default()
.fg(Color::Black)
.bg(Color::Magenta)
.add_modifier(Modifier::BOLD)
} else {
Style::default().fg(Color::White)
};
ListItem::new(Line::from(Span::styled(format!(" {} ", act.name), style)))
})
.collect();
let list = List::new(items).block(activity_block);
let mut list_state = ratatui::widgets::ListState::default();
list_state.select(Some(cursor.saturating_sub(scroll_offset)));
f.render_stateful_widget(list, zones[1], &mut list_state);
}
let comment_focused = app.log_time_form.focused_field == LogTimeField::Comment;
let comment_block = Block::default()
.borders(Borders::ALL)
.border_style(field_style(comment_focused))
.title(Span::styled(
" Comment (optional) ",
Style::default().fg(Color::White),
));
let comment_widget = Paragraph::new(app.log_time_form.comment_input.as_str())
.block(comment_block)
.style(Style::default().fg(Color::White));
f.render_widget(comment_widget, zones[2]);
let bottom_line = if let Some(err) = &app.log_time_form.error {
Line::from(vec![
Span::styled(
" ✘ ",
Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
),
Span::styled(err.clone(), Style::default().fg(Color::Red)),
])
} else if app.log_time_form.submitting {
Line::from(vec![Span::styled(
" ⟳ Submitting…",
Style::default().fg(Color::Yellow),
)])
} else {
Line::from(vec![
Span::styled(" [Tab] ", Style::default().fg(Color::DarkGray)),
Span::styled("Next field ", Style::default().fg(Color::DarkGray)),
Span::styled("[Enter] ", Style::default().fg(Color::DarkGray)),
Span::styled("Submit ", Style::default().fg(Color::DarkGray)),
Span::styled("[Esc] ", Style::default().fg(Color::DarkGray)),
Span::styled("Cancel", Style::default().fg(Color::DarkGray)),
])
};
f.render_widget(Paragraph::new(bottom_line), zones[3]);
}
fn render_milestone_autocomplete(f: &mut Frame, app: &App, input_area: Rect) {
let suggestions = &app.milestone_suggestions;
if suggestions.is_empty() {
return;
}
let max_visible: u16 = 8;
let visible_count = (suggestions.len() as u16).min(max_visible);
let popup_height = visible_count + 2;
let popup_width = (input_area.width / 2).max(40);
let popup_x = input_area.x;
let popup_y = input_area.y.saturating_sub(popup_height);
let popup_area = Rect::new(popup_x, popup_y, popup_width, popup_height);
f.render_widget(Clear, popup_area);
let cursor = app.milestone_suggestion_cursor;
let scroll_offset = if cursor >= max_visible as usize {
cursor + 1 - max_visible as usize
} else {
0
};
let items: Vec<ListItem> = suggestions
.iter()
.enumerate()
.skip(scroll_offset)
.take(max_visible as usize)
.map(|(i, title)| {
let is_selected = i == cursor;
let style = if is_selected {
Style::default()
.fg(Color::Black)
.bg(Color::Yellow)
.add_modifier(Modifier::BOLD)
} else {
Style::default().fg(Color::White)
};
ListItem::new(Line::from(Span::styled(format!(" {} ", title), style)))
})
.collect();
let list = List::new(items).block(
Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::Yellow))
.title(" Milestones │ [↑/↓ Tab]: Navigate │ [Enter]: Select "),
);
let mut list_state = ListState::default();
list_state.select(Some(cursor.saturating_sub(scroll_offset)));
f.render_stateful_widget(list, popup_area, &mut list_state);
}
fn render_column_picker(f: &mut Frame, app: &App, area: Rect) {
let mut entries_vec: Vec<(&str, bool)> = vec![
("Activity", app.config.visible_columns.activity),
("Target branch", app.config.visible_columns.target_branch),
("Labels", app.config.visible_columns.labels),
("Milestone", app.config.visible_columns.milestone),
("Notes", app.config.visible_columns.notes),
];
if app.tracker.is_some() {
entries_vec.push(("Tracker", app.config.visible_columns.tracker_ticket));
}
let entries: &[(&str, bool)] = &entries_vec;
let popup_width: u16 = 36;
let popup_height: u16 = entries.len() as u16 + 2;
let popup_x = area.x + area.width.saturating_sub(popup_width) / 2;
let popup_y = area.y + area.height.saturating_sub(popup_height) / 2;
let popup_area = Rect::new(popup_x, popup_y, popup_width, popup_height);
f.render_widget(Clear, popup_area);
let items: Vec<ListItem> = entries
.iter()
.enumerate()
.map(|(i, (label, enabled))| {
let checkbox = if *enabled { "☑" } else { "☐" };
let is_selected = i == app.column_picker_cursor;
let style = if is_selected {
Style::default()
.fg(Color::Black)
.bg(Color::Cyan)
.add_modifier(Modifier::BOLD)
} else {
Style::default().fg(Color::White)
};
ListItem::new(Line::from(vec![
Span::styled(format!(" {} ", checkbox), style),
Span::styled(label.to_string(), style),
]))
})
.collect();
let list = List::new(items).block(
Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::Cyan))
.title(" Columns "),
);
let mut list_state = ListState::default();
list_state.select(Some(app.column_picker_cursor));
f.render_stateful_widget(list, popup_area, &mut list_state);
}