1// use crate::system_specs::SystemSpecs;
2// use anyhow::bail;
3// use client::{Client, ZED_SECRET_CLIENT_TOKEN, ZED_SERVER_URL};
4// use editor::{Anchor, Editor};
5// use futures::AsyncReadExt;
6// use gpui::{actions, serde_json, AppContext, Model, PromptLevel, Task, View, ViewContext};
7// use isahc::Request;
8// use language::Buffer;
9// use postage::prelude::Stream;
10// use project::{search::SearchQuery, Project};
11// use regex::Regex;
12// use serde::Serialize;
13// use std::{
14// ops::{Range, RangeInclusive},
15// sync::Arc,
16// };
17// use util::ResultExt;
18// use workspace::{searchable::SearchableItem, Workspace};
19
20// const FEEDBACK_CHAR_LIMIT: RangeInclusive<usize> = 10..=5000;
21// const FEEDBACK_SUBMISSION_ERROR_TEXT: &str =
22// "Feedback failed to submit, see error log for details.";
23
24use gpui::actions;
25
26actions!(GiveFeedback, SubmitFeedback);
27
28// pub fn init(cx: &mut AppContext) {
29// cx.add_action({
30// move |workspace: &mut Workspace, _: &GiveFeedback, cx: &mut ViewContext<Workspace>| {
31// FeedbackEditor::deploy(workspace, cx);
32// }
33// });
34// }
35
36// #[derive(Serialize)]
37// struct FeedbackRequestBody<'a> {
38// feedback_text: &'a str,
39// email: Option<String>,
40// metrics_id: Option<Arc<str>>,
41// installation_id: Option<Arc<str>>,
42// system_specs: SystemSpecs,
43// is_staff: bool,
44// token: &'a str,
45// }
46
47// #[derive(Clone)]
48// pub(crate) struct FeedbackEditor {
49// system_specs: SystemSpecs,
50// editor: View<Editor>,
51// project: Model<Project>,
52// pub allow_submission: bool,
53// }
54
55// impl FeedbackEditor {
56// fn new(
57// system_specs: SystemSpecs,
58// project: Model<Project>,
59// buffer: Model<Buffer>,
60// cx: &mut ViewContext<Self>,
61// ) -> Self {
62// let editor = cx.add_view(|cx| {
63// let mut editor = Editor::for_buffer(buffer, Some(project.clone()), cx);
64// editor.set_vertical_scroll_margin(5, cx);
65// editor
66// });
67
68// cx.subscribe(&editor, |_, _, e, cx| cx.emit(e.clone()))
69// .detach();
70
71// Self {
72// system_specs: system_specs.clone(),
73// editor,
74// project,
75// allow_submission: true,
76// }
77// }
78
79// pub fn submit(&mut self, cx: &mut ViewContext<Self>) -> Task<anyhow::Result<()>> {
80// if !self.allow_submission {
81// return Task::ready(Ok(()));
82// }
83
84// let feedback_text = self.editor.read(cx).text(cx);
85// let feedback_char_count = feedback_text.chars().count();
86// let feedback_text = feedback_text.trim().to_string();
87
88// let error = if feedback_char_count < *FEEDBACK_CHAR_LIMIT.start() {
89// Some(format!(
90// "Feedback can't be shorter than {} characters.",
91// FEEDBACK_CHAR_LIMIT.start()
92// ))
93// } else if feedback_char_count > *FEEDBACK_CHAR_LIMIT.end() {
94// Some(format!(
95// "Feedback can't be longer than {} characters.",
96// FEEDBACK_CHAR_LIMIT.end()
97// ))
98// } else {
99// None
100// };
101
102// if let Some(error) = error {
103// cx.prompt(PromptLevel::Critical, &error, &["OK"]);
104// return Task::ready(Ok(()));
105// }
106
107// let mut answer = cx.prompt(
108// PromptLevel::Info,
109// "Ready to submit your feedback?",
110// &["Yes, Submit!", "No"],
111// );
112
113// let client = cx.global::<Arc<Client>>().clone();
114// let specs = self.system_specs.clone();
115
116// cx.spawn(|this, mut cx| async move {
117// let answer = answer.recv().await;
118
119// if answer == Some(0) {
120// this.update(&mut cx, |feedback_editor, cx| {
121// feedback_editor.set_allow_submission(false, cx);
122// })
123// .log_err();
124
125// match FeedbackEditor::submit_feedback(&feedback_text, client, specs).await {
126// Ok(_) => {
127// this.update(&mut cx, |_, cx| cx.emit(editor::EditorEvent::Closed))
128// .log_err();
129// }
130
131// Err(error) => {
132// log::error!("{}", error);
133// this.update(&mut cx, |feedback_editor, cx| {
134// cx.prompt(
135// PromptLevel::Critical,
136// FEEDBACK_SUBMISSION_ERROR_TEXT,
137// &["OK"],
138// );
139// feedback_editor.set_allow_submission(true, cx);
140// })
141// .log_err();
142// }
143// }
144// }
145// })
146// .detach();
147
148// Task::ready(Ok(()))
149// }
150
151// fn set_allow_submission(&mut self, allow_submission: bool, cx: &mut ViewContext<Self>) {
152// self.allow_submission = allow_submission;
153// cx.notify();
154// }
155
156// async fn submit_feedback(
157// feedback_text: &str,
158// zed_client: Arc<Client>,
159// system_specs: SystemSpecs,
160// ) -> anyhow::Result<()> {
161// let feedback_endpoint = format!("{}/api/feedback", *ZED_SERVER_URL);
162
163// let telemetry = zed_client.telemetry();
164// let metrics_id = telemetry.metrics_id();
165// let installation_id = telemetry.installation_id();
166// let is_staff = telemetry.is_staff();
167// let http_client = zed_client.http_client();
168
169// let re = Regex::new(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b").unwrap();
170
171// let emails: Vec<&str> = re
172// .captures_iter(feedback_text)
173// .map(|capture| capture.get(0).unwrap().as_str())
174// .collect();
175
176// let email = emails.first().map(|e| e.to_string());
177
178// let request = FeedbackRequestBody {
179// feedback_text: &feedback_text,
180// email,
181// metrics_id,
182// installation_id,
183// system_specs,
184// is_staff: is_staff.unwrap_or(false),
185// token: ZED_SECRET_CLIENT_TOKEN,
186// };
187
188// let json_bytes = serde_json::to_vec(&request)?;
189
190// let request = Request::post(feedback_endpoint)
191// .header("content-type", "application/json")
192// .body(json_bytes.into())?;
193
194// let mut response = http_client.send(request).await?;
195// let mut body = String::new();
196// response.body_mut().read_to_string(&mut body).await?;
197
198// let response_status = response.status();
199
200// if !response_status.is_success() {
201// bail!("Feedback API failed with error: {}", response_status)
202// }
203
204// Ok(())
205// }
206// }
207
208// impl FeedbackEditor {
209// pub fn deploy(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
210// let markdown = workspace
211// .app_state()
212// .languages
213// .language_for_name("Markdown");
214// cx.spawn(|workspace, mut cx| async move {
215// let markdown = markdown.await.log_err();
216// workspace
217// .update(&mut cx, |workspace, cx| {
218// workspace.with_local_workspace(cx, |workspace, cx| {
219// let project = workspace.project().clone();
220// let buffer = project
221// .update(cx, |project, cx| project.create_buffer("", markdown, cx))
222// .expect("creating buffers on a local workspace always succeeds");
223// let system_specs = SystemSpecs::new(cx);
224// let feedback_editor = cx
225// .add_view(|cx| FeedbackEditor::new(system_specs, project, buffer, cx));
226// workspace.add_item(Box::new(feedback_editor), cx);
227// })
228// })?
229// .await
230// })
231// .detach_and_log_err(cx);
232// }
233// }
234
235// impl View for FeedbackEditor {
236// fn ui_name() -> &'static str {
237// "FeedbackEditor"
238// }
239
240// fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
241// ChildView::new(&self.editor, cx).into_any()
242// }
243
244// fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
245// if cx.is_self_focused() {
246// cx.focus(&self.editor);
247// }
248// }
249// }
250
251// impl Entity for FeedbackEditor {
252// type Event = editor::Event;
253// }
254
255// impl Item for FeedbackEditor {
256// fn tab_tooltip_text(&self, _: &AppContext) -> Option<Cow<str>> {
257// Some("Send Feedback".into())
258// }
259
260// fn tab_content<T: 'static>(
261// &self,
262// _: Option<usize>,
263// style: &theme::Tab,
264// _: &AppContext,
265// ) -> AnyElement<T> {
266// Flex::row()
267// .with_child(
268// Svg::new("icons/feedback.svg")
269// .with_color(style.label.text.color)
270// .constrained()
271// .with_width(style.type_icon_width)
272// .aligned()
273// .contained()
274// .with_margin_right(style.spacing),
275// )
276// .with_child(
277// Label::new("Send Feedback", style.label.clone())
278// .aligned()
279// .contained(),
280// )
281// .into_any()
282// }
283
284// fn for_each_project_item(&self, cx: &AppContext, f: &mut dyn FnMut(usize, &dyn project::Item)) {
285// self.editor.for_each_project_item(cx, f)
286// }
287
288// fn is_singleton(&self, _: &AppContext) -> bool {
289// true
290// }
291
292// fn can_save(&self, _: &AppContext) -> bool {
293// true
294// }
295
296// fn save(
297// &mut self,
298// _: ModelHandle<Project>,
299// cx: &mut ViewContext<Self>,
300// ) -> Task<anyhow::Result<()>> {
301// self.submit(cx)
302// }
303
304// fn save_as(
305// &mut self,
306// _: ModelHandle<Project>,
307// _: std::path::PathBuf,
308// cx: &mut ViewContext<Self>,
309// ) -> Task<anyhow::Result<()>> {
310// self.submit(cx)
311// }
312
313// fn reload(
314// &mut self,
315// _: ModelHandle<Project>,
316// _: &mut ViewContext<Self>,
317// ) -> Task<anyhow::Result<()>> {
318// Task::Ready(Some(Ok(())))
319// }
320
321// fn clone_on_split(
322// &self,
323// _workspace_id: workspace::WorkspaceId,
324// cx: &mut ViewContext<Self>,
325// ) -> Option<Self>
326// where
327// Self: Sized,
328// {
329// let buffer = self
330// .editor
331// .read(cx)
332// .buffer()
333// .read(cx)
334// .as_singleton()
335// .expect("Feedback buffer is only ever singleton");
336
337// Some(Self::new(
338// self.system_specs.clone(),
339// self.project.clone(),
340// buffer.clone(),
341// cx,
342// ))
343// }
344
345// fn as_searchable(&self, handle: &ViewHandle<Self>) -> Option<Box<dyn SearchableItemHandle>> {
346// Some(Box::new(handle.clone()))
347// }
348
349// fn act_as_type<'a>(
350// &'a self,
351// type_id: TypeId,
352// self_handle: &'a ViewHandle<Self>,
353// _: &'a AppContext,
354// ) -> Option<&'a AnyViewHandle> {
355// if type_id == TypeId::of::<Self>() {
356// Some(self_handle)
357// } else if type_id == TypeId::of::<Editor>() {
358// Some(&self.editor)
359// } else {
360// None
361// }
362// }
363
364// fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
365// Editor::to_item_events(event)
366// }
367// }
368
369// impl SearchableItem for FeedbackEditor {
370// type Match = Range<Anchor>;
371
372// fn to_search_event(
373// &mut self,
374// event: &Self::Event,
375// cx: &mut ViewContext<Self>,
376// ) -> Option<workspace::searchable::SearchEvent> {
377// self.editor
378// .update(cx, |editor, cx| editor.to_search_event(event, cx))
379// }
380
381// fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
382// self.editor
383// .update(cx, |editor, cx| editor.clear_matches(cx))
384// }
385
386// fn update_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
387// self.editor
388// .update(cx, |editor, cx| editor.update_matches(matches, cx))
389// }
390
391// fn query_suggestion(&mut self, cx: &mut ViewContext<Self>) -> String {
392// self.editor
393// .update(cx, |editor, cx| editor.query_suggestion(cx))
394// }
395
396// fn activate_match(
397// &mut self,
398// index: usize,
399// matches: Vec<Self::Match>,
400// cx: &mut ViewContext<Self>,
401// ) {
402// self.editor
403// .update(cx, |editor, cx| editor.activate_match(index, matches, cx))
404// }
405
406// fn select_matches(&mut self, matches: Vec<Self::Match>, cx: &mut ViewContext<Self>) {
407// self.editor
408// .update(cx, |e, cx| e.select_matches(matches, cx))
409// }
410// fn replace(&mut self, matches: &Self::Match, query: &SearchQuery, cx: &mut ViewContext<Self>) {
411// self.editor
412// .update(cx, |e, cx| e.replace(matches, query, cx));
413// }
414// fn find_matches(
415// &mut self,
416// query: Arc<project::search::SearchQuery>,
417// cx: &mut ViewContext<Self>,
418// ) -> Task<Vec<Self::Match>> {
419// self.editor
420// .update(cx, |editor, cx| editor.find_matches(query, cx))
421// }
422
423// fn active_match_index(
424// &mut self,
425// matches: Vec<Self::Match>,
426// cx: &mut ViewContext<Self>,
427// ) -> Option<usize> {
428// self.editor
429// .update(cx, |editor, cx| editor.active_match_index(matches, cx))
430// }
431// }