required table to access logic

This commit is contained in:
filipriec
2025-05-25 17:53:06 +02:00
parent 81235c67dc
commit bd7c97ca91

View File

@@ -5,7 +5,7 @@ use crate::config::binds::config::Config;
use crate::state::app::buffer::{BufferState, AppView}; use crate::state::app::buffer::{BufferState, AppView};
use crate::state::pages::add_table::{AddTableState, LinkDefinition}; use crate::state::pages::add_table::{AddTableState, LinkDefinition};
use ratatui::widgets::ListState; use ratatui::widgets::ListState;
use crate::state::pages::add_logic::AddLogicState; use crate::state::pages::add_logic::{AddLogicState, AddLogicFocus}; // Added AddLogicFocus import
// Helper functions list_select_next and list_select_previous remain the same // Helper functions list_select_next and list_select_previous remain the same
fn list_select_next(list_state: &mut ListState, item_count: usize) { fn list_select_next(list_state: &mut ListState, item_count: usize) {
@@ -90,11 +90,11 @@ pub fn handle_admin_navigation(
} }
Some("select") => { Some("select") => {
admin_state.selected_profile_index = admin_state.profile_list_state.selected(); admin_state.selected_profile_index = admin_state.profile_list_state.selected();
admin_state.selected_table_index = None; admin_state.selected_table_index = None; // Deselect table when profile changes
if let Some(profile_idx) = admin_state.selected_profile_index { if let Some(profile_idx) = admin_state.selected_profile_index {
if let Some(profile) = app_state.profile_tree.profiles.get(profile_idx) { if let Some(profile) = app_state.profile_tree.profiles.get(profile_idx) {
if !profile.tables.is_empty() { if !profile.tables.is_empty() {
admin_state.table_list_state.select(Some(0)); admin_state.table_list_state.select(Some(0)); // Auto-select first table for nav
} else { } else {
admin_state.table_list_state.select(None); admin_state.table_list_state.select(None);
} }
@@ -147,7 +147,7 @@ pub fn handle_admin_navigation(
} else { } else {
*command_message = "No tables in selected profile.".to_string(); *command_message = "No tables in selected profile.".to_string();
} }
admin_state.current_focus = AdminFocus::Tables; admin_state.current_focus = AdminFocus::Tables; // Stay in Tables pane if no tables to enter
} }
handled = true; handled = true;
} }
@@ -205,10 +205,9 @@ pub fn handle_admin_navigation(
handled = true; handled = true;
} }
} }
Some("select") => { Some("select") => { // This is for persistently selecting a table with [*]
admin_state.selected_table_index = admin_state.table_list_state.selected(); admin_state.selected_table_index = admin_state.table_list_state.selected();
let table_name = admin_state.selected_profile_index let table_name = admin_state.selected_profile_index
.or_else(|| admin_state.profile_list_state.selected())
.and_then(|p_idx| app_state.profile_tree.profiles.get(p_idx)) .and_then(|p_idx| app_state.profile_tree.profiles.get(p_idx))
.and_then(|p| admin_state.selected_table_index.and_then(|t_idx| p.tables.get(t_idx))) .and_then(|p| admin_state.selected_table_index.and_then(|t_idx| p.tables.get(t_idx)))
.map_or("N/A", |t| t.name.as_str()); .map_or("N/A", |t| t.name.as_str());
@@ -224,30 +223,44 @@ pub fn handle_admin_navigation(
} }
} }
AdminFocus::Button1 => { AdminFocus::Button1 => { // Add Logic Button
match action.as_deref() { match action.as_deref() {
Some("select") => { Some("select") => { // Typically "Enter" key
let mut logic_state_profile_name = "None (Global)".to_string();
let mut selected_table_name_for_logic: Option<String> = None;
if let Some(p_idx) = admin_state.selected_profile_index { if let Some(p_idx) = admin_state.selected_profile_index {
if let Some(profile) = app_state.profile_tree.profiles.get(p_idx) { if let Some(profile) = app_state.profile_tree.profiles.get(p_idx) {
logic_state_profile_name = profile.name.clone();
if let Some(t_idx) = admin_state.selected_table_index { if let Some(t_idx) = admin_state.selected_table_index {
if let Some(table) = profile.tables.get(t_idx) { if let Some(table) = profile.tables.get(t_idx) {
selected_table_name_for_logic = Some(table.name.clone()); // Both profile and table are selected, proceed
}
}
}
}
admin_state.add_logic_state = AddLogicState { admin_state.add_logic_state = AddLogicState {
profile_name: logic_state_profile_name.clone(), profile_name: profile.name.clone(),
selected_table_name: selected_table_name_for_logic, selected_table_name: Some(table.name.clone()),
// selected_table_id: table.id, // If you have table IDs
editor_keybinding_mode: config.editor.keybinding_mode.clone(), editor_keybinding_mode: config.editor.keybinding_mode.clone(),
current_focus: AddLogicFocus::default(), // Reset focus for the new screen
..AddLogicState::default() ..AddLogicState::default()
}; };
buffer_state.update_history(AppView::AddLogic); buffer_state.update_history(AppView::AddLogic); // Switch view
app_state.ui.focus_outside_canvas = false; app_state.ui.focus_outside_canvas = false; // Ensure canvas focus
*command_message = "Opening Add Logic...".to_string(); *command_message = format!(
"Opening Add Logic for table '{}' in profile '{}'...",
table.name, profile.name
);
} else {
// This case should ideally not be reached if indices are managed correctly
*command_message = "Error: Selected table data not found.".to_string();
}
} else {
// Profile is selected, but table is not
*command_message = "Select a table first!".to_string();
}
} else {
// This case should ideally not be reached if p_idx is valid
*command_message = "Error: Selected profile data not found.".to_string();
}
} else {
// Profile is not selected
*command_message = "Select a profile first!".to_string();
}
handled = true; handled = true;
} }
Some("previous_option") | Some("move_up") => { Some("previous_option") | Some("move_up") => {
@@ -264,20 +277,24 @@ pub fn handle_admin_navigation(
} }
} }
AdminFocus::Button2 => { AdminFocus::Button2 => { // Add Table Button
match action.as_deref() { match action.as_deref() {
Some("select") => { Some("select") => {
if let Some(p_idx) = admin_state.selected_profile_index { if let Some(p_idx) = admin_state.selected_profile_index {
if let Some(profile) = app_state.profile_tree.profiles.get(p_idx) { if let Some(profile) = app_state.profile_tree.profiles.get(p_idx) {
let selected_profile_name = profile.name.clone(); let selected_profile_name = profile.name.clone();
// Prepare links from the selected profile's existing tables
let available_links: Vec<LinkDefinition> = profile.tables.iter() let available_links: Vec<LinkDefinition> = profile.tables.iter()
.map(|table| LinkDefinition { .map(|table| LinkDefinition {
linked_table_name: table.name.clone(), linked_table_name: table.name.clone(),
is_required: false, selected: false, is_required: false, // Default, can be changed in AddTable screen
selected: false,
}).collect(); }).collect();
admin_state.add_table_state = AddTableState { admin_state.add_table_state = AddTableState {
profile_name: selected_profile_name, links: available_links, profile_name: selected_profile_name,
..AddTableState::default() links: available_links,
..AddTableState::default() // Reset other fields
}; };
buffer_state.update_history(AppView::AddTable); buffer_state.update_history(AppView::AddTable);
app_state.ui.focus_outside_canvas = false; app_state.ui.focus_outside_canvas = false;
@@ -306,9 +323,10 @@ pub fn handle_admin_navigation(
} }
} }
AdminFocus::Button3 => { AdminFocus::Button3 => { // Change Table Button
match action.as_deref() { match action.as_deref() {
Some("select") => { Some("select") => {
// Future: Logic to load selected table into AddTableState for editing
*command_message = "Action: Change Table (Not Implemented)".to_string(); *command_message = "Action: Change Table (Not Implemented)".to_string();
handled = true; handled = true;
} }