1use edit_prediction::EditPredictionStore;
2use gpui::{
3 DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, IntoElement, ParentElement, Render,
4};
5use ui::{Button, ButtonStyle, Clickable, Headline, HeadlineSize, prelude::*};
6use ui_input::InputField;
7use workspace::ModalView;
8
9pub struct SweepApiKeyModal {
10 api_key_input: Entity<InputField>,
11 focus_handle: FocusHandle,
12}
13
14impl SweepApiKeyModal {
15 pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
16 let api_key_input = cx.new(|cx| InputField::new(window, cx, "Enter your Sweep API token"));
17
18 Self {
19 api_key_input,
20 focus_handle: cx.focus_handle(),
21 }
22 }
23
24 fn cancel(&mut self, _: &menu::Cancel, _window: &mut Window, cx: &mut Context<Self>) {
25 cx.emit(DismissEvent);
26 }
27
28 fn confirm(&mut self, _: &menu::Confirm, _window: &mut Window, cx: &mut Context<Self>) {
29 let api_key = self.api_key_input.read(cx).text(cx);
30 let api_key = (!api_key.trim().is_empty()).then_some(api_key);
31
32 if let Some(ep_store) = EditPredictionStore::try_global(cx) {
33 ep_store.update(cx, |ep_store, cx| {
34 ep_store
35 .sweep_ai
36 .set_api_token(api_key, cx)
37 .detach_and_log_err(cx);
38 });
39 }
40
41 cx.emit(DismissEvent);
42 }
43}
44
45impl EventEmitter<DismissEvent> for SweepApiKeyModal {}
46
47impl ModalView for SweepApiKeyModal {}
48
49impl Focusable for SweepApiKeyModal {
50 fn focus_handle(&self, _cx: &App) -> FocusHandle {
51 self.focus_handle.clone()
52 }
53}
54
55impl Render for SweepApiKeyModal {
56 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
57 v_flex()
58 .key_context("SweepApiKeyModal")
59 .on_action(cx.listener(Self::cancel))
60 .on_action(cx.listener(Self::confirm))
61 .elevation_2(cx)
62 .w(px(400.))
63 .p_4()
64 .gap_3()
65 .child(Headline::new("Sweep API Token").size(HeadlineSize::Small))
66 .child(self.api_key_input.clone())
67 .child(
68 h_flex()
69 .justify_end()
70 .gap_2()
71 .child(Button::new("cancel", "Cancel").on_click(cx.listener(
72 |_, _, _window, cx| {
73 cx.emit(DismissEvent);
74 },
75 )))
76 .child(
77 Button::new("save", "Save")
78 .style(ButtonStyle::Filled)
79 .on_click(cx.listener(|this, _, window, cx| {
80 this.confirm(&menu::Confirm, window, cx);
81 })),
82 ),
83 )
84 }
85}