diff --git a/src/assets.rs b/src/assets.rs index b9d1b60..db1cd8b 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -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 -icon_assets!(CreddyIcons, [LogOut, ShieldCheck]); +icon_assets!(CreddyIcons, [Key, LogOut, ShieldCheck]); /// Our extra icons, falling back to gpui-kit's defaults. pub struct CreddyAssets; diff --git a/src/main.rs b/src/main.rs index 100dc12..fc87534 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ mod test_data; use config::AppConfig; use session::AppSession; -use screens::{HomeScreen, UnlockScreen, WelcomeScreen}; +use screens::{CredentialsScreen, HomeScreen, Screen, UnlockScreen, WelcomeScreen}; struct CreddyApp {} @@ -34,9 +34,14 @@ impl Render for CreddyApp { AppSession::Locked => window .use_keyed_state("unlock", cx, |window, cx| UnlockScreen::new(window, cx)) .into_any_element(), - AppSession::Unlocked { .. } => window - .use_keyed_state("home", cx, |_, _| HomeScreen {}) - .into_any_element(), + AppSession::Unlocked { .. } => match cx.global::() { + Screen::Home => window + .use_keyed_state("home", cx, |_, _| HomeScreen {}) + .into_any_element(), + Screen::Credentials => window + .use_keyed_state("credentials", cx, |_, cx| CredentialsScreen::new(cx)) + .into_any_element(), + }, }; let theme = cx.omarchy(); @@ -54,10 +59,12 @@ fn launch(cx: &mut App, config: AppConfig, session: AppSession) -> WindowHandle< gpui_omarchy::init(cx); cx.set_global(config); cx.set_global(session); + cx.set_global(Screen::Home); // 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 cx.observe_global::(|cx| cx.refresh_windows()).detach(); cx.observe_global::(|cx| cx.refresh_windows()).detach(); + cx.observe_global::(|cx| cx.refresh_windows()).detach(); cx.open_window(WindowOptions::default(), |_, cx| cx.new(|_| CreddyApp {})) .expect("open window") @@ -83,6 +90,7 @@ mod tests { use gpui_kit::{AnyWindowHandle, TestAppContext}; use gpui_kit::test::TestWindowExt; use super::*; + use crate::credentials::AwsCredential; use crate::test_data::{TEST_PASSPHRASE, TestEnv}; /// 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!(!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::Credentials); + assert!(is_visible(window, "back", cx)); + + click(window, "back", cx); + assert_eq!(cx.read(|cx| *cx.global::()), 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)); + } } diff --git a/src/screens/credentials.rs b/src/screens/credentials.rs new file mode 100644 index 0000000..2d83ea4 --- /dev/null +++ b/src/screens/credentials.rs @@ -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, +} + +impl CredentialsScreen { + pub fn new(cx: &mut Context) -> Self { + let AppSession::Unlocked(db) = cx.global::() 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) -> 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), + ) + } +} diff --git a/src/screens/home.rs b/src/screens/home.rs index b1c2ead..c870c0c 100644 --- a/src/screens/home.rs +++ b/src/screens/home.rs @@ -6,6 +6,7 @@ use gpui_kit::{ use gpui_omarchy::{ActiveTheme, IconName, focus_scope, icon}; use crate::session::AppSession; +use super::Screen; pub struct HomeScreen {} @@ -13,6 +14,8 @@ pub struct HomeScreen {} impl Render for HomeScreen { fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { 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) .on_click(|_, _, cx| cx.set_global(AppSession::Locked)); let exit = nav_tile("exit", IconName::LogOut, "Exit", "Close Creddy.", t.accent, cx) @@ -37,6 +40,7 @@ impl Render for HomeScreen { .flex() .items_center() .gap(rems(1.5)) + .child(credentials) .child(lock) .child(exit), ) diff --git a/src/screens/mod.rs b/src/screens/mod.rs index e02fdcd..c5ef35a 100644 --- a/src/screens/mod.rs +++ b/src/screens/mod.rs @@ -1,7 +1,21 @@ +use gpui_kit::Global; + +mod credentials; mod home; mod unlock; mod welcome; +pub use credentials::CredentialsScreen; pub use home::HomeScreen; pub use unlock::UnlockScreen; 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 {}