feat: add setup inbox relays modal

This commit is contained in:
2025-02-07 16:11:04 +07:00
parent cb8a348945
commit 0daebe5762
11 changed files with 453 additions and 152 deletions

View File

@@ -14,13 +14,13 @@ use gpui::{
};
#[cfg(target_os = "linux")]
use gpui::{WindowBackgroundAppearance, WindowDecorations};
use log::{error, info};
use log::error;
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 tokio::sync::{mpsc, oneshot};
use ui::{theme::Theme, Root};
use views::{app, onboarding, startup::Startup};
use views::{app, onboarding, startup};
mod asset;
mod views;
@@ -260,10 +260,7 @@ fn main() {
})
.detach();
let root = cx.new(|cx| {
Root::new(cx.new(|cx| Startup::new(window, cx)).into(), window, cx)
});
let root = cx.new(|cx| Root::new(startup::init(window, cx).into(), window, cx));
let weak_root = root.downgrade();
let window_handle = window.window_handle();
let task = cx.read_credentials(KEYRING_SERVICE);
@@ -273,7 +270,7 @@ fn main() {
cx.spawn(|mut cx| async move {
if let Ok(Some((npub, secret))) = task.await {
let (tx, mut rx) = tokio::sync::mpsc::channel::<NostrProfile>(1);
let (tx, rx) = oneshot::channel::<NostrProfile>();
cx.background_executor()
.spawn(async move {
@@ -293,29 +290,18 @@ fn main() {
Metadata::new()
};
if tx
.send(NostrProfile::new(public_key, metadata))
.await
.is_ok()
{
info!("Found account");
}
_ = tx.send(NostrProfile::new(public_key, metadata));
})
.detach();
while let Some(profile) = rx.recv().await {
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()));
if let Some(root) = this.root() {
cx.update_entity(&root, |this: &mut Root, cx| {
this.set_view(
app::init(profile, window, cx).into(),
cx,
);
});
}
this.set_root_view(
app::init(profile, window, cx).into(),
cx,
);
});
})
.unwrap();
@@ -323,11 +309,7 @@ fn main() {
} else {
cx.update_window(window_handle, |_, window, cx| {
cx.update_global::<AppRegistry, _>(|this, cx| {
if let Some(root) = this.root() {
cx.update_entity(&root, |this: &mut Root, cx| {
this.set_view(onboarding::init(window, cx).into(), cx);
});
}
this.set_root_view(onboarding::init(window, cx).into(), cx);
});
})
.unwrap();

View File

@@ -6,17 +6,20 @@ use gpui::{
Entity, InteractiveElement, IntoElement, ObjectFit, ParentElement, Render, Styled, StyledImage,
Window,
};
use nostr_sdk::prelude::*;
use serde::Deserialize;
use state::get_client;
use std::sync::Arc;
use tokio::sync::oneshot;
use ui::{
button::{Button, ButtonVariants},
button::{Button, ButtonRounded, ButtonVariants},
dock_area::{dock::DockPlacement, DockArea, DockItem},
popup_menu::PopupMenuExt,
Icon, IconName, Root, Sizable, TitleBar,
theme::{scale::ColorScaleStep, ActiveTheme},
ContextModal, Icon, IconName, Root, Sizable, TitleBar,
};
use super::{chat, contacts, onboarding, profile, settings, sidebar, welcome};
use super::{chat, contacts, onboarding, profile, relays::Relays, settings, sidebar, welcome};
#[derive(Clone, PartialEq, Eq, Deserialize)]
pub enum PanelKind {
@@ -79,6 +82,77 @@ impl AppView {
view.set_center(center_panel, window, cx);
});
let public_key = account.public_key();
let window_handle = window.window_handle();
// Check user's inbox relays and determine user is ready for NIP17 or not.
// If not, show the setup modal and instruct user setup inbox relays
cx.spawn(|mut cx| async move {
let (tx, rx) = oneshot::channel::<bool>();
cx.background_spawn(async move {
let client = get_client();
let filter = Filter::new()
.kind(Kind::InboxRelays)
.author(public_key)
.limit(1);
let is_ready = if let Ok(events) = client.database().query(filter).await {
events.first_owned().is_some()
} else {
false
};
_ = tx.send(is_ready);
})
.detach();
if let Ok(is_ready) = rx.await {
if is_ready {
//
} else {
cx.update_window(window_handle, |_, window, cx| {
let relays = cx.new(|cx| Relays::new(window, cx));
window.open_modal(cx, move |this, window, cx| {
let is_loading = relays.read(cx).loading();
this.keyboard(false)
.closable(false)
.width(px(420.))
.title("Your Inbox is not configured")
.child(relays.clone())
.footer(
div()
.p_2()
.border_t_1()
.border_color(
cx.theme().base.step(cx, ColorScaleStep::FIVE),
)
.child(
Button::new("update_inbox_relays_btn")
.label("Update")
.primary()
.bold()
.rounded(ButtonRounded::Large)
.w_full()
.loading(is_loading)
.on_click(window.listener_for(
&relays,
|this, _, window, cx| {
this.update(window, cx);
},
)),
),
)
});
})
.unwrap();
}
}
})
.detach();
cx.new(|_| Self { account, dock })
}
@@ -158,13 +232,8 @@ impl AppView {
// Remove user
this.set_user(None);
// Update root view
if let Some(root) = this.root() {
cx.update_entity(&root, |this: &mut Root, cx| {
this.set_view(onboarding::init(window, cx).into(), cx);
});
}
this.set_root_view(onboarding::init(window, cx).into(), cx);
});
}
}

View File

@@ -1,6 +1,7 @@
mod chat;
mod contacts;
mod profile;
mod relays;
mod settings;
mod sidebar;
mod welcome;

View File

@@ -13,7 +13,7 @@ use ui::{
input::{InputEvent, TextInput},
notification::NotificationType,
theme::{scale::ColorScaleStep, ActiveTheme},
ContextModal, Root, Size, StyledExt,
ContextModal, Size, StyledExt,
};
use super::app;
@@ -126,12 +126,7 @@ impl Onboarding {
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(app::init(profile, window, cx).into(), cx);
});
}
this.set_root_view(app::init(profile, window, cx).into(), cx);
});
})
.unwrap();

View File

@@ -0,0 +1,247 @@
use gpui::{
div, prelude::FluentBuilder, px, uniform_list, AppContext, Context, Entity, FocusHandle,
InteractiveElement, IntoElement, ParentElement, Render, Styled, TextAlign, Window,
};
use nostr_sdk::prelude::*;
use state::get_client;
use tokio::sync::oneshot;
use ui::{
button::{Button, ButtonVariants},
input::{InputEvent, TextInput},
theme::{scale::ColorScaleStep, ActiveTheme},
ContextModal, IconName, Sizable,
};
pub struct Relays {
relays: Entity<Vec<Url>>,
input: Entity<TextInput>,
focus_handle: FocusHandle,
is_loading: bool,
}
impl Relays {
pub fn new(window: &mut Window, cx: &mut Context<'_, Self>) -> Self {
let relays = cx.new(|_| {
let mut list = Vec::with_capacity(10);
list.push(Url::parse("wss://auth.nostr1.com").unwrap());
list.push(Url::parse("wss://relay.0xchat.com").unwrap());
list
});
let input = cx.new(|cx| {
TextInput::new(window, cx)
.text_size(ui::Size::XSmall)
.small()
.placeholder("wss://...")
});
cx.subscribe_in(&input, window, move |this, _, input_event, window, cx| {
if let InputEvent::PressEnter = input_event {
this.add(window, cx);
}
})
.detach();
Self {
relays,
input,
is_loading: false,
focus_handle: cx.focus_handle(),
}
}
pub fn update(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let relays = self.relays.read(cx).clone();
let window_handle = window.window_handle();
self.set_loading(true, cx);
cx.spawn(|this, mut cx| async move {
let (tx, rx) = oneshot::channel();
cx.background_spawn(async move {
let client = get_client();
let signer = client.signer().await.unwrap();
let public_key = signer.get_public_key().await.unwrap();
let tags: Vec<Tag> = relays
.into_iter()
.map(|relay| Tag::custom(TagKind::Relay, vec![relay.to_string()]))
.collect();
let event = EventBuilder::new(Kind::InboxRelays, "")
.tags(tags)
.build(public_key)
.sign(&signer)
.await
.unwrap();
if let Ok(output) = client.send_event(&event).await {
_ = tx.send(output.val);
};
})
.detach();
if rx.await.is_ok() {
cx.update_window(window_handle, |_, window, cx| {
window.close_modal(cx);
this.update(cx, |this, cx| {
this.set_loading(false, cx);
})
.unwrap();
})
.unwrap();
}
})
.detach();
}
pub fn loading(&self) -> bool {
self.is_loading
}
fn set_loading(&mut self, status: bool, cx: &mut Context<Self>) {
self.is_loading = status;
cx.notify();
}
fn add(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let value = self.input.read(cx).text().to_string();
if !value.starts_with("ws") {
return;
}
if let Ok(url) = Url::parse(&value) {
self.relays.update(cx, |this, cx| {
if !this.contains(&url) {
this.push(url);
cx.notify();
}
});
self.input.update(cx, |this, cx| {
this.set_text("", window, cx);
});
}
}
fn remove(&mut self, ix: usize, _window: &mut Window, cx: &mut Context<Self>) {
self.relays.update(cx, |this, cx| {
this.remove(ix);
cx.notify();
});
}
}
impl Render for Relays {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let msg = "In order to receive messages from others, you need to setup Inbox Relays. You can use the recommend relays or add more.";
div()
.track_focus(&self.focus_handle)
.flex()
.flex_col()
.gap_2()
.child(
div()
.px_2()
.text_xs()
.text_color(cx.theme().base.step(cx, ColorScaleStep::ELEVEN))
.child(msg),
)
.child(
div()
.px_2()
.flex()
.flex_col()
.gap_2()
.child(
div()
.flex()
.items_center()
.gap_2()
.child(self.input.clone())
.child(
Button::new("add_relay_btn")
.icon(IconName::Plus)
.small()
.rounded(px(cx.theme().radius))
.on_click(
cx.listener(|this, _, window, cx| this.add(window, cx)),
),
),
)
.map(|this| {
let view = cx.entity();
let relays = self.relays.read(cx).clone();
let total = relays.len();
if !relays.is_empty() {
this.child(
uniform_list(
view,
"relays",
total,
move |_, range, _window, cx| {
let mut items = Vec::with_capacity(total);
for ix in range {
let item = relays.get(ix).unwrap().clone().to_string();
items.push(
div().group("").w_full().h_9().py_0p5().child(
div()
.px_2()
.h_full()
.w_full()
.flex()
.items_center()
.justify_between()
.rounded(px(cx.theme().radius))
.bg(cx
.theme()
.base
.step(cx, ColorScaleStep::THREE))
.text_xs()
.child(item)
.child(
Button::new("remove_{ix}")
.icon(IconName::Close)
.xsmall()
.ghost()
.invisible()
.group_hover("", |this| {
this.visible()
})
.on_click(cx.listener(
move |this, _, window, cx| {
this.remove(ix, window, cx)
},
)),
),
),
)
}
items
},
)
.min_h(px(120.)),
)
} else {
this.h_20()
.mb_2()
.flex()
.items_center()
.justify_center()
.text_xs()
.text_align(TextAlign::Center)
.child("Please add some relays.")
}
}),
)
}
}

View File

@@ -306,7 +306,7 @@ impl Render for Compose {
.gap_2()
.px_2()
.child(
Button::new("add")
Button::new("add_user_to_compose_btn")
.icon(IconName::Plus)
.small()
.rounded(ButtonRounded::Size(px(9999.)))
@@ -319,7 +319,10 @@ impl Render for Compose {
)
.map(|this| {
if let Some(contacts) = self.contacts.read(cx).clone() {
if contacts.is_empty() {
let view = cx.entity();
let total = contacts.len();
if total != 0 {
this.child(
div()
.w_full()
@@ -350,9 +353,9 @@ impl Render for Compose {
} else {
this.child(
uniform_list(
cx.entity().clone(),
view,
"contacts",
contacts.len(),
total,
move |this, range, _window, cx| {
let selected = this.selected.read(cx);
let mut items = Vec::new();

View File

@@ -64,7 +64,7 @@ impl Sidebar {
.border_t_1()
.border_color(cx.theme().base.step(cx, ColorScaleStep::FIVE))
.child(
Button::new("create")
Button::new("create_dm_btn")
.label(label)
.primary()
.bold()

View File

@@ -1,11 +1,17 @@
use gpui::{div, svg, Context, IntoElement, ParentElement, Render, Styled, Window};
use gpui::{
div, svg, App, AppContext, Context, Entity, IntoElement, ParentElement, Render, Styled, Window,
};
use ui::theme::{scale::ColorScaleStep, ActiveTheme};
pub fn init(window: &mut Window, cx: &mut App) -> Entity<Startup> {
Startup::new(window, cx)
}
pub struct Startup {}
impl Startup {
pub fn new(_window: &mut Window, _cx: &mut Context<'_, Self>) -> Self {
Self {}
pub fn new(_window: &mut Window, cx: &mut App) -> Entity<Self> {
cx.new(|_| Self {})
}
}

View File

@@ -2,7 +2,7 @@ use common::{
constants::{ALL_MESSAGES_SUB_ID, NEW_MESSAGE_SUB_ID},
profile::NostrProfile,
};
use gpui::{App, Entity, Global, WeakEntity};
use gpui::{AnyView, App, Global, WeakEntity};
use nostr_sdk::prelude::*;
use state::get_client;
use std::time::Duration;
@@ -38,11 +38,6 @@ impl AppRegistry {
// Create a filter for getting all gift wrapped events send to current user
let all_messages = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
// Subscription options
let opts = SubscribeAutoCloseOptions::default().exit_policy(
ReqExitPolicy::WaitDurationAfterEOSE(Duration::from_secs(5)),
);
// Create a filter for getting new message
let new_message = Filter::new()
.kind(Kind::GiftWrap)
@@ -51,7 +46,13 @@ impl AppRegistry {
// Subscribe for all messages
_ = client
.subscribe_with_id(all_messages_sub_id, all_messages, Some(opts))
.subscribe_with_id(
all_messages_sub_id,
all_messages,
Some(SubscribeAutoCloseOptions::default().exit_policy(
ReqExitPolicy::WaitDurationAfterEOSE(Duration::from_secs(5)),
)),
)
.await;
// Subscribe for new message
@@ -75,7 +76,11 @@ impl AppRegistry {
self.user.clone()
}
pub fn root(&self) -> Option<Entity<Root>> {
self.root.upgrade()
pub fn set_root_view(&self, view: AnyView, cx: &mut App) {
if let Err(e) = self.root.update(cx, |this, cx| {
this.set_view(view, cx);
}) {
println!("Error: {}", e)
}
}
}

View File

@@ -32,7 +32,7 @@ pub struct Modal {
max_width: Option<Pixels>,
margin_top: Option<Pixels>,
on_close: OnClose,
show_close: bool,
closable: bool,
keyboard: bool,
/// This will be change when open the modal, the focus handle is create when open the modal.
pub(crate) focus_handle: FocusHandle,
@@ -61,9 +61,9 @@ impl Modal {
max_width: None,
overlay: true,
keyboard: true,
closable: true,
layer_ix: 0,
on_close: Rc::new(|_, _, _| {}),
show_close: true,
}
}
@@ -88,9 +88,9 @@ impl Modal {
self
}
/// Sets the false to hide close icon, default: true
pub fn show_close(mut self, show_close: bool) -> Self {
self.show_close = show_close;
/// Sets the false to make modal unclosable, default: true
pub fn closable(mut self, closable: bool) -> Self {
self.closable = closable;
self
}
@@ -170,31 +170,20 @@ impl RenderOnce for Modal {
.when(self.overlay, |this| {
this.bg(cx.theme().base.step_alpha(cx, ColorScaleStep::EIGHT))
})
.on_mouse_down(MouseButton::Left, {
let on_close = self.on_close.clone();
move |_, window, cx| {
on_close(&ClickEvent::default(), window, cx);
window.close_modal(cx);
}
.when(self.closable, |this| {
this.on_mouse_down(MouseButton::Left, {
let on_close = self.on_close.clone();
move |_, window, cx| {
on_close(&ClickEvent::default(), window, cx);
window.close_modal(cx);
}
})
})
.child(
self.base
.id(SharedString::from(format!("modal-{layer_ix}")))
.key_context(CONTEXT)
.track_focus(&self.focus_handle)
.when(self.keyboard, |this| {
this.on_action({
let on_close = self.on_close.clone();
move |_: &Escape, window, cx| {
// FIXME:
//
// Here some Modal have no focus_handle, so it will not work will Escape key.
// But by now, we `cx.close_modal()` going to close the last active model, so the Escape is unexpected to work.
on_close(&ClickEvent::default(), window, cx);
window.close_modal(cx);
}
})
})
.absolute()
.occlude()
.relative()
@@ -215,7 +204,20 @@ impl RenderOnce for Modal {
.child(title),
)
})
.when(self.show_close, |this| {
.when(self.keyboard, |this| {
this.on_action({
let on_close = self.on_close.clone();
move |_: &Escape, window, cx| {
// FIXME:
//
// Here some Modal have no focus_handle, so it will not work will Escape key.
// But by now, we `cx.close_modal()` going to close the last active model, so the Escape is unexpected to work.
on_close(&ClickEvent::default(), window, cx);
window.close_modal(cx);
}
})
})
.when(self.closable, |this| {
this.child(
Button::new(SharedString::from(format!(
"modal-close-{layer_ix}"