diff --git a/client/src/services/ui_service.rs b/client/src/services/ui_service.rs index 6c94e42..55a3063 100644 --- a/client/src/services/ui_service.rs +++ b/client/src/services/ui_service.rs @@ -176,7 +176,7 @@ impl UiService { } } - // REFACTOR THIS FUNCTION + // TODO REFACTOR (maybe) pub async fn initialize_app_state_and_form( grpc_client: &mut GrpcClient, app_state: &mut AppState, @@ -185,28 +185,27 @@ impl UiService { .get_profile_tree() .await .context("Failed to get profile tree")?; + app_state.profile_tree = profile_tree; - let initial_profile_name = app_state + // Find first profile that contains tables + let (initial_profile_name, initial_table_name) = app_state .profile_tree .profiles - .first() - .map(|p| p.name.clone()) - .unwrap_or_else(|| "default".to_string()); - - let initial_table_name = app_state - .profile_tree - .profiles - .first() - .and_then(|p| p.tables.first().map(|t| t.name.clone())) - .unwrap_or_else(|| "2025_company_data1".to_string()); + .iter() + .find(|profile| !profile.tables.is_empty()) + .and_then(|profile| { + profile.tables.first().map(|table| { + (profile.name.clone(), table.name.clone()) + }) + }) + .ok_or_else(|| anyhow!("No profiles with tables found. Create a table first."))?; app_state.set_current_view_table( initial_profile_name.clone(), initial_table_name.clone(), ); - // NOW, just call our new central function. This avoids code duplication. let form_state = Self::load_table_view( grpc_client, app_state, @@ -215,7 +214,6 @@ impl UiService { ) .await?; - // The field names for the UI are derived from the new form_state let field_names = form_state.fields.iter().map(|f| f.display_name.clone()).collect(); Ok((initial_profile_name, initial_table_name, field_names))