fixed form state removed, but not won, aint working yet
This commit is contained in:
@@ -220,66 +220,90 @@ impl EventHandler {
|
||||
async fn handle_search_palette_event(
|
||||
&mut self,
|
||||
key_event: KeyEvent,
|
||||
form_state: &mut FormState,
|
||||
app_state: &mut AppState,
|
||||
) -> Result<EventOutcome> {
|
||||
let mut should_close = false;
|
||||
let mut outcome_message = String::new();
|
||||
let mut trigger_search = false;
|
||||
|
||||
if let Some(search_state) = app_state.search_state.as_mut() {
|
||||
match key_event.code {
|
||||
KeyCode::Esc => {
|
||||
should_close = true;
|
||||
outcome_message = "Search cancelled".to_string();
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
if let Some(selected_hit) =
|
||||
search_state.results.get(search_state.selected_index)
|
||||
{
|
||||
if let Ok(data) = serde_json::from_str::<
|
||||
std::collections::HashMap<String, String>,
|
||||
>(&selected_hit.content_json)
|
||||
{
|
||||
let detached_pos = form_state.total_count + 2;
|
||||
form_state
|
||||
.update_from_response(&data, detached_pos);
|
||||
}
|
||||
// Step 1: Handle search_state logic in a short scope
|
||||
let (maybe_data, maybe_id) = {
|
||||
if let Some(search_state) = app_state.search_state.as_mut() {
|
||||
match key_event.code {
|
||||
KeyCode::Esc => {
|
||||
should_close = true;
|
||||
outcome_message =
|
||||
format!("Loaded record ID {}", selected_hit.id);
|
||||
outcome_message = "Search cancelled".to_string();
|
||||
(None, None)
|
||||
}
|
||||
}
|
||||
KeyCode::Up => search_state.previous_result(),
|
||||
KeyCode::Down => search_state.next_result(),
|
||||
KeyCode::Char(c) => {
|
||||
search_state
|
||||
.input
|
||||
.insert(search_state.cursor_position, c);
|
||||
search_state.cursor_position += 1;
|
||||
trigger_search = true;
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
if search_state.cursor_position > 0 {
|
||||
search_state.cursor_position -= 1;
|
||||
search_state.input.remove(search_state.cursor_position);
|
||||
trigger_search = true;
|
||||
KeyCode::Enter => {
|
||||
if let Some(selected_hit) =
|
||||
search_state.results.get(search_state.selected_index)
|
||||
{
|
||||
if let Ok(data) = serde_json::from_str::<
|
||||
std::collections::HashMap<String, String>,
|
||||
>(&selected_hit.content_json)
|
||||
{
|
||||
(Some(data), Some(selected_hit.id))
|
||||
} else {
|
||||
(None, None)
|
||||
}
|
||||
} else {
|
||||
(None, None)
|
||||
}
|
||||
}
|
||||
}
|
||||
KeyCode::Left => {
|
||||
search_state.cursor_position =
|
||||
search_state.cursor_position.saturating_sub(1);
|
||||
}
|
||||
KeyCode::Right => {
|
||||
if search_state.cursor_position < search_state.input.len()
|
||||
{
|
||||
KeyCode::Up => {
|
||||
search_state.previous_result();
|
||||
(None, None)
|
||||
}
|
||||
KeyCode::Down => {
|
||||
search_state.next_result();
|
||||
(None, None)
|
||||
}
|
||||
KeyCode::Char(c) => {
|
||||
search_state.input.insert(search_state.cursor_position, c);
|
||||
search_state.cursor_position += 1;
|
||||
trigger_search = true;
|
||||
(None, None)
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
if search_state.cursor_position > 0 {
|
||||
search_state.cursor_position -= 1;
|
||||
search_state.input.remove(search_state.cursor_position);
|
||||
trigger_search = true;
|
||||
}
|
||||
(None, None)
|
||||
}
|
||||
KeyCode::Left => {
|
||||
search_state.cursor_position =
|
||||
search_state.cursor_position.saturating_sub(1);
|
||||
(None, None)
|
||||
}
|
||||
KeyCode::Right => {
|
||||
if search_state.cursor_position < search_state.input.len() {
|
||||
search_state.cursor_position += 1;
|
||||
}
|
||||
(None, None)
|
||||
}
|
||||
_ => (None, None),
|
||||
}
|
||||
_ => {}
|
||||
} else {
|
||||
(None, None)
|
||||
}
|
||||
};
|
||||
|
||||
if trigger_search {
|
||||
// Step 2: Now safe to borrow form_state
|
||||
if let (Some(data), Some(id)) = (maybe_data, maybe_id) {
|
||||
if let Some(fs) = app_state.form_state_mut() {
|
||||
let detached_pos = fs.total_count + 2;
|
||||
fs.update_from_response(&data, detached_pos);
|
||||
}
|
||||
should_close = true;
|
||||
outcome_message = format!("Loaded record ID {}", id);
|
||||
}
|
||||
|
||||
// Step 3: Trigger async search if needed
|
||||
if trigger_search {
|
||||
if let Some(search_state) = app_state.search_state.as_mut() {
|
||||
search_state.is_loading = true;
|
||||
search_state.results.clear();
|
||||
search_state.selected_index = 0;
|
||||
@@ -289,10 +313,7 @@ impl EventHandler {
|
||||
let sender = self.search_result_sender.clone();
|
||||
let mut grpc_client = self.grpc_client.clone();
|
||||
|
||||
info!(
|
||||
"--- 1. Spawning search task for query: '{}' ---",
|
||||
query
|
||||
);
|
||||
info!("--- 1. Spawning search task for query: '{}' ---", query);
|
||||
tokio::spawn(async move {
|
||||
info!("--- 2. Background task started. ---");
|
||||
match grpc_client.search_table(table_name, query).await {
|
||||
@@ -328,7 +349,6 @@ impl EventHandler {
|
||||
config: &Config,
|
||||
terminal: &mut TerminalCore,
|
||||
command_handler: &mut CommandHandler,
|
||||
form_state: &mut FormState,
|
||||
auth_state: &mut AuthState,
|
||||
login_state: &mut LoginState,
|
||||
register_state: &mut RegisterState,
|
||||
@@ -339,13 +359,7 @@ impl EventHandler {
|
||||
) -> Result<EventOutcome> {
|
||||
if app_state.ui.show_search_palette {
|
||||
if let Event::Key(key_event) = event {
|
||||
return self
|
||||
.handle_search_palette_event(
|
||||
key_event,
|
||||
form_state,
|
||||
app_state,
|
||||
)
|
||||
.await;
|
||||
return self.handle_search_palette_event(key_event, app_state).await;
|
||||
}
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
@@ -570,7 +584,6 @@ impl EventHandler {
|
||||
let nav_outcome = navigation::handle_navigation_event(
|
||||
key_event,
|
||||
config,
|
||||
form_state,
|
||||
app_state,
|
||||
login_state,
|
||||
register_state,
|
||||
@@ -580,9 +593,7 @@ impl EventHandler {
|
||||
&mut self.command_input,
|
||||
&mut self.command_message,
|
||||
&mut self.navigation_state,
|
||||
)
|
||||
.await;
|
||||
|
||||
).await;
|
||||
match nav_outcome {
|
||||
Ok(EventOutcome::ButtonSelected { context, index }) => {
|
||||
let message = match context {
|
||||
@@ -692,7 +703,6 @@ impl EventHandler {
|
||||
return self
|
||||
.handle_core_action(
|
||||
action,
|
||||
form_state,
|
||||
auth_state,
|
||||
login_state,
|
||||
register_state,
|
||||
@@ -739,7 +749,6 @@ impl EventHandler {
|
||||
return self
|
||||
.handle_core_action(
|
||||
action,
|
||||
form_state,
|
||||
auth_state,
|
||||
login_state,
|
||||
register_state,
|
||||
@@ -792,15 +801,18 @@ impl EventHandler {
|
||||
}
|
||||
|
||||
if config.is_command_execute(key_code, modifiers) {
|
||||
let mut current_position = form_state.current_position;
|
||||
let total_count = form_state.total_count;
|
||||
let (mut current_position, total_count) = if let Some(fs) = app_state.form_state() {
|
||||
(fs.current_position, fs.total_count)
|
||||
} else {
|
||||
(1, 0)
|
||||
};
|
||||
|
||||
let outcome = command_mode::handle_command_event(
|
||||
key_event,
|
||||
config,
|
||||
app_state,
|
||||
login_state,
|
||||
register_state,
|
||||
form_state,
|
||||
&mut self.command_input,
|
||||
&mut self.command_message,
|
||||
&mut self.grpc_client,
|
||||
@@ -808,9 +820,10 @@ impl EventHandler {
|
||||
terminal,
|
||||
&mut current_position,
|
||||
total_count,
|
||||
)
|
||||
.await?;
|
||||
form_state.current_position = current_position;
|
||||
).await?;
|
||||
if let Some(fs) = app_state.form_state_mut() {
|
||||
fs.current_position = current_position;
|
||||
}
|
||||
self.command_mode = false;
|
||||
self.key_sequence_tracker.reset();
|
||||
let new_mode = ModeManager::derive_mode(
|
||||
@@ -921,7 +934,6 @@ impl EventHandler {
|
||||
async fn handle_core_action(
|
||||
&mut self,
|
||||
action: &str,
|
||||
form_state: &mut FormState,
|
||||
auth_state: &mut AuthState,
|
||||
login_state: &mut LoginState,
|
||||
register_state: &mut RegisterState,
|
||||
@@ -940,12 +952,15 @@ impl EventHandler {
|
||||
.await?;
|
||||
Ok(EventOutcome::Ok(message))
|
||||
} else {
|
||||
let save_outcome = crate::tui::functions::common::form::save(
|
||||
app_state,
|
||||
form_state,
|
||||
&mut self.grpc_client,
|
||||
)
|
||||
.await?;
|
||||
let save_outcome = if let Some(fs) = app_state.form_state_mut() {
|
||||
crate::tui::functions::common::form::save(
|
||||
app_state,
|
||||
&mut self.grpc_client,
|
||||
)
|
||||
.await?
|
||||
} else {
|
||||
SaveOutcome::NoChange
|
||||
};
|
||||
let message = match save_outcome {
|
||||
SaveOutcome::NoChange => "No changes to save.".to_string(),
|
||||
SaveOutcome::UpdatedExisting => "Entry updated.".to_string(),
|
||||
@@ -975,10 +990,8 @@ impl EventHandler {
|
||||
} else {
|
||||
let save_outcome = crate::tui::functions::common::form::save(
|
||||
app_state,
|
||||
form_state,
|
||||
&mut self.grpc_client,
|
||||
)
|
||||
.await?;
|
||||
).await?;
|
||||
match save_outcome {
|
||||
SaveOutcome::NoChange => "No changes to save.".to_string(),
|
||||
SaveOutcome::UpdatedExisting => "Entry updated.".to_string(),
|
||||
@@ -1003,13 +1016,17 @@ impl EventHandler {
|
||||
register_state,
|
||||
app_state,
|
||||
)
|
||||
.await
|
||||
.await
|
||||
} else {
|
||||
crate::tui::functions::common::form::revert(
|
||||
form_state,
|
||||
&mut self.grpc_client,
|
||||
)
|
||||
.await?
|
||||
if let Some(fs) = app_state.form_state_mut() {
|
||||
crate::tui::functions::common::form::revert(
|
||||
app_state,
|
||||
&mut self.grpc_client,
|
||||
)
|
||||
.await?
|
||||
} else {
|
||||
"Nothing to revert".to_string()
|
||||
}
|
||||
};
|
||||
Ok(EventOutcome::Ok(message))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user