use color_eyre::Result; use ratatui::{ text::Text, layout::{Alignment, Constraint, Rect}, widgets::{Block, Cell, Row, Table}, Frame }; use super::{Component, PartialComponent, CurrentTab}; use crate::{ action::Action, config::Config, palette::StylePalette, widgets::DotSpinner, }; #[derive(Debug)] pub struct Overview { is_active: bool, existential_balance: Option, total_issuance: Option, palette: StylePalette } impl Default for Overview { fn default() -> Self { Self::new() } } impl Overview { const TICKER: &str = " CSPR"; const DECIMALS: usize = 6; pub fn new() -> Self { Self { is_active: false, existential_balance: None, total_issuance: None, palette: StylePalette::default(), } } fn prepare_u128(&self, maybe_value: Option) -> String { match maybe_value { Some(value) => { let value = value as f64 / 10f64.powi(18); let after = Self::DECIMALS; format!("{:.after$}{}", value, Self::TICKER) }, None => format!("{}{}", DotSpinner::default().to_string(), Self::TICKER) } } } impl PartialComponent for Overview { fn set_active(&mut self, current_tab: CurrentTab) { match current_tab { CurrentTab::Accounts => self.is_active = true, _ => self.is_active = false, } } } impl Component for Overview { fn register_config_handler(&mut self, config: Config) -> Result<()> { if let Some(style) = config.styles.get(&crate::app::Mode::Wallet) { self.palette.with_normal_style(style.get("normal_style").copied()); self.palette.with_hover_style(style.get("hover_style").copied()); self.palette.with_normal_border_style(style.get("normal_border_style").copied()); self.palette.with_hover_border_style(style.get("hover_border_style").copied()); self.palette.with_normal_title_style(style.get("normal_title_style").copied()); self.palette.with_hover_title_style(style.get("hover_title_style").copied()); } Ok(()) } fn update(&mut self, action: Action) -> Result> { match action { Action::SetExistentialDeposit(ed) => self.existential_balance = Some(ed), Action::SetTotalIssuance(maybe_issuance) => self.total_issuance = maybe_issuance, _ => {} }; Ok(None) } fn draw(&mut self, frame: &mut Frame, area: Rect) -> Result<()> { let [place, _, _, _] = super::account_layout(area); let (border_style, border_type) = self.palette .create_border_style(self.is_active); let table = Table::new( [ Row::new(vec![ Cell::from(Text::from("total supply: ".to_string()).alignment(Alignment::Left)), Cell::from(Text::from(self.prepare_u128(self.total_issuance)).alignment(Alignment::Right)), ]), Row::new(vec![ Cell::from(Text::from("min deposit: ".to_string()).alignment(Alignment::Left)), Cell::from(Text::from(self.prepare_u128(self.existential_balance)).alignment(Alignment::Right)), ]), ], [ Constraint::Max(15), Constraint::Min(0), ] ) .block(Block::bordered() .border_style(border_style) .border_type(border_type) .title_alignment(Alignment::Right) .title_style(self.palette.create_title_style(false)) .title("Overview")); frame.render_widget(table, place); Ok(()) } }