feat: make global state simpler
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
use app_state::registry::AppRegistry;
|
||||
use chat_state::registry::ChatRegistry;
|
||||
use common::profile::NostrProfile;
|
||||
use gpui::{
|
||||
actions, div, img, impl_internal_actions, px, App, AppContext, Axis, BorrowAppContext, Context,
|
||||
Entity, InteractiveElement, IntoElement, ObjectFit, ParentElement, Render, Styled, StyledImage,
|
||||
Window,
|
||||
actions, div, img, impl_internal_actions, px, App, AppContext, Axis, Context, Entity,
|
||||
InteractiveElement, IntoElement, ObjectFit, ParentElement, Render, Styled, StyledImage, Window,
|
||||
};
|
||||
use nostr_sdk::prelude::*;
|
||||
use serde::Deserialize;
|
||||
@@ -200,13 +198,11 @@ impl AppView {
|
||||
}
|
||||
}
|
||||
PanelKind::Profile => {
|
||||
if let Some(profile) = cx.global::<AppRegistry>().user() {
|
||||
let panel = Arc::new(profile::init(profile, window, cx));
|
||||
let panel = Arc::new(profile::init(self.account.clone(), window, cx));
|
||||
|
||||
self.dock.update(cx, |dock_area, cx| {
|
||||
dock_area.add_panel(panel, action.position, window, cx);
|
||||
});
|
||||
}
|
||||
self.dock.update(cx, |dock_area, cx| {
|
||||
dock_area.add_panel(panel, action.position, window, cx);
|
||||
});
|
||||
}
|
||||
PanelKind::Contacts => {
|
||||
let panel = Arc::new(contacts::init(window, cx));
|
||||
@@ -229,10 +225,6 @@ impl AppView {
|
||||
cx.background_spawn(async move { get_client().reset().await })
|
||||
.detach();
|
||||
|
||||
cx.update_global::<AppRegistry, _>(|this, _cx| {
|
||||
this.set_user(None);
|
||||
});
|
||||
|
||||
window.replace_root(cx, |window, cx| {
|
||||
Root::new(onboarding::init(window, cx).into(), window, cx)
|
||||
});
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
use app_state::registry::AppRegistry;
|
||||
use common::{profile::NostrProfile, qr::create_qr};
|
||||
use common::{profile::NostrProfile, qr::create_qr, utils::preload};
|
||||
use gpui::{
|
||||
div, img, prelude::FluentBuilder, relative, svg, App, AppContext, BorrowAppContext,
|
||||
ClipboardItem, Context, Div, Entity, IntoElement, ParentElement, Render, Styled, Window,
|
||||
div, img, prelude::FluentBuilder, relative, svg, App, AppContext, ClipboardItem, Context, Div,
|
||||
Entity, IntoElement, ParentElement, Render, Styled, Window,
|
||||
};
|
||||
use nostr_connect::prelude::*;
|
||||
use state::get_client;
|
||||
@@ -103,8 +102,10 @@ impl Onboarding {
|
||||
.ok()
|
||||
.unwrap_or_default();
|
||||
|
||||
_ = client.set_signer(signer).await;
|
||||
_ = tx.send(NostrProfile::new(*public_key, metadata));
|
||||
if tx.send(NostrProfile::new(*public_key, metadata)).is_ok() {
|
||||
_ = client.set_signer(signer).await;
|
||||
_ = preload(client, *public_key).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -113,10 +114,6 @@ impl Onboarding {
|
||||
|
||||
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()));
|
||||
});
|
||||
|
||||
window.replace_root(cx, |window, cx| {
|
||||
Root::new(app::init(profile, window, cx).into(), window, cx)
|
||||
});
|
||||
@@ -173,18 +170,16 @@ impl Onboarding {
|
||||
.ok()
|
||||
.unwrap_or_default();
|
||||
|
||||
_ = client.set_signer(keys).await;
|
||||
_ = tx.send(NostrProfile::new(public_key, metadata));
|
||||
if tx.send(NostrProfile::new(public_key, metadata)).is_ok() {
|
||||
_ = client.set_signer(keys).await;
|
||||
_ = preload(client, public_key).await;
|
||||
}
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
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()));
|
||||
});
|
||||
|
||||
window.replace_root(cx, |window, cx| {
|
||||
Root::new(app::init(profile, window, cx).into(), window, cx)
|
||||
});
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use app_state::registry::AppRegistry;
|
||||
use chat_state::registry::ChatRegistry;
|
||||
use common::{
|
||||
constants::FAKE_SIG,
|
||||
@@ -141,18 +140,9 @@ impl Compose {
|
||||
return;
|
||||
}
|
||||
|
||||
let current_user = if let Some(profile) = cx.global::<AppRegistry>().user() {
|
||||
profile
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Show loading spinner
|
||||
self.set_submitting(true, cx);
|
||||
|
||||
// Get nostr client
|
||||
let client = get_client();
|
||||
|
||||
// Get message from user's input
|
||||
let content = message.to_string();
|
||||
|
||||
@@ -163,9 +153,7 @@ impl Compose {
|
||||
);
|
||||
|
||||
// Get all pubkeys
|
||||
let current_user = current_user.public_key();
|
||||
let mut pubkeys: Vec<PublicKey> = selected.iter().copied().collect();
|
||||
pubkeys.push(current_user);
|
||||
|
||||
// Convert selected pubkeys into Nostr tags
|
||||
let mut tag_list: Vec<Tag> = selected.iter().map(|pk| Tag::public_key(*pk)).collect();
|
||||
@@ -178,14 +166,18 @@ impl Compose {
|
||||
let (tx, rx) = oneshot::channel::<Event>();
|
||||
|
||||
cx.background_spawn(async move {
|
||||
let client = get_client();
|
||||
let public_key = signer_public_key(client).await.unwrap();
|
||||
let mut event: Option<Event> = None;
|
||||
|
||||
pubkeys.push(public_key);
|
||||
|
||||
for pubkey in pubkeys.iter() {
|
||||
if let Ok(output) = client
|
||||
.send_private_msg(*pubkey, &content, tags.clone())
|
||||
.await
|
||||
{
|
||||
if pubkey == ¤t_user && event.is_none() {
|
||||
if pubkey == &public_key && event.is_none() {
|
||||
if let Ok(Some(ev)) = client.database().event_by_id(&output.val).await {
|
||||
if let Ok(UnwrappedGift { mut rumor, .. }) =
|
||||
client.unwrap_gift_wrap(&ev).await
|
||||
|
||||
Reference in New Issue
Block a user