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 chat::registry::ChatRegistry;
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(room) = weak_room.upgrade() {
let panel = Arc::new(ChatPanel::new(room, window, cx));
self.dock.update(cx, |dock_area, cx| {
dock_area.add_panel(panel, action.position, window, cx);
});
@@ -137,8 +136,8 @@ impl AppView {
fn on_profile_action(
&mut self,
_action: &OpenProfile,
window: &mut Window,
cx: &mut Context<Self>,
_window: &mut Window,
_cx: &mut Context<Self>,
) {
// TODO
}
@@ -146,8 +145,8 @@ impl AppView {
fn on_contacts_action(
&mut self,
_action: &OpenContacts,
window: &mut Window,
cx: &mut Context<Self>,
_window: &mut Window,
_cx: &mut Context<Self>,
) {
// TODO
}
@@ -155,17 +154,27 @@ impl AppView {
fn on_settings_action(
&mut self,
_action: &OpenSettings,
window: &mut Window,
cx: &mut Context<Self>,
_window: &mut Window,
_cx: &mut Context<Self>,
) {
// TODO
}
fn on_logout_action(&mut self, _action: &Logout, _window: &mut Window, cx: &mut Context<Self>) {
cx.update_global::<AppRegistry, _>(|_this, cx| {
fn on_logout_action(&mut self, _action: &Logout, window: &mut Window, cx: &mut Context<Self>) {
cx.update_global::<AppRegistry, _>(|this, cx| {
cx.background_executor()
.spawn(async move { get_client().reset().await })
.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 members = room.members.clone();
let owner = room.owner.clone();
@@ -221,7 +221,7 @@ impl ChatPanel {
fn load_new_messages(
&self,
model: WeakEntity<Room>,
window: &mut Window,
_window: &mut Window,
cx: &mut Context<Self>,
) {
if let Some(model) = model.upgrade() {
@@ -265,6 +265,7 @@ impl ChatPanel {
window: &mut Window,
cx: &mut Context<Self>,
) {
let window_handle = window.window_handle();
let room = self.room.read(cx);
let owner = room.owner.clone();
let mut members = room.members.to_vec();
@@ -297,11 +298,9 @@ impl ChatPanel {
});
}
/*
cx.spawn(|this, mut async_cx| async move {
cx.spawn(|this, mut cx| async move {
// Send message to all members
async_cx
.background_executor()
cx.background_executor()
.spawn({
let client = get_client();
let content = content.clone().to_string();
@@ -328,7 +327,7 @@ impl ChatPanel {
.detach();
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| {
let message = Message::new(
owner,
@@ -339,23 +338,26 @@ impl ChatPanel {
model.items.extend(vec![message]);
model.count = model.items.len();
cx.notify();
})
});
cx.notify();
});
}
if let Some(input) = view.upgrade() {
_ = async_cx.update_entity(&input, |input, cx| {
cx.update_window(window_handle, |_, window, cx| {
cx.update_entity(&input, |input, cx| {
input.set_loading(false, window, cx);
input.set_disabled(false, window, cx);
input.set_text("", window, cx);
});
})
.unwrap()
}
})
.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 paths = cx.prompt_for_paths(PathPromptOptions {
files: true,
@@ -412,7 +414,7 @@ impl ChatPanel {
.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| {
if let Some(urls) = model.as_mut() {
let ix = urls.iter().position(|x| x == url).unwrap();
@@ -469,7 +471,7 @@ impl Focusable 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()
.size_full()
.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 nostr_sdk::prelude::*;
use state::get_client;
@@ -33,7 +33,7 @@ impl Onboarding {
fn save_keys(
content: &str,
window: &mut Window,
_window: &mut Window,
cx: &mut Context<Self>,
) -> anyhow::Result<(), anyhow::Error> {
let keys = Keys::parse(content)?;
@@ -41,7 +41,7 @@ impl Onboarding {
let bech32 = public_key.to_bech32()?;
let secret = keys.secret_key().to_secret_hex();
let mut async_cx = cx.to_async();
let async_cx = cx.to_async();
cx.foreground_executor()
.spawn({
@@ -67,7 +67,7 @@ impl Onboarding {
})
.await;
if let Ok(metadata) = query {
if let Ok(_metadata) = query {
//
}
}
@@ -80,7 +80,7 @@ impl 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()
.size_full()
.flex()

View File

@@ -78,21 +78,17 @@ impl Compose {
)
.detach();
cx.spawn(|this, mut async_cx| {
let client = get_client();
async move {
cx.spawn(|this, mut async_cx| async move {
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())
})
.map(|profile| NostrProfile::new(profile.public_key(), profile.metadata()))
.collect();
Ok(members)
@@ -111,7 +107,6 @@ impl Compose {
});
}
}
}
})
.detach();
@@ -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 {
"Create Group DM".into()
} else {
@@ -170,12 +165,12 @@ impl Compose {
}
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 input = self.user_input.downgrade();
// Show loading spinner
self.is_loading = true;
cx.notify();
self.set_loading(true, cx);
if let Ok(public_key) = PublicKey::parse(&content) {
cx.spawn(|this, mut async_cx| async move {
@@ -206,14 +201,15 @@ impl Compose {
cx.notify();
});
this.is_loading = false;
cx.notify();
this.set_loading(false, cx);
});
}
if let Some(input) = input.upgrade() {
_ = async_cx.update_entity(&input, |input, cx| {
// input.set_text("", window, cx);
_ = async_cx.update_window(window_handle, |_, 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(
&mut self,
action: &SelectContact,
window: &mut Window,
_window: &mut Window,
cx: &mut Context<Self>,
) {
self.selected.update(cx, |this, cx| {
@@ -242,7 +243,7 @@ impl 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 =
"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(),
"contacts",
contacts.len(),
move |this, range, window, cx| {
move |this, range, _window, cx| {
let selected = this.selected.read(cx);
let mut items = Vec::new();
@@ -366,9 +367,12 @@ impl Render for Compose {
.step(cx, ColorScaleStep::THREE))
})
.on_click(move |_, window, cx| {
cx.dispatch_action(&SelectContact(
window.dispatch_action(
Box::new(SelectContact(
item.public_key(),
));
)),
cx,
);
}),
);
}

View File

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