make aws credentials editable and refactor to hold name and id
This commit is contained in:
1 parent
dbcf72fa38
commit
21efa29fdd
9 files changed
+289
-184
No files matched your search
+126
-92
@@ -12,16 +12,19 @@ use crate::components::{PassphraseInput, PassphraseInputEvent, colored_switch, e
|
||||
use crate::credentials::AwsCredential;
|
||||
use crate::session::AppSession;
|
||||
use super::Screen;
|
||||
use uuid::Uuid;
|
||||
|
||||
|
||||
pub struct CredentialsScreen {
|
||||
tab: usize,
|
||||
aws: Vec<(String, AwsCredential)>,
|
||||
aws: Vec<AwsCredential>,
|
||||
aws_form: Option<AwsForm>,
|
||||
error: Option<SharedString>,
|
||||
}
|
||||
|
||||
struct AwsForm {
|
||||
/// The id of the credential being edited, or None if this is a new one.
|
||||
editing: Option<Uuid>,
|
||||
name: Entity<InputState>,
|
||||
access_key_id: Entity<InputState>,
|
||||
secret_access_key: Entity<PassphraseInput>,
|
||||
@@ -43,18 +46,25 @@ impl CredentialsScreen {
|
||||
|
||||
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.aws = creds;
|
||||
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);
|
||||
fn open_aws_form(&mut self, editing: Option<Uuid>, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let existing = self.aws
|
||||
.iter()
|
||||
.find(|cred| Some(cred.id) == editing)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
|
||||
let name = cx.new(|cx| InputState::new(window, cx).default_value(existing.name));
|
||||
let access_key_id = cx.new(|cx| InputState::new(window, cx).default_value(existing.access_key_id));
|
||||
let secret_access_key = PassphraseInput::builder("aws-secret")
|
||||
.value(existing.secret_access_key)
|
||||
.build(window, cx);
|
||||
name.update(cx, |name, cx| name.focus(window, cx));
|
||||
|
||||
let mut _subscriptions = Vec::new();
|
||||
@@ -72,10 +82,11 @@ impl CredentialsScreen {
|
||||
}));
|
||||
|
||||
self.aws_form = Some(AwsForm {
|
||||
editing,
|
||||
name,
|
||||
access_key_id,
|
||||
secret_access_key,
|
||||
is_default: false,
|
||||
is_default: existing.is_default,
|
||||
_subscriptions,
|
||||
});
|
||||
self.error = None;
|
||||
@@ -84,21 +95,22 @@ impl CredentialsScreen {
|
||||
|
||||
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 editing = form.editing;
|
||||
let cred = AwsCredential {
|
||||
id: editing.unwrap_or_else(Uuid::new_v4),
|
||||
name: form.name.read(cx).value().trim().to_string(),
|
||||
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() {
|
||||
if cred.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());
|
||||
if self.aws.iter().any(|existing| existing.name == cred.name && existing.id != cred.id) {
|
||||
self.error = Some(format!("An AWS credential named \"{}\" already exists.", cred.name).into());
|
||||
cx.notify();
|
||||
return;
|
||||
}
|
||||
@@ -106,7 +118,7 @@ impl CredentialsScreen {
|
||||
let AppSession::Unlocked(db) = cx.global::<AppSession>() else {
|
||||
unreachable!("credentials screen is only shown while unlocked");
|
||||
};
|
||||
match db.save_aws_credential(&name, &cred) {
|
||||
match db.save_aws_credential(&cred) {
|
||||
Ok(()) => {
|
||||
self.aws_form = None;
|
||||
self.load_aws(cx);
|
||||
@@ -119,43 +131,67 @@ impl CredentialsScreen {
|
||||
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 mut panel = div().flex().flex_col().gap(rems(0.5));
|
||||
if self.aws.is_empty() {
|
||||
panel = panel.child(empty_state("AWS access keys", "You have no saved AWS credentials.", cx));
|
||||
}
|
||||
for cred in &self.aws {
|
||||
if let Some(form) = &self.aws_form
|
||||
&& form.editing == Some(cred.id)
|
||||
{
|
||||
panel = panel.child(self.render_aws_form(form, window, cx));
|
||||
continue;
|
||||
}
|
||||
|
||||
let Some(form) = &self.aws_form else {
|
||||
return div().flex().flex_col().gap(rems(1.)).child(list).child(
|
||||
let id = cred.id;
|
||||
panel = panel.child(
|
||||
div()
|
||||
.id(SharedString::from(&cred.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(cred.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()),
|
||||
)
|
||||
.child(
|
||||
icon_button(
|
||||
SharedString::from(format!("edit-{}", cred.name)),
|
||||
IconName::Pencil,
|
||||
"Edit",
|
||||
ButtonVariant::Outline,
|
||||
cx,
|
||||
)
|
||||
.on_click(cx.listener(move |this, _, window, cx| {
|
||||
this.open_aws_form(Some(id), window, cx)
|
||||
})),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
match &self.aws_form {
|
||||
None => panel.child(
|
||||
button("aws-add", "Add", ButtonVariant::Primary, cx)
|
||||
.self_center()
|
||||
.on_click(cx.listener(|this, _, window, cx| this.add_aws(window, cx))),
|
||||
);
|
||||
};
|
||||
.mt(rems(0.5))
|
||||
.on_click(cx.listener(|this, _, window, cx| this.open_aws_form(None, window, cx))),
|
||||
),
|
||||
Some(form) if form.editing.is_none() => panel.child(self.render_aws_form(form, window, cx)),
|
||||
Some(_) => panel,
|
||||
}
|
||||
}
|
||||
|
||||
fn render_aws_form(&self, form: &AwsForm, window: &mut Window, cx: &mut Context<Self>) -> Div {
|
||||
let t = cx.omarchy().clone();
|
||||
let field = |label: &'static str, input: gpui_kit::AnyElement| {
|
||||
div()
|
||||
.flex()
|
||||
@@ -166,51 +202,49 @@ impl CredentialsScreen {
|
||||
};
|
||||
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))),
|
||||
),
|
||||
),
|
||||
)
|
||||
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))),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user