use super::{account, fg, selected, spinner, INK3, LIVE, NUM, SEED};
use crate::session::app::{App, Write, SHARE_ROWS};
use ratatui::layout::Rect;
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, List, ListItem, ListState};
use ratatui::Frame;
pub fn render(f: &mut Frame, area: Rect, app: &App) {
let Some(s) = app.summary() else {
return account::not_running(f, area, app);
};
let pending = |row: &str| match (&app.pending, row) {
(Some(Write::Sharing(_)), "Sharing")
| (Some(Write::Workers(_)), "Workers")
| (Some(Write::Price(_)), "Price") => Some(format!(" {} applying…", spinner(app))),
_ => None,
};
let w = s.this_mac.workers;
let price_now = s.prices.this_mac.per_hour.or(s.prices.default_per_hour);
let items: Vec<ListItem> = SHARE_ROWS
.iter()
.map(|row| {
let value: Vec<Span> = match *row {
"Sharing" => vec![Span::styled(
if s.this_mac.sharing { "on" } else { "off" },
fg(if s.this_mac.sharing { LIVE } else { INK3 }),
)],
"Workers" => vec![Span::raw(format!(
"{} of {} · {} running · {} busy · {} draining",
w.desired, w.max, w.running, w.busy, w.draining
))],
"Price" => match &app.price_edit {
Some(ed) => vec![
Span::styled(format!("[ {} ]", ed.buf), fg(SEED)),
Span::styled(
format!(" /h {}–{} · step {}", ed.min, ed.max, ed.step),
fg(INK3),
),
],
None => vec![
Span::styled(
price_now
.map(|p| format!("{p} yen/h"))
.unwrap_or_else(|| "-".into()),
fg(NUM),
),
Span::styled(
format!(
" effective {} · {}",
s.prices
.this_mac
.effective_per_hour
.map(|p| p.to_string())
.unwrap_or_else(|| "-".into()),
s.prices.this_mac.state
),
fg(INK3),
),
],
},
"Broker" => vec![Span::raw(s.this_mac.broker.clone())],
_ => vec![Span::raw(format!(
"{} in 5h · {} in 1w",
s.this_mac.requests.last_5h, s.this_mac.requests.last_1w
))],
};
let mut spans = vec![Span::styled(format!("{row:<10} "), fg(INK3))];
spans.extend(value);
if let Some(p) = pending(row) {
spans.push(Span::styled(p, fg(INK3)));
}
ListItem::new(Line::from(spans))
})
.collect();
let list = List::new(items)
.block(
Block::bordered()
.border_style(fg(INK3))
.title(format!(" share · {} ", s.this_mac.name)),
)
.highlight_style(selected());
let mut state = ListState::default();
state.select(Some(app.share_sel));
f.render_stateful_widget(list, area, &mut state);
}