add credentials screen
This commit is contained in:
1 parent
b1c8762981
commit
31edd55b0a
5 files changed
+204
-3
No files matched your search
+1
-1
@@ -5,7 +5,7 @@ use gpui_kit::{AssetSource, Result, SharedString};
|
|||||||
|
|
||||||
|
|
||||||
// gpui-kit only embeds the icons its own components use, so anything else has to be listed here
|
// gpui-kit only embeds the icons its own components use, so anything else has to be listed here
|
||||||
icon_assets!(CreddyIcons, [LogOut, ShieldCheck]);
|
icon_assets!(CreddyIcons, [Key, LogOut, ShieldCheck]);
|
||||||
|
|
||||||
/// Our extra icons, falling back to gpui-kit's defaults.
|
/// Our extra icons, falling back to gpui-kit's defaults.
|
||||||
pub struct CreddyAssets;
|
pub struct CreddyAssets;
|
||||||
|
|||||||
+53
-2
@@ -18,7 +18,7 @@ mod test_data;
|
|||||||
|
|
||||||
use config::AppConfig;
|
use config::AppConfig;
|
||||||
use session::AppSession;
|
use session::AppSession;
|
||||||
use screens::{HomeScreen, UnlockScreen, WelcomeScreen};
|
use screens::{CredentialsScreen, HomeScreen, Screen, UnlockScreen, WelcomeScreen};
|
||||||
|
|
||||||
struct CreddyApp {}
|
struct CreddyApp {}
|
||||||
|
|
||||||
@@ -34,9 +34,14 @@ impl Render for CreddyApp {
|
|||||||
AppSession::Locked => window
|
AppSession::Locked => window
|
||||||
.use_keyed_state("unlock", cx, |window, cx| UnlockScreen::new(window, cx))
|
.use_keyed_state("unlock", cx, |window, cx| UnlockScreen::new(window, cx))
|
||||||
.into_any_element(),
|
.into_any_element(),
|
||||||
AppSession::Unlocked { .. } => window
|
AppSession::Unlocked { .. } => match cx.global::<Screen>() {
|
||||||
|
Screen::Home => window
|
||||||
.use_keyed_state("home", cx, |_, _| HomeScreen {})
|
.use_keyed_state("home", cx, |_, _| HomeScreen {})
|
||||||
.into_any_element(),
|
.into_any_element(),
|
||||||
|
Screen::Credentials => window
|
||||||
|
.use_keyed_state("credentials", cx, |_, cx| CredentialsScreen::new(cx))
|
||||||
|
.into_any_element(),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let theme = cx.omarchy();
|
let theme = cx.omarchy();
|
||||||
@@ -54,10 +59,12 @@ fn launch(cx: &mut App, config: AppConfig, session: AppSession) -> WindowHandle<
|
|||||||
gpui_omarchy::init(cx);
|
gpui_omarchy::init(cx);
|
||||||
cx.set_global(config);
|
cx.set_global(config);
|
||||||
cx.set_global(session);
|
cx.set_global(session);
|
||||||
|
cx.set_global(Screen::Home);
|
||||||
// refresh_windows() forces everything to re-render, which is kind of a nuclear solution,
|
// refresh_windows() forces everything to re-render, which is kind of a nuclear solution,
|
||||||
// but these values aren't expected to change very frequently, so it shouldn't be a problem
|
// but these values aren't expected to change very frequently, so it shouldn't be a problem
|
||||||
cx.observe_global::<AppConfig>(|cx| cx.refresh_windows()).detach();
|
cx.observe_global::<AppConfig>(|cx| cx.refresh_windows()).detach();
|
||||||
cx.observe_global::<AppSession>(|cx| cx.refresh_windows()).detach();
|
cx.observe_global::<AppSession>(|cx| cx.refresh_windows()).detach();
|
||||||
|
cx.observe_global::<Screen>(|cx| cx.refresh_windows()).detach();
|
||||||
|
|
||||||
cx.open_window(WindowOptions::default(), |_, cx| cx.new(|_| CreddyApp {}))
|
cx.open_window(WindowOptions::default(), |_, cx| cx.new(|_| CreddyApp {}))
|
||||||
.expect("open window")
|
.expect("open window")
|
||||||
@@ -83,6 +90,7 @@ mod tests {
|
|||||||
use gpui_kit::{AnyWindowHandle, TestAppContext};
|
use gpui_kit::{AnyWindowHandle, TestAppContext};
|
||||||
use gpui_kit::test::TestWindowExt;
|
use gpui_kit::test::TestWindowExt;
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::credentials::AwsCredential;
|
||||||
use crate::test_data::{TEST_PASSPHRASE, TestEnv};
|
use crate::test_data::{TEST_PASSPHRASE, TestEnv};
|
||||||
|
|
||||||
/// Start the app against the test environment, as `main` would.
|
/// Start the app against the test environment, as `main` would.
|
||||||
@@ -225,4 +233,47 @@ mod tests {
|
|||||||
assert_eq!(error_message(window, cx).as_deref(), Some("Passphrase is empty."));
|
assert_eq!(error_message(window, cx).as_deref(), Some("Passphrase is empty."));
|
||||||
assert!(!is_unlocked(cx));
|
assert!(!is_unlocked(cx));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_visible(window: AnyWindowHandle, id: &'static str, cx: &mut TestAppContext) -> bool {
|
||||||
|
cx.update_window(window, |_, window, _| window.try_find(id).is_some()).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[gpui_kit::test]
|
||||||
|
fn test_credentials_navigation(cx: &mut TestAppContext) {
|
||||||
|
let env = TestEnv::new().unwrap();
|
||||||
|
env.db().unwrap();
|
||||||
|
let window = open_app(&env, cx);
|
||||||
|
fill(window, "passphrase", TEST_PASSPHRASE, cx);
|
||||||
|
click(window, "submit", cx);
|
||||||
|
|
||||||
|
click(window, "credentials", cx);
|
||||||
|
assert_eq!(cx.read(|cx| *cx.global::<Screen>()), Screen::Credentials);
|
||||||
|
assert!(is_visible(window, "back", cx));
|
||||||
|
|
||||||
|
click(window, "back", cx);
|
||||||
|
assert_eq!(cx.read(|cx| *cx.global::<Screen>()), Screen::Home);
|
||||||
|
assert!(is_visible(window, "credentials", cx));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[gpui_kit::test]
|
||||||
|
fn test_credentials_tabs(cx: &mut TestAppContext) {
|
||||||
|
let env = TestEnv::new().unwrap();
|
||||||
|
let cred = AwsCredential {
|
||||||
|
access_key_id: "AKIAEXAMPLE".into(),
|
||||||
|
secret_access_key: "EXAMPLESECRET".into(),
|
||||||
|
};
|
||||||
|
env.db().unwrap().save_aws_credential("work", &cred).unwrap();
|
||||||
|
let window = open_app(&env, cx);
|
||||||
|
fill(window, "passphrase", TEST_PASSPHRASE, cx);
|
||||||
|
click(window, "submit", cx);
|
||||||
|
click(window, "credentials", cx);
|
||||||
|
|
||||||
|
assert!(is_visible(window, "work", cx));
|
||||||
|
|
||||||
|
act(window, cx, |window, cx| window.within("credential-tabs").click(1usize, cx));
|
||||||
|
assert!(!is_visible(window, "work", cx));
|
||||||
|
|
||||||
|
act(window, cx, |window, cx| window.within("credential-tabs").click(0usize, cx));
|
||||||
|
assert!(is_visible(window, "work", cx));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,132 @@
|
|||||||
|
use gpui_kit::{
|
||||||
|
Context, FontWeight, InteractiveElement, IntoElement, ParentElement, Render, SharedString,
|
||||||
|
Styled, TestSupportExt, Window, div, rems,
|
||||||
|
};
|
||||||
|
use gpui_omarchy::{
|
||||||
|
ActiveTheme, ButtonVariant, ChoiceItem, IconName, empty_state, focus_scope, icon_button,
|
||||||
|
tab_list,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::components::error_alert;
|
||||||
|
use crate::credentials::AwsCredential;
|
||||||
|
use crate::session::AppSession;
|
||||||
|
use super::Screen;
|
||||||
|
|
||||||
|
|
||||||
|
pub struct CredentialsScreen {
|
||||||
|
tab: usize,
|
||||||
|
aws: Vec<(String, AwsCredential)>,
|
||||||
|
error: Option<SharedString>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CredentialsScreen {
|
||||||
|
pub fn new(cx: &mut Context<Self>) -> Self {
|
||||||
|
let AppSession::Unlocked(db) = cx.global::<AppSession>() else {
|
||||||
|
unreachable!("credentials screen is only shown while unlocked");
|
||||||
|
};
|
||||||
|
|
||||||
|
let (aws, error) = match db.list_aws_credentials() {
|
||||||
|
Ok(creds) => {
|
||||||
|
let mut creds: Vec<_> = creds.into_iter().collect();
|
||||||
|
creds.sort_by(|a, b| a.0.cmp(&b.0));
|
||||||
|
(creds, None)
|
||||||
|
},
|
||||||
|
Err(e) => (Vec::new(), Some(format!("Failed to load credentials: {e}").into())),
|
||||||
|
};
|
||||||
|
|
||||||
|
Self { tab: 0, aws, error }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Render for CredentialsScreen {
|
||||||
|
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||||
|
let t = cx.omarchy().clone();
|
||||||
|
|
||||||
|
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 if self.aws.is_empty() => empty_state(
|
||||||
|
"AWS access keys",
|
||||||
|
"You have no saved AWS credentials.",
|
||||||
|
cx,
|
||||||
|
),
|
||||||
|
0 => div()
|
||||||
|
.flex()
|
||||||
|
.flex_col()
|
||||||
|
.gap(rems(0.5))
|
||||||
|
.children(self.aws.iter().map(|(name, cred)| {
|
||||||
|
div()
|
||||||
|
.id(SharedString::from(name))
|
||||||
|
.test_support()
|
||||||
|
.flex()
|
||||||
|
.justify_between()
|
||||||
|
.p(rems(0.625))
|
||||||
|
.border_1()
|
||||||
|
.border_color(t.border)
|
||||||
|
.child(div().font_weight(FontWeight::BOLD).child(name.clone()))
|
||||||
|
.child(
|
||||||
|
div()
|
||||||
|
.font_family(t.mono_font.clone())
|
||||||
|
.text_color(t.secondary)
|
||||||
|
.child(cred.access_key_id.clone()),
|
||||||
|
)
|
||||||
|
})),
|
||||||
|
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),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ use gpui_kit::{
|
|||||||
use gpui_omarchy::{ActiveTheme, IconName, focus_scope, icon};
|
use gpui_omarchy::{ActiveTheme, IconName, focus_scope, icon};
|
||||||
|
|
||||||
use crate::session::AppSession;
|
use crate::session::AppSession;
|
||||||
|
use super::Screen;
|
||||||
|
|
||||||
|
|
||||||
pub struct HomeScreen {}
|
pub struct HomeScreen {}
|
||||||
@@ -13,6 +14,8 @@ pub struct HomeScreen {}
|
|||||||
impl Render for HomeScreen {
|
impl Render for HomeScreen {
|
||||||
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||||
let t = cx.omarchy();
|
let t = cx.omarchy();
|
||||||
|
let credentials = nav_tile("credentials", IconName::Key, "Credentials", "Add, remove, and change credentials.", t.success, cx)
|
||||||
|
.on_click(|_, _, cx| cx.set_global(Screen::Credentials));
|
||||||
let lock = nav_tile("lock", IconName::ShieldCheck, "Lock", "Lock Creddy.", t.warning, cx)
|
let lock = nav_tile("lock", IconName::ShieldCheck, "Lock", "Lock Creddy.", t.warning, cx)
|
||||||
.on_click(|_, _, cx| cx.set_global(AppSession::Locked));
|
.on_click(|_, _, cx| cx.set_global(AppSession::Locked));
|
||||||
let exit = nav_tile("exit", IconName::LogOut, "Exit", "Close Creddy.", t.accent, cx)
|
let exit = nav_tile("exit", IconName::LogOut, "Exit", "Close Creddy.", t.accent, cx)
|
||||||
@@ -37,6 +40,7 @@ impl Render for HomeScreen {
|
|||||||
.flex()
|
.flex()
|
||||||
.items_center()
|
.items_center()
|
||||||
.gap(rems(1.5))
|
.gap(rems(1.5))
|
||||||
|
.child(credentials)
|
||||||
.child(lock)
|
.child(lock)
|
||||||
.child(exit),
|
.child(exit),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,7 +1,21 @@
|
|||||||
|
use gpui_kit::Global;
|
||||||
|
|
||||||
|
mod credentials;
|
||||||
mod home;
|
mod home;
|
||||||
mod unlock;
|
mod unlock;
|
||||||
mod welcome;
|
mod welcome;
|
||||||
|
|
||||||
|
pub use credentials::CredentialsScreen;
|
||||||
pub use home::HomeScreen;
|
pub use home::HomeScreen;
|
||||||
pub use unlock::UnlockScreen;
|
pub use unlock::UnlockScreen;
|
||||||
pub use welcome::WelcomeScreen;
|
pub use welcome::WelcomeScreen;
|
||||||
|
|
||||||
|
|
||||||
|
/// Which screen to show while the app is unlocked.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum Screen {
|
||||||
|
Home,
|
||||||
|
Credentials,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Global for Screen {}
|
||||||
Reference in new issue
Block a user