the search tui is not working yet

This commit is contained in:
filipriec
2025-06-11 22:08:23 +02:00
parent c9131d4457
commit 3c0af05a3c
5 changed files with 47 additions and 55 deletions

View File

@@ -1,23 +1,19 @@
// src/state/pages/form.rs
use std::collections::HashMap; // NEW
use std::collections::HashMap;
use crate::config::colors::themes::Theme;
use ratatui::layout::Rect;
use ratatui::Frame;
use crate::state::app::highlight::HighlightState;
use crate::state::pages::canvas_state::CanvasState;
use crate::state::app::state::AppState;
use crate::components::common::search_palette::render_search_palette;
pub struct FormState {
pub id: i64,
// NEW fields for dynamic table context
pub profile_name: String,
pub table_name: String,
pub total_count: u64,
pub current_position: u64, // 1-based index, 0 or total_count + 1 for new entry
pub fields: Vec<String>, // Already dynamic, which is good
pub current_position: u64,
pub fields: Vec<String>,
pub values: Vec<String>,
pub current_field: usize,
pub has_unsaved_changes: bool,
@@ -25,9 +21,6 @@ pub struct FormState {
}
impl FormState {
/// Creates a new, empty FormState for a given table.
/// The position defaults to 1, representing either the first record
/// or the position for a new entry if the table is empty.
pub fn new(
profile_name: String,
table_name: String,
@@ -35,11 +28,10 @@ impl FormState {
) -> Self {
let values = vec![String::new(); fields.len()];
FormState {
id: 0, // Default to 0, indicating a new or unloaded record
id: 0,
profile_name,
table_name,
total_count: 0, // Will be fetched after initialization
// FIX: Default to 1. A position of 0 is an invalid state.
total_count: 0,
current_position: 1,
fields,
values,
@@ -49,6 +41,7 @@ impl FormState {
}
}
// This signature is now correct and only deals with form-related state.
pub fn render(
&self,
f: &mut Frame,
@@ -56,7 +49,6 @@ impl FormState {
theme: &Theme,
is_edit_mode: bool,
highlight_state: &HighlightState,
app_state: &AppState,
) {
let fields_str_slice: Vec<&str> =
self.fields.iter().map(|s| s.as_str()).collect();
@@ -65,7 +57,7 @@ impl FormState {
crate::components::form::form::render_form(
f,
area,
self, // Pass self as CanvasState
self,
&fields_str_slice,
&self.current_field,
&values_str_slice,
@@ -76,27 +68,19 @@ impl FormState {
self.total_count,
self.current_position,
);
if app_state.ui.show_search_palette {
if let Some(search_state) = &app_state.search_state {
render_search_palette(f, f.size(), theme, search_state);
}
}
}
/// Resets the form to a state for creating a new entry.
/// It clears all values and sets the position to be one after the last record.
// ... other methods are unchanged ...
pub fn reset_to_empty(&mut self) {
self.id = 0;
self.values.iter_mut().for_each(|v| v.clear());
self.current_field = 0;
self.current_cursor_pos = 0;
self.has_unsaved_changes = false;
// Set the position for a new entry.
if self.total_count > 0 {
self.current_position = self.total_count + 1;
} else {
self.current_position = 1; // If table is empty, new record is at position 1
self.current_position = 1;
}
}
@@ -113,26 +97,19 @@ impl FormState {
.expect("Invalid current_field index")
}
/// Updates the form's values from a data response and sets its position.
/// This is the single source of truth for populating the form after a data fetch.
pub fn update_from_response(
&mut self,
response_data: &HashMap<String, String>,
// FIX: Add new_position to make this method authoritative.
new_position: u64,
) {
// Create a new vector for the values, ensuring they are in the correct order.
self.values = self.fields.iter().map(|field_from_schema| {
// For each field from our schema, find the corresponding key in the
// response data by doing a case-insensitive comparison.
response_data
.iter()
.find(|(key_from_data, _)| key_from_data.eq_ignore_ascii_case(field_from_schema))
.map(|(_, value)| value.clone()) // If found, clone its value.
.unwrap_or_default() // If not found, use an empty string.
.map(|(_, value)| value.clone())
.unwrap_or_default()
}).collect();
// Now, do the same case-insensitive lookup for the 'id' field.
let id_str_opt = response_data
.iter()
.find(|(k, _)| k.eq_ignore_ascii_case("id"))
@@ -149,7 +126,6 @@ impl FormState {
self.id = 0;
}
// FIX: Set the position from the provided parameter.
self.current_position = new_position;
self.has_unsaved_changes = false;
self.current_field = 0;
@@ -175,12 +151,10 @@ impl CanvasState for FormState {
}
fn get_current_input(&self) -> &str {
// Re-use the struct's own method
FormState::get_current_input(self)
}
fn get_current_input_mut(&mut self) -> &mut String {
// Re-use the struct's own method
FormState::get_current_input_mut(self)
}