suggestions in the dropdown menu now works amazingly well

This commit is contained in:
filipriec
2025-06-15 23:11:27 +02:00
parent 0e69df8282
commit 512e7fb9e7
7 changed files with 541 additions and 118 deletions

View File

@@ -1,16 +1,19 @@
// src/modes/canvas/edit.rs
use crate::config::binds::config::Config;
use crate::functions::modes::edit::{
add_logic_e, add_table_e, auth_e, form_e,
};
use crate::modes::handlers::event::EventOutcome;
use crate::services::grpc_client::GrpcClient;
use crate::state::app::state::AppState;
use crate::state::pages::admin::AdminState;
use crate::state::pages::{
auth::{LoginState, RegisterState},
canvas_state::CanvasState,
form::FormState,
};
use crate::state::pages::form::FormState;
use crate::state::pages::admin::AdminState;
use crate::modes::handlers::event::EventOutcome;
use crate::functions::modes::edit::{add_logic_e, auth_e, form_e, add_table_e};
use crate::state::app::state::AppState;
use anyhow::Result;
use common::proto::multieko2::search::search_response::Hit;
use crossterm::event::KeyEvent;
use tracing::debug;
@@ -33,20 +36,25 @@ pub async fn handle_edit_event(
grpc_client: &mut GrpcClient,
app_state: &AppState,
) -> Result<EditEventOutcome> {
if app_state.ui.show_form && form_state.autocomplete_active {
if let Some(action) = config.get_edit_action_for_key(key.code, key.modifiers) {
if app_state.ui.show_form && form_state.autocomplete_active {
if let Some(action) =
config.get_edit_action_for_key(key.code, key.modifiers)
{
match action {
"suggestion_down" => {
if !form_state.autocomplete_suggestions.is_empty() {
let current = form_state.selected_suggestion_index.unwrap_or(0);
let next = (current + 1) % form_state.autocomplete_suggestions.len();
let current =
form_state.selected_suggestion_index.unwrap_or(0);
let next = (current + 1)
% form_state.autocomplete_suggestions.len();
form_state.selected_suggestion_index = Some(next);
}
return Ok(EditEventOutcome::Message(String::new()));
}
"suggestion_up" => {
if !form_state.autocomplete_suggestions.is_empty() {
let current = form_state.selected_suggestion_index.unwrap_or(0);
let current =
form_state.selected_suggestion_index.unwrap_or(0);
let prev = if current == 0 {
form_state.autocomplete_suggestions.len() - 1
} else {
@@ -58,19 +66,31 @@ if app_state.ui.show_form && form_state.autocomplete_active {
}
"exit" => {
form_state.deactivate_autocomplete();
return Ok(EditEventOutcome::Message("Autocomplete cancelled".to_string()));
return Ok(EditEventOutcome::Message(
"Autocomplete cancelled".to_string(),
));
}
"enter_decider" => {
if let Some(selected_idx) = form_state.selected_suggestion_index {
if let Some(selection) = form_state.autocomplete_suggestions.get(selected_idx).cloned() {
let current_input = form_state.get_current_input_mut();
*current_input = selection;
if let Some(selected_idx) =
form_state.selected_suggestion_index
{
if let Some(selection) = form_state
.autocomplete_suggestions
.get(selected_idx)
.cloned()
{
let current_input =
form_state.get_current_input_mut();
// Use the ID from the Hit struct for the field value
*current_input = selection.id.to_string();
let new_cursor_pos = current_input.len();
form_state.set_current_cursor_pos(new_cursor_pos);
*ideal_cursor_column = new_cursor_pos;
form_state.deactivate_autocomplete();
form_state.set_has_unsaved_changes(true);
return Ok(EditEventOutcome::Message("Selection made".to_string()));
return Ok(EditEventOutcome::Message(
"Selection made".to_string(),
));
}
}
// If no selection, fall through to default behavior
@@ -80,7 +100,7 @@ if app_state.ui.show_form && form_state.autocomplete_active {
}
}
}
if let Some("enter_command_mode") = config.get_action_for_key_in_mode(
&config.keybindings.global,
key.code,
@@ -91,171 +111,410 @@ if app_state.ui.show_form && form_state.autocomplete_active {
));
}
if let Some(action) = config.get_action_for_key_in_mode(
&config.keybindings.common,
key.code,
key.modifiers,
).as_deref() {
if let Some(action) = config
.get_action_for_key_in_mode(
&config.keybindings.common,
key.code,
key.modifiers,
)
.as_deref()
{
if matches!(action, "save" | "revert") {
let message_string: String = if app_state.ui.show_login {
auth_e::execute_common_action(action, login_state, grpc_client, current_position, total_count).await?
auth_e::execute_common_action(
action,
login_state,
grpc_client,
current_position,
total_count,
)
.await?
} else if app_state.ui.show_register {
auth_e::execute_common_action(action, register_state, grpc_client, current_position, total_count).await?
auth_e::execute_common_action(
action,
register_state,
grpc_client,
current_position,
total_count,
)
.await?
} else if app_state.ui.show_add_table {
format!("Action '{}' not implemented for Add Table in edit mode.", action)
format!(
"Action '{}' not implemented for Add Table in edit mode.",
action
)
} else if app_state.ui.show_add_logic {
format!("Action '{}' not implemented for Add Logic in edit mode.", action)
format!(
"Action '{}' not implemented for Add Logic in edit mode.",
action
)
} else {
let outcome = form_e::execute_common_action(action, form_state, grpc_client).await?;
let outcome =
form_e::execute_common_action(action, form_state, grpc_client)
.await?;
match outcome {
EventOutcome::Ok(msg) | EventOutcome::DataSaved(_, msg) => msg,
_ => format!("Unexpected outcome from common action: {:?}", outcome),
_ => format!(
"Unexpected outcome from common action: {:?}",
outcome
),
}
};
return Ok(EditEventOutcome::Message(message_string));
}
}
if let Some(action_str) = config.get_edit_action_for_key(key.code, key.modifiers).as_deref() {
tracing::info!("[Handler] `handle_edit_event` received action: '{}'", action_str);
if let Some(action_str) =
config.get_edit_action_for_key(key.code, key.modifiers).as_deref()
{
tracing::info!(
"[Handler] `handle_edit_event` received action: '{}'",
action_str
);
// --- MANUAL AUTOCOMPLETE TRIGGER ---
if action_str == "trigger_autocomplete" {
tracing::info!("[Handler] Action is 'trigger_autocomplete'. Checking conditions..."); // <-- ADD THIS
tracing::info!("[Handler] Action is 'trigger_autocomplete'. Checking conditions...");
if app_state.ui.show_form {
tracing::info!("[Handler] In form view. Checking field..."); // <-- ADD THIS
if let Some(field_def) = form_state.fields.get(form_state.current_field) {
if field_def.is_link {
tracing::info!("[Handler] Field '{}' is a link. Activating autocomplete.", field_def.display_name); // <-- ADD THIS
form_state.autocomplete_active = true;
form_state.selected_suggestion_index = Some(0);
form_state.autocomplete_suggestions = vec![
"Hardcoded Supplier A".to_string(),
"Hardcoded Supplier B".to_string(),
"Hardcoded Company C".to_string(),
];
return Ok(EditEventOutcome::Message("Autocomplete triggered".to_string()));
// Use our new field to get the table to search
if let Some(target_table) = &field_def.link_target_table {
tracing::info!(
"[Handler] Field '{}' is a link to table '{}'. Triggering search.",
field_def.display_name,
target_table
);
// Set loading state and activate autocomplete UI
form_state.autocomplete_loading = true;
form_state.autocomplete_active = true;
form_state.autocomplete_suggestions.clear();
form_state.selected_suggestion_index = None;
let query = form_state.get_current_input().to_string();
let table_to_search = target_table.clone();
// Perform the gRPC call asynchronously
match grpc_client.search_table(table_to_search, query).await {
Ok(response) => {
form_state.autocomplete_suggestions = response.hits;
if !form_state.autocomplete_suggestions.is_empty() {
form_state.selected_suggestion_index = Some(0);
}
form_state.autocomplete_loading = false; // Turn off loading
return Ok(EditEventOutcome::Message(format!(
"Found {} suggestions.",
form_state.autocomplete_suggestions.len()
)));
}
Err(e) => {
// Handle errors gracefully
form_state.autocomplete_loading = false;
form_state.deactivate_autocomplete(); // Close UI on error
let error_msg = format!("Search failed: {}", e);
tracing::error!("[Handler] {}", error_msg);
return Ok(EditEventOutcome::Message(error_msg));
}
}
} else {
let msg = "Field is a link, but target table is not defined.".to_string();
tracing::error!("[Handler] {}", msg);
return Ok(EditEventOutcome::Message(msg));
}
} else {
tracing::error!("[Handler] Field '{}' is NOT a link.", field_def.display_name); // <-- ADD THIS
let msg = format!("Field '{}' is not a linkable field.", field_def.display_name);
tracing::error!("[Handler] {}", msg);
return Ok(EditEventOutcome::Message(msg));
}
}
} else {
tracing::error!("[Handler] Not in form view. Cannot trigger autocomplete."); // <-- ADD THIS
}
return Ok(EditEventOutcome::Message("Not a linkable field".to_string()));
// Fallback message if not in a form or something went wrong
return Ok(EditEventOutcome::Message("Autocomplete not available here.".to_string()));
}
// --- END OF NEW LOGIC ---
if action_str == "enter_decider" {
let effective_action = if app_state.ui.show_register
&& register_state.in_suggestion_mode
&& register_state.current_field() == 4 {
&& register_state.current_field() == 4
{
"select_suggestion"
} else if app_state.ui.show_add_logic
&& admin_state.add_logic_state.in_target_column_suggestion_mode
&& admin_state.add_logic_state.current_field() == 1 {
&& admin_state
.add_logic_state
.in_target_column_suggestion_mode
&& admin_state.add_logic_state.current_field() == 1
{
"select_suggestion"
} else {
"next_field"
};
let msg = if app_state.ui.show_login {
auth_e::execute_edit_action(effective_action, key, login_state, ideal_cursor_column).await?
auth_e::execute_edit_action(
effective_action,
key,
login_state,
ideal_cursor_column,
)
.await?
} else if app_state.ui.show_add_table {
add_table_e::execute_edit_action(effective_action, key, &mut admin_state.add_table_state, ideal_cursor_column).await?
add_table_e::execute_edit_action(
effective_action,
key,
&mut admin_state.add_table_state,
ideal_cursor_column,
)
.await?
} else if app_state.ui.show_add_logic {
add_logic_e::execute_edit_action(effective_action, key, &mut admin_state.add_logic_state, ideal_cursor_column).await?
add_logic_e::execute_edit_action(
effective_action,
key,
&mut admin_state.add_logic_state,
ideal_cursor_column,
)
.await?
} else if app_state.ui.show_register {
auth_e::execute_edit_action(effective_action, key, register_state, ideal_cursor_column).await?
auth_e::execute_edit_action(
effective_action,
key,
register_state,
ideal_cursor_column,
)
.await?
} else {
form_e::execute_edit_action(effective_action, key, form_state, ideal_cursor_column).await?
form_e::execute_edit_action(
effective_action,
key,
form_state,
ideal_cursor_column,
)
.await?
};
return Ok(EditEventOutcome::Message(msg));
}
if action_str == "exit" {
if app_state.ui.show_register && register_state.in_suggestion_mode {
let msg = auth_e::execute_edit_action("exit_suggestion_mode", key, register_state, ideal_cursor_column).await?;
let msg = auth_e::execute_edit_action(
"exit_suggestion_mode",
key,
register_state,
ideal_cursor_column,
)
.await?;
return Ok(EditEventOutcome::Message(msg));
} else if app_state.ui.show_add_logic && admin_state.add_logic_state.in_target_column_suggestion_mode {
admin_state.add_logic_state.in_target_column_suggestion_mode = false;
admin_state.add_logic_state.show_target_column_suggestions = false;
admin_state.add_logic_state.selected_target_column_suggestion_index = None;
return Ok(EditEventOutcome::Message("Exited column suggestions".to_string()));
} else if app_state.ui.show_add_logic
&& admin_state
.add_logic_state
.in_target_column_suggestion_mode
{
admin_state
.add_logic_state
.in_target_column_suggestion_mode = false;
admin_state
.add_logic_state
.show_target_column_suggestions = false;
admin_state
.add_logic_state
.selected_target_column_suggestion_index = None;
return Ok(EditEventOutcome::Message(
"Exited column suggestions".to_string(),
));
} else {
return Ok(EditEventOutcome::ExitEditMode);
}
}
if app_state.ui.show_add_logic && admin_state.add_logic_state.current_field() == 1 {
if app_state.ui.show_add_logic
&& admin_state.add_logic_state.current_field() == 1
{
if action_str == "suggestion_down" {
if !admin_state.add_logic_state.in_target_column_suggestion_mode {
if let Some(profile_name) = admin_state.add_logic_state.profile_name.clone().into() {
if let Some(table_name) = admin_state.add_logic_state.selected_table_name.clone() {
if !admin_state
.add_logic_state
.in_target_column_suggestion_mode
{
if let Some(profile_name) =
admin_state.add_logic_state.profile_name.clone().into()
{
if let Some(table_name) = admin_state
.add_logic_state
.selected_table_name
.clone()
{
debug!("Fetching table structure for autocomplete: Profile='{}', Table='{}'", profile_name, table_name);
match grpc_client.get_table_structure(profile_name, table_name).await {
match grpc_client
.get_table_structure(profile_name, table_name)
.await
{
Ok(ts_response) => {
admin_state.add_logic_state.table_columns_for_suggestions =
ts_response.columns.into_iter().map(|c| c.name).collect();
admin_state.add_logic_state.update_target_column_suggestions();
if !admin_state.add_logic_state.target_column_suggestions.is_empty() {
admin_state.add_logic_state.in_target_column_suggestion_mode = true;
return Ok(EditEventOutcome::Message("Column suggestions shown".to_string()));
admin_state
.add_logic_state
.table_columns_for_suggestions =
ts_response
.columns
.into_iter()
.map(|c| c.name)
.collect();
admin_state
.add_logic_state
.update_target_column_suggestions();
if !admin_state
.add_logic_state
.target_column_suggestions
.is_empty()
{
admin_state
.add_logic_state
.in_target_column_suggestion_mode =
true;
return Ok(EditEventOutcome::Message(
"Column suggestions shown"
.to_string(),
));
} else {
return Ok(EditEventOutcome::Message("No column suggestions for current input".to_string()));
return Ok(EditEventOutcome::Message(
"No column suggestions for current input"
.to_string(),
));
}
}
Err(e) => {
debug!("Error fetching table structure: {}", e);
admin_state.add_logic_state.table_columns_for_suggestions.clear();
admin_state.add_logic_state.update_target_column_suggestions();
return Ok(EditEventOutcome::Message(format!("Error fetching columns: {}", e)));
debug!(
"Error fetching table structure: {}",
e
);
admin_state
.add_logic_state
.table_columns_for_suggestions
.clear();
admin_state
.add_logic_state
.update_target_column_suggestions();
return Ok(EditEventOutcome::Message(
format!("Error fetching columns: {}", e),
));
}
}
} else {
return Ok(EditEventOutcome::Message("No table selected for column suggestions".to_string()));
return Ok(EditEventOutcome::Message(
"No table selected for column suggestions"
.to_string(),
));
}
} else {
return Ok(EditEventOutcome::Message("Profile name missing for column suggestions".to_string()));
return Ok(EditEventOutcome::Message(
"Profile name missing for column suggestions"
.to_string(),
));
}
} else {
let msg = add_logic_e::execute_edit_action(action_str, key, &mut admin_state.add_logic_state, ideal_cursor_column).await?;
let msg = add_logic_e::execute_edit_action(
action_str,
key,
&mut admin_state.add_logic_state,
ideal_cursor_column,
)
.await?;
return Ok(EditEventOutcome::Message(msg));
}
} else if admin_state.add_logic_state.in_target_column_suggestion_mode && action_str == "suggestion_up" {
let msg = add_logic_e::execute_edit_action(action_str, key, &mut admin_state.add_logic_state, ideal_cursor_column).await?;
} else if admin_state
.add_logic_state
.in_target_column_suggestion_mode
&& action_str == "suggestion_up"
{
let msg = add_logic_e::execute_edit_action(
action_str,
key,
&mut admin_state.add_logic_state,
ideal_cursor_column,
)
.await?;
return Ok(EditEventOutcome::Message(msg));
}
}
if app_state.ui.show_register && register_state.current_field() == 4 {
if !register_state.in_suggestion_mode && action_str == "suggestion_down" {
if !register_state.in_suggestion_mode
&& action_str == "suggestion_down"
{
register_state.update_role_suggestions();
if !register_state.role_suggestions.is_empty() {
register_state.in_suggestion_mode = true;
return Ok(EditEventOutcome::Message("Role suggestions shown".to_string()));
return Ok(EditEventOutcome::Message(
"Role suggestions shown".to_string(),
));
}
}
if register_state.in_suggestion_mode && matches!(action_str, "suggestion_down" | "suggestion_up") {
let msg = auth_e::execute_edit_action(action_str, key, register_state, ideal_cursor_column).await?;
if register_state.in_suggestion_mode
&& matches!(action_str, "suggestion_down" | "suggestion_up")
{
let msg = auth_e::execute_edit_action(
action_str,
key,
register_state,
ideal_cursor_column,
)
.await?;
return Ok(EditEventOutcome::Message(msg));
}
}
let msg = if app_state.ui.show_login {
auth_e::execute_edit_action(action_str, key, login_state, ideal_cursor_column).await?
auth_e::execute_edit_action(
action_str,
key,
login_state,
ideal_cursor_column,
)
.await?
} else if app_state.ui.show_add_table {
add_table_e::execute_edit_action(action_str, key, &mut admin_state.add_table_state, ideal_cursor_column).await?
add_table_e::execute_edit_action(
action_str,
key,
&mut admin_state.add_table_state,
ideal_cursor_column,
)
.await?
} else if app_state.ui.show_add_logic {
if !(admin_state.add_logic_state.in_target_column_suggestion_mode && matches!(action_str, "suggestion_down" | "suggestion_up")) {
add_logic_e::execute_edit_action(action_str, key, &mut admin_state.add_logic_state, ideal_cursor_column).await?
} else { String::new() }
if !(admin_state
.add_logic_state
.in_target_column_suggestion_mode
&& matches!(action_str, "suggestion_down" | "suggestion_up"))
{
add_logic_e::execute_edit_action(
action_str,
key,
&mut admin_state.add_logic_state,
ideal_cursor_column,
)
.await?
} else {
String::new()
}
} else if app_state.ui.show_register {
if !(register_state.in_suggestion_mode && matches!(action_str, "suggestion_down" | "suggestion_up")) {
auth_e::execute_edit_action(action_str, key, register_state, ideal_cursor_column).await?
} else { String::new() }
if !(register_state.in_suggestion_mode
&& matches!(action_str, "suggestion_down" | "suggestion_up"))
{
auth_e::execute_edit_action(
action_str,
key,
register_state,
ideal_cursor_column,
)
.await?
} else {
String::new()
}
} else {
form_e::execute_edit_action(action_str, key, form_state, ideal_cursor_column).await?
form_e::execute_edit_action(
action_str,
key,
form_state,
ideal_cursor_column,
)
.await?
};
return Ok(EditEventOutcome::Message(msg));
}
@@ -267,29 +526,65 @@ if app_state.ui.show_form && form_state.autocomplete_active {
register_state.selected_suggestion_index = None;
exited_suggestion_mode_for_typing = true;
}
if app_state.ui.show_add_logic && admin_state.add_logic_state.in_target_column_suggestion_mode {
if app_state.ui.show_add_logic
&& admin_state.add_logic_state.in_target_column_suggestion_mode
{
admin_state.add_logic_state.in_target_column_suggestion_mode = false;
admin_state.add_logic_state.show_target_column_suggestions = false;
admin_state.add_logic_state.selected_target_column_suggestion_index = None;
admin_state
.add_logic_state
.selected_target_column_suggestion_index = None;
exited_suggestion_mode_for_typing = true;
}
let mut char_insert_msg = if app_state.ui.show_login {
auth_e::execute_edit_action("insert_char", key, login_state, ideal_cursor_column).await?
auth_e::execute_edit_action(
"insert_char",
key,
login_state,
ideal_cursor_column,
)
.await?
} else if app_state.ui.show_add_table {
add_table_e::execute_edit_action("insert_char", key, &mut admin_state.add_table_state, ideal_cursor_column).await?
add_table_e::execute_edit_action(
"insert_char",
key,
&mut admin_state.add_table_state,
ideal_cursor_column,
)
.await?
} else if app_state.ui.show_add_logic {
add_logic_e::execute_edit_action("insert_char", key, &mut admin_state.add_logic_state, ideal_cursor_column).await?
add_logic_e::execute_edit_action(
"insert_char",
key,
&mut admin_state.add_logic_state,
ideal_cursor_column,
)
.await?
} else if app_state.ui.show_register {
auth_e::execute_edit_action("insert_char", key, register_state, ideal_cursor_column).await?
auth_e::execute_edit_action(
"insert_char",
key,
register_state,
ideal_cursor_column,
)
.await?
} else {
form_e::execute_edit_action("insert_char", key, form_state, ideal_cursor_column).await?
form_e::execute_edit_action(
"insert_char",
key,
form_state,
ideal_cursor_column,
)
.await?
};
if app_state.ui.show_register && register_state.current_field() == 4 {
register_state.update_role_suggestions();
}
if app_state.ui.show_add_logic && admin_state.add_logic_state.current_field() == 1 {
if app_state.ui.show_add_logic
&& admin_state.add_logic_state.current_field() == 1
{
admin_state.add_logic_state.update_target_column_suggestions();
}