@@ -28,8 +28,6 @@ use tui_logger::TuiWidgetEvent;
2828use crate :: tui:: state:: tabs:: flightsql:: FlightSQLConnectionStatus ;
2929use crate :: tui:: state:: tabs:: history:: Context ;
3030use crate :: tui:: ExecutionResultsBatch ;
31-
32- #[ cfg( feature = "flightsql" ) ]
3331use std:: sync:: Arc ;
3432
3533use super :: App ;
@@ -222,13 +220,47 @@ pub fn app_event_handler(app: &mut App, event: AppEvent) -> Result<()> {
222220 duration,
223221 batch,
224222 } = r;
223+
224+ let is_first_batch = app. state . sql_tab . current_page ( ) . is_none ( ) ;
225+
225226 app. state . sql_tab . add_batch ( batch) ;
227+
228+ if is_first_batch {
229+ app. state . sql_tab . next_page ( ) ;
230+ app. state . sql_tab . refresh_query_results_state ( ) ;
231+
232+ let history_query =
233+ HistoryQuery :: new ( Context :: Local , query. to_string ( ) , duration, None , None ) ;
234+ app. state . history_tab . add_to_history ( history_query) ;
235+ app. state . history_tab . refresh_history_table_state ( ) ;
236+ } else {
237+ app. state . sql_tab . refresh_query_results_state ( ) ;
238+
239+ // Check if we have enough data for the next page now
240+ // If not, automatically fetch another batch
241+ if let Some ( current_page) = app. state . sql_tab . current_page ( ) {
242+ let next_page = current_page + 1 ;
243+ if app. state . sql_tab . needs_more_batches_for_page ( next_page) {
244+ info ! ( "Still need more batches for page {}, fetching next batch" , next_page) ;
245+ let execution = Arc :: clone ( & app. execution ) ;
246+ let sql = query. clone ( ) ;
247+ let _event_tx = app. event_tx ( ) ;
248+ tokio:: spawn ( async move {
249+ execution. next_batch ( sql, _event_tx) . await ;
250+ } ) ;
251+ } else {
252+ // We now have enough data, advance to the page
253+ info ! ( "Sufficient data loaded, advancing to page {}" , next_page) ;
254+ if let Err ( e) = app. event_tx ( ) . send ( AppEvent :: ExecutionResultsNextPage ) {
255+ error ! ( "Error advancing to next page: {e}" ) ;
256+ }
257+ }
258+ }
259+ }
260+ }
261+ AppEvent :: ExecutionResultsNextPage => {
226262 app. state . sql_tab . next_page ( ) ;
227263 app. state . sql_tab . refresh_query_results_state ( ) ;
228- let history_query =
229- HistoryQuery :: new ( Context :: Local , query. to_string ( ) , duration, None , None ) ;
230- app. state . history_tab . add_to_history ( history_query) ;
231- app. state . history_tab . refresh_history_table_state ( ) ;
232264 }
233265 #[ cfg( feature = "flightsql" ) ]
234266 AppEvent :: FlightSQLExecutionResultsNextBatch ( r) => {
0 commit comments