sweep_api_token_modal.rs

 1use gpui::{
 2    DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, IntoElement, ParentElement, Render,
 3};
 4use ui::{Button, ButtonStyle, Clickable, Headline, HeadlineSize, prelude::*};
 5use ui_input::InputField;
 6use workspace::ModalView;
 7use zeta::Zeta;
 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(zeta) = Zeta::try_global(cx) {
33            zeta.update(cx, |zeta, cx| {
34                zeta.sweep_ai
35                    .set_api_token(api_key, cx)
36                    .detach_and_log_err(cx);
37            });
38        }
39
40        cx.emit(DismissEvent);
41    }
42}
43
44impl EventEmitter<DismissEvent> for SweepApiKeyModal {}
45
46impl ModalView for SweepApiKeyModal {}
47
48impl Focusable for SweepApiKeyModal {
49    fn focus_handle(&self, _cx: &App) -> FocusHandle {
50        self.focus_handle.clone()
51    }
52}
53
54impl Render for SweepApiKeyModal {
55    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
56        v_flex()
57            .key_context("SweepApiKeyModal")
58            .on_action(cx.listener(Self::cancel))
59            .on_action(cx.listener(Self::confirm))
60            .elevation_2(cx)
61            .w(px(400.))
62            .p_4()
63            .gap_3()
64            .child(Headline::new("Sweep API Token").size(HeadlineSize::Small))
65            .child(self.api_key_input.clone())
66            .child(
67                h_flex()
68                    .justify_end()
69                    .gap_2()
70                    .child(Button::new("cancel", "Cancel").on_click(cx.listener(
71                        |_, _, _window, cx| {
72                            cx.emit(DismissEvent);
73                        },
74                    )))
75                    .child(
76                        Button::new("save", "Save")
77                            .style(ButtonStyle::Filled)
78                            .on_click(cx.listener(|this, _, window, cx| {
79                                this.confirm(&menu::Confirm, window, cx);
80                            })),
81                    ),
82            )
83    }
84}