// src/state/pages/admin.rs use ratatui::widgets::ListState; #[derive(Default, Clone, Debug)] pub struct AdminState { pub profiles: Vec, pub list_state: ListState, } impl AdminState { /// Gets the index of the currently selected item. pub fn get_selected_index(&self) -> Option { self.list_state.selected() } /// Gets the name of the currently selected profile. pub fn get_selected_profile_name(&self) -> Option<&String> { self.list_state.selected().and_then(|i| self.profiles.get(i)) } /// Populates the profile list and updates/resets the selection. pub fn set_profiles(&mut self, new_profiles: Vec) { let current_selection_index = self.list_state.selected(); self.profiles = new_profiles; if self.profiles.is_empty() { self.list_state.select(None); } else { let new_selection = match current_selection_index { Some(index) => Some(index.min(self.profiles.len() - 1)), None => Some(0), }; self.list_state.select(new_selection); } } /// Selects the next profile in the list, wrapping around. pub fn next(&mut self) { if self.profiles.is_empty() { self.list_state.select(None); return; } let i = match self.list_state.selected() { Some(i) => if i >= self.profiles.len() - 1 { 0 } else { i + 1 }, None => 0, }; self.list_state.select(Some(i)); } /// Selects the previous profile in the list, wrapping around. pub fn previous(&mut self) { if self.profiles.is_empty() { self.list_state.select(None); return; } let i = match self.list_state.selected() { Some(i) => if i == 0 { self.profiles.len() - 1 } else { i - 1 }, None => self.profiles.len() - 1, }; self.list_state.select(Some(i)); } }