282 lines
10 KiB
Rust
282 lines
10 KiB
Rust
use gpui_kit::base::input::{InputEvent, InputState};
|
|
use gpui_kit::{
|
|
AppContext, Context, Div, Entity, FontWeight, InteractiveElement, IntoElement, ParentElement,
|
|
Render, SharedString, Styled, Subscription, TestSupportExt, Window, div, rems,
|
|
};
|
|
use gpui_omarchy::{
|
|
ActiveTheme, ButtonVariant, ChoiceItem, IconName, Status, badge, button, empty_state,
|
|
focus_scope, icon_button, input, tab_list,
|
|
};
|
|
|
|
use crate::components::{PassphraseInput, PassphraseInputEvent, colored_switch, error_alert};
|
|
use crate::credentials::AwsCredential;
|
|
use crate::session::AppSession;
|
|
use super::Screen;
|
|
|
|
|
|
pub struct CredentialsScreen {
|
|
tab: usize,
|
|
aws: Vec<(String, AwsCredential)>,
|
|
aws_form: Option<AwsForm>,
|
|
error: Option<SharedString>,
|
|
}
|
|
|
|
struct AwsForm {
|
|
name: Entity<InputState>,
|
|
access_key_id: Entity<InputState>,
|
|
secret_access_key: Entity<PassphraseInput>,
|
|
is_default: bool,
|
|
_subscriptions: Vec<Subscription>,
|
|
}
|
|
|
|
impl CredentialsScreen {
|
|
pub fn new(cx: &mut Context<Self>) -> Self {
|
|
let mut this = Self { tab: 0, aws: Vec::new(), aws_form: None, error: None };
|
|
this.load_aws(cx);
|
|
this
|
|
}
|
|
|
|
fn load_aws(&mut self, cx: &mut Context<Self>) {
|
|
let AppSession::Unlocked(db) = cx.global::<AppSession>() else {
|
|
unreachable!("credentials screen is only shown while unlocked");
|
|
};
|
|
|
|
match db.list_aws_credentials() {
|
|
Ok(creds) => {
|
|
self.aws = creds.into_iter().collect();
|
|
self.aws.sort_by(|a, b| a.0.cmp(&b.0));
|
|
self.error = None;
|
|
},
|
|
Err(e) => self.error = Some(format!("Failed to load credentials: {e}").into()),
|
|
}
|
|
}
|
|
|
|
fn add_aws(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
|
let name = cx.new(|cx| InputState::new(window, cx));
|
|
let access_key_id = cx.new(|cx| InputState::new(window, cx));
|
|
let secret_access_key = PassphraseInput::builder("aws-secret").build(window, cx);
|
|
name.update(cx, |name, cx| name.focus(window, cx));
|
|
|
|
let mut _subscriptions = Vec::new();
|
|
for input in [&name, &access_key_id] {
|
|
_subscriptions.push(cx.subscribe(input, |this, _, event: &InputEvent, cx| {
|
|
if let InputEvent::PressEnter { .. } = event {
|
|
this.save_aws(cx);
|
|
}
|
|
}));
|
|
}
|
|
_subscriptions.push(cx.subscribe(&secret_access_key, |this, _, event, cx| {
|
|
if let PassphraseInputEvent::Submit = event {
|
|
this.save_aws(cx);
|
|
}
|
|
}));
|
|
|
|
self.aws_form = Some(AwsForm {
|
|
name,
|
|
access_key_id,
|
|
secret_access_key,
|
|
is_default: false,
|
|
_subscriptions,
|
|
});
|
|
self.error = None;
|
|
cx.notify();
|
|
}
|
|
|
|
fn save_aws(&mut self, cx: &mut Context<Self>) {
|
|
let Some(form) = &self.aws_form else { return };
|
|
let name = form.name.read(cx).value().trim().to_string();
|
|
let cred = AwsCredential {
|
|
access_key_id: form.access_key_id.read(cx).value().trim().to_string(),
|
|
secret_access_key: form.secret_access_key.read(cx).value(cx).trim().to_string(),
|
|
is_default: form.is_default,
|
|
};
|
|
|
|
if name.is_empty() || cred.access_key_id.is_empty() || cred.secret_access_key.is_empty() {
|
|
self.error = Some("All fields are required.".into());
|
|
cx.notify();
|
|
return;
|
|
}
|
|
// saving is an upsert, so without this a new credential would silently replace the old one
|
|
if self.aws.iter().any(|(existing, _)| *existing == name) {
|
|
self.error = Some(format!("An AWS credential named \"{name}\" already exists.").into());
|
|
cx.notify();
|
|
return;
|
|
}
|
|
|
|
let AppSession::Unlocked(db) = cx.global::<AppSession>() else {
|
|
unreachable!("credentials screen is only shown while unlocked");
|
|
};
|
|
match db.save_aws_credential(&name, &cred) {
|
|
Ok(()) => {
|
|
self.aws_form = None;
|
|
self.load_aws(cx);
|
|
},
|
|
Err(e) => self.error = Some(format!("Failed to save credential: {e}").into()),
|
|
}
|
|
cx.notify();
|
|
}
|
|
|
|
fn render_aws_tab(&self, window: &mut Window, cx: &mut Context<Self>) -> Div {
|
|
let t = cx.omarchy().clone();
|
|
|
|
let list = if self.aws.is_empty() {
|
|
empty_state("AWS access keys", "You have no saved AWS credentials.", cx)
|
|
} else {
|
|
div()
|
|
.flex()
|
|
.flex_col()
|
|
.gap(rems(0.5))
|
|
.children(self.aws.iter().map(|(name, cred)| {
|
|
div()
|
|
.id(SharedString::from(name))
|
|
.test_support()
|
|
.flex()
|
|
.items_center()
|
|
.gap(rems(0.75))
|
|
.p(rems(0.625))
|
|
.border_1()
|
|
.border_color(if cred.is_default { t.accent } else { t.border })
|
|
.child(div().font_weight(FontWeight::BOLD).child(name.clone()))
|
|
.children(cred.is_default.then(|| badge("Default", Status::Success, cx)))
|
|
.child(
|
|
div()
|
|
.ml_auto()
|
|
.font_family(t.mono_font.clone())
|
|
.text_color(t.secondary)
|
|
.child(cred.access_key_id.clone()),
|
|
)
|
|
}))
|
|
};
|
|
|
|
let Some(form) = &self.aws_form else {
|
|
return div().flex().flex_col().gap(rems(1.)).child(list).child(
|
|
button("aws-add", "Add", ButtonVariant::Primary, cx)
|
|
.self_center()
|
|
.on_click(cx.listener(|this, _, window, cx| this.add_aws(window, cx))),
|
|
);
|
|
};
|
|
|
|
let field = |label: &'static str, input: gpui_kit::AnyElement| {
|
|
div()
|
|
.flex()
|
|
.flex_col()
|
|
.gap(rems(0.25))
|
|
.child(div().text_size(rems(0.875)).child(label))
|
|
.child(input)
|
|
};
|
|
let this = cx.entity();
|
|
|
|
div().flex().flex_col().gap(rems(1.)).child(list).child(
|
|
div()
|
|
.flex()
|
|
.flex_col()
|
|
.gap(rems(1.))
|
|
.p(rems(1.))
|
|
.border_1()
|
|
.border_color(t.border)
|
|
.child(field("Name", input("aws-name", &form.name, window, cx).into_any_element()))
|
|
.child(field(
|
|
"Key ID",
|
|
input("aws-key-id", &form.access_key_id, window, cx)
|
|
.font_family(t.mono_font.clone())
|
|
.into_any_element(),
|
|
))
|
|
.child(field("Secret key", form.secret_access_key.clone().into_any_element()))
|
|
.child(
|
|
div()
|
|
.flex()
|
|
.items_center()
|
|
.gap(rems(0.5))
|
|
.child(
|
|
colored_switch("aws-default", "Default AWS access key", form.is_default, t.success, cx)
|
|
.on_change(move |value, _, _, cx| this.update(cx, |this, cx| {
|
|
if let Some(form) = &mut this.aws_form {
|
|
form.is_default = value;
|
|
}
|
|
cx.notify();
|
|
})),
|
|
)
|
|
.child(
|
|
button("aws-cancel", "Cancel", ButtonVariant::Outline, cx)
|
|
.ml_auto()
|
|
.on_click(cx.listener(|this, _, _, cx| {
|
|
this.aws_form = None;
|
|
this.error = None;
|
|
cx.notify();
|
|
})),
|
|
)
|
|
.child(
|
|
button("aws-save", "Save", ButtonVariant::Primary, cx)
|
|
.on_click(cx.listener(|this, _, _, cx| this.save_aws(cx))),
|
|
),
|
|
),
|
|
)
|
|
}
|
|
}
|
|
|
|
impl Render for CredentialsScreen {
|
|
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
let this = cx.entity();
|
|
let tabs = tab_list(
|
|
"credential-tabs",
|
|
vec![
|
|
ChoiceItem::new("aws", "AWS"),
|
|
ChoiceItem::new("ssh", "SSH"),
|
|
ChoiceItem::new("docker", "Docker"),
|
|
],
|
|
Some(self.tab),
|
|
false,
|
|
move |index, _, cx| this.update(cx, |this, cx| {
|
|
this.tab = index;
|
|
cx.notify();
|
|
}),
|
|
window,
|
|
cx,
|
|
);
|
|
|
|
let panel = match self.tab {
|
|
0 => self.render_aws_tab(window, cx),
|
|
1 => empty_state("SSH keys", "Not implemented yet.", cx),
|
|
_ => empty_state("Docker credentials", "Not implemented yet.", cx),
|
|
};
|
|
|
|
focus_scope("credentials-screen")
|
|
.size_full()
|
|
.flex()
|
|
.flex_col()
|
|
.items_center()
|
|
.gap(rems(1.))
|
|
.p(rems(0.5))
|
|
.child(
|
|
div()
|
|
.relative()
|
|
.w_full()
|
|
.flex()
|
|
.justify_center()
|
|
.child(
|
|
icon_button("back", IconName::ChevronLeft, "Back", ButtonVariant::Secondary, cx)
|
|
.absolute()
|
|
.left_0()
|
|
.on_click(|_, _, cx| cx.set_global(Screen::Home)),
|
|
)
|
|
.child(
|
|
div()
|
|
.text_size(rems(1.875))
|
|
.font_weight(FontWeight::BOLD)
|
|
.child("Credentials"),
|
|
),
|
|
)
|
|
.child(tabs)
|
|
.child(
|
|
div()
|
|
.w_full()
|
|
.max_w(rems(36.))
|
|
.flex()
|
|
.flex_col()
|
|
.gap(rems(1.))
|
|
.children(self.error.clone().map(|e| error_alert(e, cx)))
|
|
.child(panel),
|
|
)
|
|
}
|
|
}
|