wip: refactor

This commit is contained in:
2025-02-01 09:07:01 +07:00
parent a61fd27b9d
commit 944d3b9e46
5 changed files with 96 additions and 81 deletions

View File

@@ -1,4 +1,4 @@
use super::{chat::ChatPanel, sidebar::Sidebar, welcome::WelcomePanel}; use super::{chat::ChatPanel, onboarding::Onboarding, sidebar::Sidebar, welcome::WelcomePanel};
use app_state::registry::AppRegistry; use app_state::registry::AppRegistry;
use chat::registry::ChatRegistry; use chat::registry::ChatRegistry;
use common::profile::NostrProfile; use common::profile::NostrProfile;
@@ -116,7 +116,6 @@ impl AppView {
if let Some(weak_room) = cx.global::<ChatRegistry>().get_room(id, cx) { if let Some(weak_room) = cx.global::<ChatRegistry>().get_room(id, cx) {
if let Some(room) = weak_room.upgrade() { if let Some(room) = weak_room.upgrade() {
let panel = Arc::new(ChatPanel::new(room, window, cx)); let panel = Arc::new(ChatPanel::new(room, window, cx));
self.dock.update(cx, |dock_area, cx| { self.dock.update(cx, |dock_area, cx| {
dock_area.add_panel(panel, action.position, window, cx); dock_area.add_panel(panel, action.position, window, cx);
}); });
@@ -137,8 +136,8 @@ impl AppView {
fn on_profile_action( fn on_profile_action(
&mut self, &mut self,
_action: &OpenProfile, _action: &OpenProfile,
window: &mut Window, _window: &mut Window,
cx: &mut Context<Self>, _cx: &mut Context<Self>,
) { ) {
// TODO // TODO
} }
@@ -146,8 +145,8 @@ impl AppView {
fn on_contacts_action( fn on_contacts_action(
&mut self, &mut self,
_action: &OpenContacts, _action: &OpenContacts,
window: &mut Window, _window: &mut Window,
cx: &mut Context<Self>, _cx: &mut Context<Self>,
) { ) {
// TODO // TODO
} }
@@ -155,17 +154,27 @@ impl AppView {
fn on_settings_action( fn on_settings_action(
&mut self, &mut self,
_action: &OpenSettings, _action: &OpenSettings,
window: &mut Window, _window: &mut Window,
cx: &mut Context<Self>, _cx: &mut Context<Self>,
) { ) {
// TODO // TODO
} }
fn on_logout_action(&mut self, _action: &Logout, _window: &mut Window, cx: &mut Context<Self>) { fn on_logout_action(&mut self, _action: &Logout, window: &mut Window, cx: &mut Context<Self>) {
cx.update_global::<AppRegistry, _>(|_this, cx| { cx.update_global::<AppRegistry, _>(|this, cx| {
cx.background_executor() cx.background_executor()
.spawn(async move { get_client().reset().await }) .spawn(async move { get_client().reset().await })
.detach(); .detach();
// 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(cx.new(|cx| Onboarding::new(window, cx)).into(), cx);
});
}
}); });
} }
} }

View File

@@ -135,7 +135,7 @@ impl ChatPanel {
}) })
} }
fn load_messages(&self, window: &mut Window, cx: &mut Context<Self>) { fn load_messages(&self, _window: &mut Window, cx: &mut Context<Self>) {
let room = self.room.read(cx); let room = self.room.read(cx);
let members = room.members.clone(); let members = room.members.clone();
let owner = room.owner.clone(); let owner = room.owner.clone();
@@ -221,7 +221,7 @@ impl ChatPanel {
fn load_new_messages( fn load_new_messages(
&self, &self,
model: WeakEntity<Room>, model: WeakEntity<Room>,
window: &mut Window, _window: &mut Window,
cx: &mut Context<Self>, cx: &mut Context<Self>,
) { ) {
if let Some(model) = model.upgrade() { if let Some(model) = model.upgrade() {
@@ -265,6 +265,7 @@ impl ChatPanel {
window: &mut Window, window: &mut Window,
cx: &mut Context<Self>, cx: &mut Context<Self>,
) { ) {
let window_handle = window.window_handle();
let room = self.room.read(cx); let room = self.room.read(cx);
let owner = room.owner.clone(); let owner = room.owner.clone();
let mut members = room.members.to_vec(); let mut members = room.members.to_vec();
@@ -297,11 +298,9 @@ impl ChatPanel {
}); });
} }
/* cx.spawn(|this, mut cx| async move {
cx.spawn(|this, mut async_cx| async move {
// Send message to all members // Send message to all members
async_cx cx.background_executor()
.background_executor()
.spawn({ .spawn({
let client = get_client(); let client = get_client();
let content = content.clone().to_string(); let content = content.clone().to_string();
@@ -328,7 +327,7 @@ impl ChatPanel {
.detach(); .detach();
if let Some(view) = this.upgrade() { if let Some(view) = this.upgrade() {
_ = async_cx.update_entity(&view, |this, cx| { _ = cx.update_entity(&view, |this, cx| {
cx.update_entity(&this.state, |model, cx| { cx.update_entity(&this.state, |model, cx| {
let message = Message::new( let message = Message::new(
owner, owner,
@@ -339,23 +338,26 @@ impl ChatPanel {
model.items.extend(vec![message]); model.items.extend(vec![message]);
model.count = model.items.len(); model.count = model.items.len();
cx.notify(); cx.notify();
}) });
cx.notify();
}); });
} }
if let Some(input) = view.upgrade() { if let Some(input) = view.upgrade() {
_ = async_cx.update_entity(&input, |input, cx| { cx.update_window(window_handle, |_, window, cx| {
input.set_loading(false, window, cx); cx.update_entity(&input, |input, cx| {
input.set_disabled(false, window, cx); input.set_loading(false, window, cx);
input.set_text("", window, cx); input.set_disabled(false, window, cx);
}); input.set_text("", window, cx);
});
})
.unwrap()
} }
}) })
.detach(); .detach();
*/
} }
fn upload(&mut self, window: &mut Window, cx: &mut Context<Self>) { fn upload(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
let attaches = self.attaches.clone(); let attaches = self.attaches.clone();
let paths = cx.prompt_for_paths(PathPromptOptions { let paths = cx.prompt_for_paths(PathPromptOptions {
files: true, files: true,
@@ -412,7 +414,7 @@ impl ChatPanel {
.detach(); .detach();
} }
fn remove(&mut self, url: &Url, window: &mut Window, cx: &mut Context<Self>) { fn remove(&mut self, url: &Url, _window: &mut Window, cx: &mut Context<Self>) {
self.attaches.update(cx, |model, cx| { self.attaches.update(cx, |model, cx| {
if let Some(urls) = model.as_mut() { if let Some(urls) = model.as_mut() {
let ix = urls.iter().position(|x| x == url).unwrap(); let ix = urls.iter().position(|x| x == url).unwrap();
@@ -469,7 +471,7 @@ impl Focusable for ChatPanel {
} }
impl Render for ChatPanel { impl Render for ChatPanel {
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 {
v_flex() v_flex()
.size_full() .size_full()
.child(list(self.list.clone()).flex_1()) .child(list(self.list.clone()).flex_1())

View File

@@ -1,4 +1,4 @@
use common::{constants::KEYRING_SERVICE, profile::NostrProfile}; use common::constants::KEYRING_SERVICE;
use gpui::{div, AppContext, Context, Entity, IntoElement, ParentElement, Render, Styled, Window}; use gpui::{div, AppContext, Context, Entity, IntoElement, ParentElement, Render, Styled, Window};
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use state::get_client; use state::get_client;
@@ -33,7 +33,7 @@ impl Onboarding {
fn save_keys( fn save_keys(
content: &str, content: &str,
window: &mut Window, _window: &mut Window,
cx: &mut Context<Self>, cx: &mut Context<Self>,
) -> anyhow::Result<(), anyhow::Error> { ) -> anyhow::Result<(), anyhow::Error> {
let keys = Keys::parse(content)?; let keys = Keys::parse(content)?;
@@ -41,7 +41,7 @@ impl Onboarding {
let bech32 = public_key.to_bech32()?; let bech32 = public_key.to_bech32()?;
let secret = keys.secret_key().to_secret_hex(); let secret = keys.secret_key().to_secret_hex();
let mut async_cx = cx.to_async(); let async_cx = cx.to_async();
cx.foreground_executor() cx.foreground_executor()
.spawn({ .spawn({
@@ -67,7 +67,7 @@ impl Onboarding {
}) })
.await; .await;
if let Ok(metadata) = query { if let Ok(_metadata) = query {
// //
} }
} }
@@ -80,7 +80,7 @@ impl Onboarding {
} }
impl Render for Onboarding { 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() div()
.size_full() .size_full()
.flex() .flex()

View File

@@ -78,38 +78,33 @@ impl Compose {
) )
.detach(); .detach();
cx.spawn(|this, mut async_cx| { cx.spawn(|this, mut async_cx| async move {
let client = get_client(); let query: anyhow::Result<Vec<NostrProfile>, anyhow::Error> = async_cx
.background_executor()
.spawn(async move {
let client = get_client();
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let profiles = client.database().contacts(public_key).await?;
let members: Vec<NostrProfile> = profiles
.into_iter()
.map(|profile| NostrProfile::new(profile.public_key(), profile.metadata()))
.collect();
async move { Ok(members)
let query: anyhow::Result<Vec<NostrProfile>, anyhow::Error> = async_cx })
.background_executor() .await;
.spawn(async move {
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let profiles = client.database().contacts(public_key).await?;
let members: Vec<NostrProfile> = profiles
.into_iter()
.map(|profile| {
NostrProfile::new(profile.public_key(), profile.metadata())
})
.collect();
Ok(members)
})
.await;
if let Ok(contacts) = query {
if let Some(view) = this.upgrade() {
_ = async_cx.update_entity(&view, |this, cx| {
this.contacts.update(cx, |this, cx| {
*this = Some(contacts);
cx.notify();
});
if let Ok(contacts) = query {
if let Some(view) = this.upgrade() {
_ = async_cx.update_entity(&view, |this, cx| {
this.contacts.update(cx, |this, cx| {
*this = Some(contacts);
cx.notify(); cx.notify();
}); });
}
cx.notify();
});
} }
} }
}) })
@@ -161,7 +156,7 @@ impl Compose {
} }
} }
pub fn label(&self, window: &Window, cx: &App) -> SharedString { pub fn label(&self, _window: &Window, cx: &App) -> SharedString {
if self.selected.read(cx).len() > 1 { if self.selected.read(cx).len() > 1 {
"Create Group DM".into() "Create Group DM".into()
} else { } else {
@@ -170,12 +165,12 @@ impl Compose {
} }
fn add(&mut self, window: &mut Window, cx: &mut Context<Self>) { fn add(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let window_handle = window.window_handle();
let content = self.user_input.read(cx).text().to_string(); let content = self.user_input.read(cx).text().to_string();
let input = self.user_input.downgrade(); let input = self.user_input.downgrade();
// Show loading spinner // Show loading spinner
self.is_loading = true; self.set_loading(true, cx);
cx.notify();
if let Ok(public_key) = PublicKey::parse(&content) { if let Ok(public_key) = PublicKey::parse(&content) {
cx.spawn(|this, mut async_cx| async move { cx.spawn(|this, mut async_cx| async move {
@@ -206,14 +201,15 @@ impl Compose {
cx.notify(); cx.notify();
}); });
this.is_loading = false; this.set_loading(false, cx);
cx.notify();
}); });
} }
if let Some(input) = input.upgrade() { if let Some(input) = input.upgrade() {
_ = async_cx.update_entity(&input, |input, cx| { _ = async_cx.update_window(window_handle, |_, window, cx| {
// input.set_text("", window, cx); cx.update_entity(&input, |this, cx| {
this.set_text("", window, cx);
})
}); });
} }
} }
@@ -224,10 +220,15 @@ impl Compose {
} }
} }
fn set_loading(&mut self, status: bool, cx: &mut Context<Self>) {
self.is_loading = status;
cx.notify();
}
fn on_action_select( fn on_action_select(
&mut self, &mut self,
action: &SelectContact, action: &SelectContact,
window: &mut Window, _window: &mut Window,
cx: &mut Context<Self>, cx: &mut Context<Self>,
) { ) {
self.selected.update(cx, |this, cx| { self.selected.update(cx, |this, cx| {
@@ -242,7 +243,7 @@ impl Compose {
} }
impl Render for Compose { impl Render for Compose {
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 {
let msg = let msg =
"Start a conversation with someone using their npub or NIP-05 (like foo@bar.com)."; "Start a conversation with someone using their npub or NIP-05 (like foo@bar.com).";
@@ -319,7 +320,7 @@ impl Render for Compose {
cx.entity().clone(), cx.entity().clone(),
"contacts", "contacts",
contacts.len(), contacts.len(),
move |this, range, window, cx| { move |this, range, _window, cx| {
let selected = this.selected.read(cx); let selected = this.selected.read(cx);
let mut items = Vec::new(); let mut items = Vec::new();
@@ -366,9 +367,12 @@ impl Render for Compose {
.step(cx, ColorScaleStep::THREE)) .step(cx, ColorScaleStep::THREE))
}) })
.on_click(move |_, window, cx| { .on_click(move |_, window, cx| {
cx.dispatch_action(&SelectContact( window.dispatch_action(
item.public_key(), Box::new(SelectContact(
)); item.public_key(),
)),
cx,
);
}), }),
); );
} }

View File

@@ -5,7 +5,6 @@ use gpui::{
div, img, percentage, prelude::FluentBuilder, px, Context, InteractiveElement, IntoElement, div, img, percentage, prelude::FluentBuilder, px, Context, InteractiveElement, IntoElement,
ParentElement, Render, SharedString, StatefulInteractiveElement, Styled, Window, ParentElement, Render, SharedString, StatefulInteractiveElement, Styled, Window,
}; };
use std::sync::Arc;
use ui::{ use ui::{
dock_area::dock::DockPlacement, dock_area::dock::DockPlacement,
skeleton::Skeleton, skeleton::Skeleton,
@@ -103,13 +102,14 @@ impl Inbox {
} }
} }
fn action(&self, id: u64, _window: &mut Window, cx: &mut Context<Self>) { fn action(&self, id: u64, window: &mut Window, cx: &mut Context<Self>) {
let action = AddPanel { window.dispatch_action(
panel: PanelKind::Room(id), Box::new(AddPanel {
position: DockPlacement::Center, panel: PanelKind::Room(id),
}; position: DockPlacement::Center,
}),
cx.dispatch_action(&action) cx,
);
} }
} }