use color_eyre::Result; use ratatui::{ layout::{Alignment, Rect}, text::Line, widgets::{Block, Paragraph, Wrap}, Frame, }; use subxt::utils::H256; use super::Component; use crate::{ config::Config, action::Action, palette::StylePalette, widgets::OghamCenter, }; #[derive(Debug, Clone, Default)] pub struct Version { genesis_hash: Option, node_version: Option, chain_name: Option, palette: StylePalette, } impl Version { fn prepared_genesis_hash(&self) -> String { match self.genesis_hash { Some(genesis_hash) => genesis_hash.to_string(), None => OghamCenter::default().to_string(), } } } impl Component for Version { fn register_config_handler(&mut self, config: Config) -> Result<()> { if let Some(style) = config.styles.get(&crate::app::Mode::Menu) { self.palette.with_normal_style(style.get("normal_style").copied()); self.palette.with_normal_border_style(style.get("normal_border_style").copied()); } Ok(()) } fn update(&mut self, action: Action) -> Result> { match action { Action::SetChainName(maybe_name) => self.chain_name = maybe_name, Action::SetChainVersion(version) => self.node_version = version, Action::SetGenesisHash(maybe_genesis) => self.genesis_hash = maybe_genesis, _ => {} } Ok(None) } fn draw(&mut self, frame: &mut Frame, area: Rect) -> Result<()> { let [_, version] = super::menu_layout(area); let text_style = self.palette.create_basic_style(false); let (border_style, border_type) = self.palette.create_border_style(false); let text = vec![ Line::styled(self.chain_name.clone().unwrap_or(OghamCenter::default().to_string()), text_style), Line::styled(self.node_version.clone().unwrap_or(OghamCenter::default().to_string()), text_style), Line::styled(self.prepared_genesis_hash(), text_style), ]; let paragraph = Paragraph::new(text) .block(Block::bordered() .border_style(border_style) .border_type(border_type) ) .alignment(Alignment::Center) .wrap(Wrap { trim: true }); frame.render_widget(paragraph, version); Ok(()) } }