From d661ef9ee9678d94f3c22a8e3b10ac30ad8c8736 Mon Sep 17 00:00:00 2001 From: Lucas Pickering Date: Fri, 6 Dec 2024 17:28:45 -0500 Subject: [PATCH] Only allow request cancellation when the selected request is pending --- CHANGELOG.md | 4 ++++ crates/tui/src/http.rs | 8 ++++++++ crates/tui/src/view/component/root.rs | 23 +++++++++++++---------- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01ea2f20..43ef4609 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - Improve UX of query text box - The query is now auto-applied when changed (with a 500ms debounce), and drops focus on the text box when Enter is pressed +### Fixed + +- Don't show request cancellation dialog if the selected request isn't building/loading + ## [2.3.0] - 2024-11-11 ### Added diff --git a/crates/tui/src/http.rs b/crates/tui/src/http.rs index b88a314c..85780f15 100644 --- a/crates/tui/src/http.rs +++ b/crates/tui/src/http.rs @@ -311,6 +311,14 @@ impl RequestStore { Ok(()) } + /// Is the given request either building or loading? + pub fn is_in_progress(&self, id: RequestId) -> bool { + matches!( + self.get(id), + Some(RequestState::Building { .. } | RequestState::Loading { .. },) + ) + } + /// Replace a request state in the store with new state, by mapping it /// through a function. This assumes the request state is supposed to be in /// the state, so it logs a message if it isn't (panics in debug mode). This diff --git a/crates/tui/src/view/component/root.rs b/crates/tui/src/view/component/root.rs index 0e1a893a..5ceaa14e 100644 --- a/crates/tui/src/view/component/root.rs +++ b/crates/tui/src/view/component/root.rs @@ -151,16 +151,19 @@ impl EventHandler for Root { } Action::Cancel => { if let Some(request_id) = self.selected_request_id.0 { - ViewContext::open_modal(ConfirmModal::new( - "Cancel request?".into(), - move |response| { - if response { - ViewContext::send_message( - Message::HttpCancel(request_id), - ); - } - }, - )) + // 2024 edition: if-let chain + if context.request_store.is_in_progress(request_id) { + ViewContext::open_modal(ConfirmModal::new( + "Cancel request?".into(), + move |response| { + if response { + ViewContext::send_message( + Message::HttpCancel(request_id), + ); + } + }, + )) + } } } Action::Quit => ViewContext::send_message(Message::Quit),