feat: improve startup screen
This commit is contained in:
@@ -20,6 +20,7 @@ gpui_tokio.workspace = true
|
||||
reqwest_client.workspace = true
|
||||
|
||||
tokio.workspace = true
|
||||
nostr-connect.workspace = true
|
||||
nostr-sdk.workspace = true
|
||||
anyhow.workspace = true
|
||||
serde.workspace = true
|
||||
|
||||
@@ -19,8 +19,8 @@ use nostr_sdk::prelude::*;
|
||||
use state::{get_client, initialize_client};
|
||||
use std::{borrow::Cow, collections::HashSet, ops::Deref, str::FromStr, sync::Arc, time::Duration};
|
||||
use tokio::sync::mpsc;
|
||||
use ui::Root;
|
||||
use views::{app::AppView, onboarding::Onboarding, startup::Startup};
|
||||
use ui::{theme::Theme, Root};
|
||||
use views::{app::AppView, onboarding, startup::Startup};
|
||||
|
||||
mod asset;
|
||||
mod views;
|
||||
@@ -254,6 +254,11 @@ fn main() {
|
||||
.open_window(opts, |window, cx| {
|
||||
window.set_window_title(APP_NAME);
|
||||
window.set_app_id(APP_ID);
|
||||
window
|
||||
.observe_window_appearance(|window, cx| {
|
||||
Theme::sync_system_appearance(Some(window), cx);
|
||||
})
|
||||
.detach();
|
||||
|
||||
let root = cx.new(|cx| {
|
||||
Root::new(cx.new(|cx| Startup::new(window, cx)).into(), window, cx)
|
||||
@@ -321,10 +326,7 @@ fn main() {
|
||||
cx.update_global::<AppRegistry, _>(|this, cx| {
|
||||
if let Some(root) = this.root() {
|
||||
cx.update_entity(&root, |this: &mut Root, cx| {
|
||||
this.set_view(
|
||||
cx.new(|cx| Onboarding::new(window, cx)).into(),
|
||||
cx,
|
||||
);
|
||||
this.set_view(onboarding::init(window, cx).into(), cx);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
use super::{
|
||||
chat, contacts, onboarding::Onboarding, profile, settings, sidebar::Sidebar,
|
||||
welcome::WelcomePanel,
|
||||
};
|
||||
use app_state::registry::AppRegistry;
|
||||
use chat_state::registry::ChatRegistry;
|
||||
use common::profile::NostrProfile;
|
||||
@@ -20,6 +16,10 @@ use ui::{
|
||||
Icon, IconName, Root, Sizable, TitleBar,
|
||||
};
|
||||
|
||||
use super::{
|
||||
chat, contacts, onboarding, profile, settings, sidebar::Sidebar, welcome::WelcomePanel,
|
||||
};
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Deserialize)]
|
||||
pub enum PanelKind {
|
||||
Room(u64),
|
||||
@@ -170,7 +170,7 @@ impl AppView {
|
||||
// Update root view
|
||||
if let Some(root) = this.root() {
|
||||
cx.update_entity(&root, |this: &mut Root, cx| {
|
||||
this.set_view(cx.new(|cx| Onboarding::new(window, cx)).into(), cx);
|
||||
this.set_view(onboarding::init(window, cx).into(), cx);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,121 +1,265 @@
|
||||
use app_state::registry::AppRegistry;
|
||||
use common::{constants::KEYRING_SERVICE, profile::NostrProfile};
|
||||
use common::profile::NostrProfile;
|
||||
use gpui::{
|
||||
div, AppContext, BorrowAppContext, Context, Entity, IntoElement, ParentElement, Render, Styled,
|
||||
Window,
|
||||
div, relative, svg, App, AppContext, BorrowAppContext, Context, Entity, IntoElement,
|
||||
ParentElement, Render, Styled, Window,
|
||||
};
|
||||
use nostr_sdk::prelude::*;
|
||||
use nostr_connect::prelude::*;
|
||||
use state::get_client;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::oneshot;
|
||||
use ui::{
|
||||
button::{Button, ButtonVariants},
|
||||
input::{InputEvent, TextInput},
|
||||
Root,
|
||||
notification::NotificationType,
|
||||
prelude::FluentBuilder,
|
||||
theme::{scale::ColorScaleStep, ActiveTheme},
|
||||
ContextModal, Root, Size, StyledExt,
|
||||
};
|
||||
|
||||
use super::app::AppView;
|
||||
|
||||
const ALPHA_MESSAGE: &str = "Coop is in the alpha stage; it does not store any credentials. You will need to log in again when you reopen the app.";
|
||||
const JOIN_URL: &str = "https://start.njump.me/";
|
||||
|
||||
pub fn init(window: &mut Window, cx: &mut App) -> Entity<Onboarding> {
|
||||
Onboarding::new(window, cx)
|
||||
}
|
||||
|
||||
pub struct Onboarding {
|
||||
input: Entity<TextInput>,
|
||||
use_connect: bool,
|
||||
use_privkey: bool,
|
||||
is_loading: bool,
|
||||
}
|
||||
|
||||
impl Onboarding {
|
||||
pub fn new(window: &mut Window, cx: &mut Context<'_, Self>) -> Self {
|
||||
pub fn new(window: &mut Window, cx: &mut App) -> Entity<Self> {
|
||||
let input = cx.new(|cx| {
|
||||
let mut input = TextInput::new(window, cx);
|
||||
input.set_size(ui::Size::Medium, window, cx);
|
||||
input
|
||||
TextInput::new(window, cx)
|
||||
.text_size(Size::XSmall)
|
||||
.placeholder("nsec...")
|
||||
});
|
||||
|
||||
cx.subscribe_in(
|
||||
&input,
|
||||
window,
|
||||
move |_, text_input, input_event, window, cx| {
|
||||
if let InputEvent::PressEnter = input_event {
|
||||
let content = text_input.read(cx).text().to_string();
|
||||
_ = Self::save_keys(&content, window, cx);
|
||||
}
|
||||
},
|
||||
)
|
||||
.detach();
|
||||
cx.new(|cx| {
|
||||
cx.subscribe_in(
|
||||
&input,
|
||||
window,
|
||||
move |this: &mut Self, _, input_event, window, cx| {
|
||||
if let InputEvent::PressEnter = input_event {
|
||||
this.login(window, cx);
|
||||
}
|
||||
},
|
||||
)
|
||||
.detach();
|
||||
|
||||
Self { input }
|
||||
Self {
|
||||
input,
|
||||
use_connect: false,
|
||||
use_privkey: false,
|
||||
is_loading: false,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn save_keys(
|
||||
content: &str,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) -> anyhow::Result<(), anyhow::Error> {
|
||||
let keys = Keys::parse(content)?;
|
||||
let public_key = keys.public_key();
|
||||
let bech32 = public_key.to_bech32()?;
|
||||
let secret = keys.secret_key().to_secret_hex();
|
||||
fn use_connect(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
|
||||
self.use_connect = true;
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn use_privkey(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
|
||||
self.use_privkey = true;
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn reset(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
|
||||
self.use_privkey = false;
|
||||
self.use_connect = false;
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn set_loading(&mut self, status: bool, cx: &mut Context<Self>) {
|
||||
self.is_loading = status;
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn login(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let value = self.input.read(cx).text().to_string();
|
||||
|
||||
if !value.starts_with("nsec") || value.is_empty() {
|
||||
window.push_notification((NotificationType::Warning, "Private Key is required"), cx);
|
||||
return;
|
||||
}
|
||||
|
||||
// Show loading spinner
|
||||
self.set_loading(true, cx);
|
||||
|
||||
let window_handle = window.window_handle();
|
||||
let task = cx.write_credentials(KEYRING_SERVICE, &bech32, secret.as_bytes());
|
||||
let keys = if let Ok(keys) = Keys::parse(&value) {
|
||||
keys
|
||||
} else {
|
||||
// TODO: handle error
|
||||
return;
|
||||
};
|
||||
|
||||
cx.spawn(|_, mut cx| async move {
|
||||
let client = get_client();
|
||||
let (tx, rx) = oneshot::channel::<NostrProfile>();
|
||||
|
||||
if task.await.is_ok() {
|
||||
let (tx, mut rx) = tokio::sync::mpsc::channel::<NostrProfile>(1);
|
||||
cx.background_executor()
|
||||
.spawn(async move {
|
||||
let public_key = keys.get_public_key().await.unwrap();
|
||||
let metadata = client
|
||||
.fetch_metadata(public_key, Duration::from_secs(3))
|
||||
.await
|
||||
.ok()
|
||||
.unwrap_or(Metadata::new());
|
||||
let profile = NostrProfile::new(public_key, metadata);
|
||||
|
||||
cx.background_executor()
|
||||
.spawn(async move {
|
||||
// Update signer
|
||||
_ = client.set_signer(keys).await;
|
||||
_ = tx.send(profile);
|
||||
_ = client.set_signer(keys).await;
|
||||
})
|
||||
.detach();
|
||||
|
||||
// Get metadata
|
||||
let metadata = if let Ok(Some(metadata)) =
|
||||
client.database().metadata(public_key).await
|
||||
{
|
||||
metadata
|
||||
} else {
|
||||
Metadata::new()
|
||||
};
|
||||
if let Ok(profile) = rx.await {
|
||||
cx.update_window(window_handle, |_, window, cx| {
|
||||
cx.update_global::<AppRegistry, _>(|this, cx| {
|
||||
this.set_user(Some(profile.clone()));
|
||||
|
||||
_ = tx.send(NostrProfile::new(public_key, metadata)).await;
|
||||
})
|
||||
.await;
|
||||
|
||||
while let Some(profile) = rx.recv().await {
|
||||
cx.update_window(window_handle, |_, window, cx| {
|
||||
cx.update_global::<AppRegistry, _>(|this, cx| {
|
||||
this.set_user(Some(profile.clone()));
|
||||
|
||||
if let Some(root) = this.root() {
|
||||
cx.update_entity(&root, |this: &mut Root, cx| {
|
||||
this.set_view(
|
||||
cx.new(|cx| AppView::new(profile, window, cx)).into(),
|
||||
cx,
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
if let Some(root) = this.root() {
|
||||
cx.update_entity(&root, |this: &mut Root, cx| {
|
||||
this.set_view(
|
||||
cx.new(|cx| AppView::new(profile, window, cx)).into(),
|
||||
cx,
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for Onboarding {
|
||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
div()
|
||||
.size_full()
|
||||
.relative()
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.child(
|
||||
div()
|
||||
.size_1_3()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.gap_1()
|
||||
.child(div().child("Private Key").text_sm())
|
||||
.child(self.input.clone()),
|
||||
.items_center()
|
||||
.gap_6()
|
||||
.child(
|
||||
div()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.items_center()
|
||||
.gap_4()
|
||||
.child(
|
||||
svg()
|
||||
.path("brand/coop.svg")
|
||||
.size_12()
|
||||
.text_color(cx.theme().base.step(cx, ColorScaleStep::THREE)),
|
||||
)
|
||||
.child(
|
||||
div()
|
||||
.text_align(gpui::TextAlign::Center)
|
||||
.child(
|
||||
div()
|
||||
.text_lg()
|
||||
.font_semibold()
|
||||
.line_height(relative(1.2))
|
||||
.child("Welcome to Coop!"),
|
||||
)
|
||||
.child(
|
||||
div()
|
||||
.text_sm()
|
||||
.text_color(
|
||||
cx.theme().base.step(cx, ColorScaleStep::ELEVEN),
|
||||
)
|
||||
.child("A Nostr client for secure communication."),
|
||||
),
|
||||
),
|
||||
)
|
||||
.child(div().w_72().map(|this| {
|
||||
if self.use_privkey {
|
||||
this.flex()
|
||||
.flex_col()
|
||||
.gap_2()
|
||||
.child(
|
||||
div()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.gap_1()
|
||||
.text_xs()
|
||||
.child("Private Key:")
|
||||
.child(self.input.clone()),
|
||||
)
|
||||
.child(
|
||||
Button::new("login")
|
||||
.label("Login")
|
||||
.primary()
|
||||
.w_full()
|
||||
.loading(self.is_loading)
|
||||
.on_click(cx.listener(move |this, _, window, cx| {
|
||||
this.login(window, cx);
|
||||
})),
|
||||
)
|
||||
.child(
|
||||
Button::new("cancel")
|
||||
.label("Cancel")
|
||||
.ghost()
|
||||
.w_full()
|
||||
.on_click(cx.listener(move |this, _, window, cx| {
|
||||
this.reset(window, cx);
|
||||
})),
|
||||
)
|
||||
} else {
|
||||
this.flex()
|
||||
.flex_col()
|
||||
.items_center()
|
||||
.gap_2()
|
||||
.child(
|
||||
Button::new("login_btn")
|
||||
.label("Login with Private Key")
|
||||
.primary()
|
||||
.w_full()
|
||||
.on_click(cx.listener(move |this, _, window, cx| {
|
||||
this.use_privkey(window, cx);
|
||||
})),
|
||||
)
|
||||
.child(
|
||||
Button::new("join_btn")
|
||||
.label("Join Nostr")
|
||||
.ghost()
|
||||
.w_full()
|
||||
.on_click(|_, _, cx| {
|
||||
cx.open_url(JOIN_URL);
|
||||
}),
|
||||
)
|
||||
}
|
||||
})),
|
||||
)
|
||||
.child(
|
||||
div()
|
||||
.absolute()
|
||||
.bottom_2()
|
||||
.w_full()
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.text_xs()
|
||||
.text_color(cx.theme().base.step(cx, ColorScaleStep::ELEVEN))
|
||||
.text_align(gpui::TextAlign::Center)
|
||||
.child(ALPHA_MESSAGE),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user