mod connections;
pub use connections::{ConnectionManager, ConnectionStatus};
use crate::config::Config;
use crate::formatter::{JsonColorConfig, JsonFormatter, TextFormatter};
use crate::types::{KeyMetadata, Value};
pub struct AppState {
pub connection_manager: ConnectionManager,
pub keys: Vec<Option<KeyMetadata>>, pub selected_value: Option<Value>,
pub selected_key_index: Option<usize>,
pub value_request_token: u64,
pub text_formatter: TextFormatter,
pub json_formatter: JsonFormatter,
pub error_message: Option<String>,
pub keys_cursor: Option<String>,
pub has_more_keys: bool,
pub total_key_count: Option<u64>,
pub is_loading_keys: bool,
pub keys_per_chunk: usize,
pub viewport_height: usize, pub is_searching: bool, pub search_query: String, pub search_results_local: Vec<usize>, pub search_results_server: Vec<KeyMetadata>, pub search_token: u64, pub is_server_searching: bool, pub search_selection_index: Option<usize>, }
impl AppState {
pub fn new() -> Self {
Self::new_with_config(&Config::default())
}
pub fn new_with_config(config: &Config) -> Self {
let json_color_config = JsonColorConfig {
indent: config.json.indent,
key_color: config.json_key_color(),
string_color: config.json_string_color(),
number_color: config.json_number_color(),
boolean_color: config.json_boolean_color(),
null_color: config.json_null_color(),
brace_color: config.json_brace_color(),
bracket_color: config.json_bracket_color(),
comma_color: config.json_comma_color(),
colon_color: config.json_colon_color(),
};
Self {
connection_manager: ConnectionManager::new(),
keys: Vec::new(),
selected_value: None,
selected_key_index: None,
value_request_token: 0,
text_formatter: TextFormatter,
json_formatter: JsonFormatter::new(json_color_config),
error_message: None,
keys_cursor: None,
has_more_keys: false,
total_key_count: None,
is_loading_keys: false,
keys_per_chunk: config.data.keys_per_chunk,
viewport_height: config.ui.viewport_height,
is_searching: false,
search_query: String::new(),
search_results_local: Vec::new(),
search_results_server: Vec::new(),
search_token: 0,
is_server_searching: false,
search_selection_index: None,
}
}
pub fn set_json_config(&mut self, config: JsonColorConfig) {
self.json_formatter = JsonFormatter::new(config);
}
pub fn reset_pagination(&mut self) {
self.keys_cursor = None;
self.has_more_keys = false;
self.total_key_count = None;
self.is_loading_keys = false;
self.keys.clear();
self.selected_value = None;
self.selected_key_index = None;
self.reset_search();
}
pub fn reset_search(&mut self) {
self.is_searching = false;
self.search_query.clear();
self.search_results_local.clear();
self.search_results_server.clear();
self.search_token = self.search_token.wrapping_add(1);
self.is_server_searching = false;
self.search_selection_index = None;
}
pub fn start_search(&mut self) {
self.is_searching = true;
self.search_query.clear();
self.search_results_local.clear();
self.search_results_server.clear();
self.search_token = self.search_token.wrapping_add(1);
self.is_server_searching = false;
self.search_selection_index = None;
}
pub fn get_preferred_indices_for_filling(&self, center: usize, count: usize) -> Vec<usize> {
let total = self.keys.len();
let n = self.viewport_height;
let mut preferred_indices: Vec<usize> = Vec::with_capacity(count);
if total > 0 {
let start = center.saturating_sub(n);
let end = (center + n).min(total);
for i in start..end {
if self.keys.get(i).and_then(|k| k.as_ref()).is_none() {
preferred_indices.push(i);
if preferred_indices.len() >= count {
return preferred_indices;
}
}
}
if preferred_indices.len() < count && center < n {
let wrap_start = total.saturating_sub(n);
for i in wrap_start..total {
if self.keys.get(i).and_then(|k| k.as_ref()).is_none() {
preferred_indices.push(i);
if preferred_indices.len() >= count {
return preferred_indices;
}
}
}
}
if preferred_indices.len() < count && center + n >= total {
for i in 0..n.min(total) {
if self.keys.get(i).and_then(|k| k.as_ref()).is_none() {
preferred_indices.push(i);
if preferred_indices.len() >= count {
return preferred_indices;
}
}
}
}
}
preferred_indices
}
pub fn needs_loading_around(&self, index: usize) -> bool {
if self.is_loading_keys || (!self.has_more_keys && self.total_key_count.is_some()) {
if !self.has_more_keys && self.keys.iter().all(|k| k.is_some()) {
return false;
}
if !self.has_more_keys {
return false;
}
}
if self.is_loading_keys {
return false;
}
let n = self.viewport_height;
let total = self.keys.len();
if total == 0 {
return self.total_key_count.is_none();
}
let start = index.saturating_sub(n);
let end = (index + n).min(total);
for i in start..end {
if self.keys.get(i).and_then(|k| k.as_ref()).is_none() {
return true;
}
}
if index < n {
let wrap_start = total.saturating_sub(n);
for i in wrap_start..total {
if self.keys.get(i).and_then(|k| k.as_ref()).is_none() {
return true;
}
}
}
if index + n >= total {
for i in 0..n.min(total) {
if self.keys.get(i).and_then(|k| k.as_ref()).is_none() {
return true;
}
}
}
false
}
pub fn clear_error(&mut self) {
self.error_message = None;
}
}
impl Default for AppState {
fn default() -> Self {
Self::new()
}
}