From cbbcc3502af5dc7960ca329a0befe09443c6c67d Mon Sep 17 00:00:00 2001 From: "Joseph T. Lyons" Date: Fri, 8 Dec 2023 17:21:14 -0500 Subject: [PATCH] Fix bug where feedback dismissal prompt is shown twice --- crates/feedback2/src/feedback_modal.rs | 46 +++++++++++--------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/crates/feedback2/src/feedback_modal.rs b/crates/feedback2/src/feedback_modal.rs index 54d0705eb3..1e219db7be 100644 --- a/crates/feedback2/src/feedback_modal.rs +++ b/crates/feedback2/src/feedback_modal.rs @@ -4,7 +4,7 @@ use anyhow::bail; use client::{Client, ZED_SECRET_CLIENT_TOKEN, ZED_SERVER_URL}; use db::kvp::KEY_VALUE_STORE; use editor::{Editor, EditorEvent}; -use futures::{AsyncReadExt, Future}; +use futures::AsyncReadExt; use gpui::{ div, rems, serde_json, AppContext, DismissEvent, Div, EventEmitter, FocusHandle, FocusableView, Model, PromptLevel, Render, Task, View, ViewContext, @@ -54,8 +54,15 @@ impl EventEmitter for FeedbackModal {} impl ModalView for FeedbackModal { fn dismiss(&mut self, cx: &mut ViewContext) -> Task { - let prompt = Self::prompt_dismiss(cx); - cx.spawn(|_, _| prompt) + let has_feedback = self.feedback_editor.read(cx).text_option(cx).is_some(); + + if !has_feedback { + return cx.spawn(|_, _| async { true }); + } + + let answer = cx.prompt(PromptLevel::Info, "Discard feedback?", &["Yes", "No"]); + + cx.spawn(|_, _| async { answer.await.ok() == Some(0) }) } } @@ -246,28 +253,7 @@ impl FeedbackModal { // TODO: Escape button calls dismiss fn cancel(&mut self, _: &menu::Cancel, cx: &mut ViewContext) { - self.dismiss_event(cx) - } - - fn dismiss_event(&mut self, cx: &mut ViewContext) { - let has_feedback = self.feedback_editor.read(cx).text_option(cx).is_some(); - let dismiss = Self::prompt_dismiss(cx); - - cx.spawn(|this, mut cx| async move { - if !has_feedback || (has_feedback && dismiss.await) { - this.update(&mut cx, |_, cx| cx.emit(DismissEvent)).ok(); - } - }) - .detach() - } - - fn prompt_dismiss(cx: &mut ViewContext) -> impl Future { - let answer = cx.prompt(PromptLevel::Info, "Discard feedback?", &["Yes", "No"]); - - async { - let answer = answer.await.ok(); - answer == Some(0) - } + cx.emit(DismissEvent) } } @@ -374,8 +360,14 @@ impl Render for FeedbackModal { Button::new("cancel_feedback", "Cancel") .style(ButtonStyle::Subtle) .color(Color::Muted) - .on_click(cx.listener(move |this, _, cx| { - this.dismiss_event(cx) + .on_click(cx.listener(move |_, _, cx| { + cx.spawn(|this, mut cx| async move { + this.update(&mut cx, |_, cx| { + cx.emit(DismissEvent) + }) + .ok(); + }) + .detach(); })), ) .child(