426 lines
15 KiB
Rust
426 lines
15 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 super::Screen;
|
|
use crate::components::{PassphraseInput, PassphraseInputEvent, colored_switch, error_alert};
|
|
use crate::credentials::AwsCredential;
|
|
use crate::session::AppSession;
|
|
use uuid::Uuid;
|
|
|
|
pub struct CredentialsScreen {
|
|
tab: usize,
|
|
aws: Vec<AwsCredential>,
|
|
aws_form: Option<AwsForm>,
|
|
/// The id of the credential awaiting delete confirmation.
|
|
aws_deleting: Option<Uuid>,
|
|
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>,
|
|
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,
|
|
aws_deleting: 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;
|
|
self.error = None;
|
|
}
|
|
Err(e) => self.error = Some(format!("Failed to load credentials: {e}").into()),
|
|
}
|
|
}
|
|
|
|
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();
|
|
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 {
|
|
editing,
|
|
name,
|
|
access_key_id,
|
|
secret_access_key,
|
|
is_default: existing.is_default,
|
|
_subscriptions,
|
|
});
|
|
self.error = None;
|
|
cx.notify();
|
|
}
|
|
|
|
fn save_aws(&mut self, cx: &mut Context<Self>) {
|
|
let Some(form) = &self.aws_form else { return };
|
|
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 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;
|
|
}
|
|
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;
|
|
}
|
|
|
|
let AppSession::Unlocked(db) = cx.global::<AppSession>() else {
|
|
unreachable!("credentials screen is only shown while unlocked");
|
|
};
|
|
match db.save_aws_credential(&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 delete_aws(&mut self, id: Uuid, cx: &mut Context<Self>) {
|
|
let AppSession::Unlocked(db) = cx.global::<AppSession>() else {
|
|
unreachable!("credentials screen is only shown while unlocked");
|
|
};
|
|
match db.delete_aws_credential(id) {
|
|
Ok(()) => {
|
|
self.aws_deleting = None;
|
|
self.load_aws(cx);
|
|
}
|
|
Err(e) => self.error = Some(format!("Failed to delete credential: {e}").into()),
|
|
}
|
|
cx.notify();
|
|
}
|
|
|
|
fn render_aws_tab(&self, window: &mut Window, cx: &mut Context<Self>) -> Div {
|
|
let t = cx.omarchy().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 id = cred.id;
|
|
if self.aws_deleting == Some(id) {
|
|
panel = panel.child(
|
|
div()
|
|
.flex()
|
|
.items_center()
|
|
.gap(rems(0.5))
|
|
.p(rems(0.625))
|
|
.border_1()
|
|
.border_color(t.danger)
|
|
.child(format!("Delete AWS credential \"{}\"?", cred.name))
|
|
.child(
|
|
button("aws-delete-cancel", "Cancel", ButtonVariant::Outline, cx)
|
|
.ml_auto()
|
|
.on_click(cx.listener(|this, _, _, cx| {
|
|
this.aws_deleting = None;
|
|
cx.notify();
|
|
})),
|
|
)
|
|
.child(
|
|
button("aws-delete-confirm", "Delete", ButtonVariant::Danger, cx)
|
|
.on_click(
|
|
cx.listener(move |this, _, _, cx| this.delete_aws(id, cx)),
|
|
),
|
|
),
|
|
);
|
|
continue;
|
|
}
|
|
|
|
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),
|
|
)),
|
|
)
|
|
.child(
|
|
icon_button(
|
|
SharedString::from(format!("delete-{}", cred.name)),
|
|
IconName::Trash,
|
|
"Delete",
|
|
ButtonVariant::Danger,
|
|
cx,
|
|
)
|
|
.on_click(cx.listener(move |this, _, _, cx| {
|
|
this.aws_deleting = Some(id);
|
|
cx.notify();
|
|
})),
|
|
),
|
|
);
|
|
}
|
|
|
|
match &self.aws_form {
|
|
None => panel.child(
|
|
button("aws-add", "Add", ButtonVariant::Primary, cx)
|
|
.self_center()
|
|
.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()
|
|
.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.))
|
|
.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),
|
|
)
|
|
}
|
|
}
|