|
| 1 | +// Application State - Global application state management |
| 2 | +use std::collections::VecDeque; |
| 3 | +use super::{Tunnel, Connection, LogEntry, Settings}; |
| 4 | + |
| 5 | +/// Current active page in the application |
| 6 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 7 | +pub enum Page { |
| 8 | + GetStarted, |
| 9 | + Connections, |
| 10 | + NetworkLogs, |
| 11 | + Settings, |
| 12 | +} |
| 13 | + |
| 14 | +/// Daemon connection status |
| 15 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 16 | +pub enum DaemonStatus { |
| 17 | + Stopped, |
| 18 | + Starting, |
| 19 | + Running, |
| 20 | + Stopping, |
| 21 | + Error, |
| 22 | +} |
| 23 | + |
| 24 | +/// Global application state |
| 25 | +/// This struct holds the main application data that is shared across views |
| 26 | +pub struct AppState { |
| 27 | + /// Currently active page |
| 28 | + pub current_page: Page, |
| 29 | + |
| 30 | + /// List of configured tunnels |
| 31 | + pub tunnels: Vec<Tunnel>, |
| 32 | + |
| 33 | + /// Active connections |
| 34 | + pub connections: Vec<Connection>, |
| 35 | + |
| 36 | + /// Application settings |
| 37 | + pub settings: Settings, |
| 38 | + |
| 39 | + /// Recent log entries (circular buffer) |
| 40 | + pub recent_logs: VecDeque<LogEntry>, |
| 41 | + |
| 42 | + /// Maximum number of logs to keep in memory |
| 43 | + pub max_logs: usize, |
| 44 | + |
| 45 | + /// Current daemon status |
| 46 | + pub daemon_status: DaemonStatus, |
| 47 | +} |
| 48 | + |
| 49 | +impl AppState { |
| 50 | + /// Create a new AppState with default values |
| 51 | + pub fn new() -> Self { |
| 52 | + Self { |
| 53 | + current_page: Page::GetStarted, |
| 54 | + tunnels: Vec::new(), |
| 55 | + connections: Vec::new(), |
| 56 | + settings: Settings::default(), |
| 57 | + recent_logs: VecDeque::new(), |
| 58 | + max_logs: 10000, |
| 59 | + daemon_status: DaemonStatus::Stopped, |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// Add a log entry, removing oldest if over capacity |
| 64 | + pub fn add_log(&mut self, entry: LogEntry) { |
| 65 | + if self.recent_logs.len() >= self.max_logs { |
| 66 | + self.recent_logs.pop_front(); |
| 67 | + } |
| 68 | + self.recent_logs.push_back(entry); |
| 69 | + } |
| 70 | + |
| 71 | + /// Clear all logs |
| 72 | + pub fn clear_logs(&mut self) { |
| 73 | + self.recent_logs.clear(); |
| 74 | + } |
| 75 | + |
| 76 | + /// Add or update a tunnel |
| 77 | + pub fn upsert_tunnel(&mut self, tunnel: Tunnel) { |
| 78 | + if let Some(pos) = self.tunnels.iter().position(|t| t.id == tunnel.id) { |
| 79 | + self.tunnels[pos] = tunnel; |
| 80 | + } else { |
| 81 | + self.tunnels.push(tunnel); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// Remove a tunnel by ID |
| 86 | + pub fn remove_tunnel(&mut self, tunnel_id: &str) { |
| 87 | + self.tunnels.retain(|t| t.id != tunnel_id); |
| 88 | + } |
| 89 | + |
| 90 | + /// Get a tunnel by ID |
| 91 | + pub fn get_tunnel(&self, tunnel_id: &str) -> Option<&Tunnel> { |
| 92 | + self.tunnels.iter().find(|t| t.id == tunnel_id) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl Default for AppState { |
| 97 | + fn default() -> Self { |
| 98 | + Self::new() |
| 99 | + } |
| 100 | +} |
0 commit comments