ghost-eye/src/components/wallet/balance.rs
Uncle Stretch 79abbd566d
use correct mode for the wallet screen
Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
2024-12-05 13:45:08 +03:00

138 lines
4.9 KiB
Rust

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,
};
#[derive(Debug)]
pub struct Balance {
is_active: bool,
total_balance: Option<u128>,
transferable_balance: Option<u128>,
locked_balance: Option<u128>,
bonded_balance: Option<u128>,
palette: StylePalette
}
impl Default for Balance {
fn default() -> Self {
Self::new()
}
}
impl Balance {
const DECIMALS_FOR_BALANCE: usize = 5;
pub fn new() -> Self {
Self {
is_active: false,
total_balance: None,
transferable_balance: None,
locked_balance: None,
bonded_balance: None,
palette: StylePalette::default(),
}
}
fn prepare_u128(&self, value: u128, after: usize) -> String {
let value = value as f64 / 10f64.powi(18);
format!("{:.after$}", value)
}
}
impl PartialComponent for Balance {
fn set_active(&mut self, current_tab: CurrentTab) {
match current_tab {
CurrentTab::Accounts => self.is_active = true,
_ => self.is_active = false,
}
}
}
impl Component for Balance {
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<Option<Action>> {
match action {
_ => {}
};
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("account: ".to_string()).alignment(Alignment::Left)),
Cell::from(Text::from(self.prepare_u128(
self.total_balance.unwrap_or_default(),
Self::DECIMALS_FOR_BALANCE,
)).alignment(Alignment::Center)),
Cell::from(Text::from("CSPR".to_string()).alignment(Alignment::Right))
]),
Row::new(vec![
Cell::from(Text::from("free: ".to_string()).alignment(Alignment::Left)),
Cell::from(Text::from(self.prepare_u128(
self.transferable_balance.unwrap_or_default(),
Self::DECIMALS_FOR_BALANCE,
)).alignment(Alignment::Center)),
Cell::from(Text::from("CSPR".to_string()).alignment(Alignment::Right)),
]),
Row::new(vec![
Cell::from(Text::from("locked: ".to_string()).alignment(Alignment::Left)),
Cell::from(Text::from(self.prepare_u128(
self.locked_balance.unwrap_or_default(),
Self::DECIMALS_FOR_BALANCE,
)).alignment(Alignment::Center)),
Cell::from(Text::from("CSPR".to_string()).alignment(Alignment::Right)),
]),
Row::new(vec![
Cell::from(Text::from("bonded: ".to_string()).alignment(Alignment::Left)),
Cell::from(Text::from(self.prepare_u128(
self.bonded_balance.unwrap_or_default(),
Self::DECIMALS_FOR_BALANCE,
)).alignment(Alignment::Center)),
Cell::from(Text::from("CSPR".to_string()).alignment(Alignment::Right)),
]),
],
[
Constraint::Max(10),
Constraint::Min(0),
Constraint::Length(5),
]
)
.block(Block::bordered()
.border_style(border_style)
.border_type(border_type)
.title_alignment(Alignment::Right)
.title_style(self.palette.create_title_style(false))
.title("Balance"));
frame.render_widget(table, place);
Ok(())
}
}