wip: refactor
This commit is contained in:
@@ -17,7 +17,7 @@ use crate::states::account::AccountRegistry;
|
||||
#[derive(Clone, PartialEq, Eq, Deserialize)]
|
||||
pub struct AddPanel {
|
||||
pub title: Option<String>,
|
||||
pub receiver: PublicKey,
|
||||
pub from: PublicKey,
|
||||
}
|
||||
|
||||
impl_actions!(dock, [AddPanel]);
|
||||
@@ -98,7 +98,7 @@ impl AppView {
|
||||
}
|
||||
|
||||
fn on_action_add_panel(&mut self, action: &AddPanel, cx: &mut ViewContext<Self>) {
|
||||
let chat_panel = Arc::new(ChatPanel::new(action.receiver, cx));
|
||||
let chat_panel = Arc::new(ChatPanel::new(action.from, cx));
|
||||
|
||||
self.dock.update(cx, |dock_area, cx| {
|
||||
dock_area.add_panel(chat_panel, DockPlacement::Center, cx);
|
||||
|
||||
66
crates/app/src/views/dock/chat/messages.rs
Normal file
66
crates/app/src/views/dock/chat/messages.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
use gpui::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
|
||||
use crate::get_client;
|
||||
|
||||
pub struct Messages {
|
||||
messages: Model<Option<Events>>,
|
||||
}
|
||||
|
||||
impl Messages {
|
||||
pub fn new(from: PublicKey, cx: &mut ViewContext<'_, Self>) -> Self {
|
||||
let messages = cx.new_model(|_| None);
|
||||
let async_messages = messages.clone();
|
||||
|
||||
let mut async_cx = cx.to_async();
|
||||
|
||||
cx.foreground_executor()
|
||||
.spawn(async move {
|
||||
let client = get_client();
|
||||
let signer = client.signer().await.unwrap();
|
||||
let public_key = signer.get_public_key().await.unwrap();
|
||||
|
||||
let recv_filter = Filter::new()
|
||||
.kind(Kind::PrivateDirectMessage)
|
||||
.author(from)
|
||||
.pubkey(public_key);
|
||||
|
||||
let sender_filter = Filter::new()
|
||||
.kind(Kind::PrivateDirectMessage)
|
||||
.author(public_key)
|
||||
.pubkey(from);
|
||||
|
||||
let events = async_cx
|
||||
.background_executor()
|
||||
.spawn(async move {
|
||||
client
|
||||
.database()
|
||||
.query(vec![recv_filter, sender_filter])
|
||||
.await
|
||||
})
|
||||
.await;
|
||||
|
||||
if let Ok(events) = events {
|
||||
_ = async_cx.update_model(&async_messages, |a, b| {
|
||||
*a = Some(events);
|
||||
b.notify();
|
||||
});
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
Self { messages }
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for Messages {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
let mut content = div().size_full().flex().flex_col().justify_end();
|
||||
|
||||
if let Some(messages) = self.messages.read(cx).as_ref() {
|
||||
content = content.children(messages.clone().into_iter().map(|m| div().child(m.content)))
|
||||
}
|
||||
|
||||
div().flex_1().child(content)
|
||||
}
|
||||
}
|
||||
@@ -7,9 +7,10 @@ use coop_ui::{
|
||||
Sizable,
|
||||
};
|
||||
use gpui::*;
|
||||
use messages::Messages;
|
||||
use nostr_sdk::*;
|
||||
|
||||
pub mod list;
|
||||
pub mod messages;
|
||||
|
||||
pub struct ChatPanel {
|
||||
// Panel
|
||||
@@ -18,13 +19,14 @@ pub struct ChatPanel {
|
||||
zoomable: bool,
|
||||
focus_handle: FocusHandle,
|
||||
// Chat Room
|
||||
receiver: PublicKey,
|
||||
messages: View<Messages>,
|
||||
input: View<TextInput>,
|
||||
}
|
||||
|
||||
impl ChatPanel {
|
||||
pub fn new(receiver: PublicKey, cx: &mut WindowContext) -> View<Self> {
|
||||
pub fn new(from: PublicKey, cx: &mut WindowContext) -> View<Self> {
|
||||
let input = cx.new_view(TextInput::new);
|
||||
let messages = cx.new_view(|cx| Messages::new(from, cx));
|
||||
|
||||
input.update(cx, |input, _cx| {
|
||||
input.set_placeholder("Message");
|
||||
@@ -35,7 +37,7 @@ impl ChatPanel {
|
||||
closeable: true,
|
||||
zoomable: true,
|
||||
focus_handle: cx.focus_handle(),
|
||||
receiver,
|
||||
messages,
|
||||
input,
|
||||
})
|
||||
}
|
||||
@@ -89,14 +91,7 @@ impl Render for ChatPanel {
|
||||
.size_full()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.child(
|
||||
div()
|
||||
.flex_1()
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.child(self.receiver.to_hex()),
|
||||
)
|
||||
.child(self.messages.clone())
|
||||
.child(
|
||||
div()
|
||||
.flex_shrink_0()
|
||||
|
||||
@@ -130,7 +130,7 @@ impl RenderOnce for ChatItem {
|
||||
.on_click(move |_, cx| {
|
||||
cx.dispatch_action(Box::new(AddPanel {
|
||||
title: self.title.clone(),
|
||||
receiver: self.public_key,
|
||||
from: self.public_key,
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user