wip: refactor

This commit is contained in:
2024-12-25 13:44:16 +07:00
parent 37d810d9e5
commit 0745b497f0
13 changed files with 117 additions and 120 deletions

View File

@@ -1,6 +1,6 @@
use coop_ui::{
button::Button,
dock::{Panel, PanelEvent, PanelState, TitleStyle},
dock::{Panel, PanelEvent, PanelState},
popup_menu::PopupMenu,
};
use gpui::*;
@@ -18,11 +18,13 @@ pub struct ChatPanel {
zoomable: bool,
focus_handle: FocusHandle,
// Room
id: SharedString,
room: View<ChatRoom>,
}
impl ChatPanel {
pub fn new(room: &Arc<Room>, cx: &mut WindowContext) -> View<Self> {
let id = room.id.clone();
let room = cx.new_view(|cx| {
let view = ChatRoom::new(room, cx);
// Load messages
@@ -38,24 +40,21 @@ impl ChatPanel {
closeable: true,
zoomable: true,
focus_handle: cx.focus_handle(),
id,
room,
})
}
}
impl Panel for ChatPanel {
fn panel_name(&self) -> &'static str {
"ChatPanel"
fn panel_name(&self) -> SharedString {
self.id.clone()
}
fn title(&self, _cx: &WindowContext) -> AnyElement {
self.name.clone().into_any_element()
}
fn title_style(&self, _cx: &WindowContext) -> Option<TitleStyle> {
None
}
fn closeable(&self, _cx: &WindowContext) -> bool {
self.closeable
}

View File

@@ -181,22 +181,20 @@ impl ChatRoom {
cx.foreground_executor()
.spawn({
let client = get_client();
let owner = self.owner;
let members = self.members.to_vec();
let recv = Filter::new()
.kind(Kind::PrivateDirectMessage)
.author(owner)
.pubkeys(members.clone());
let send = Filter::new()
.kind(Kind::PrivateDirectMessage)
.authors(members)
.pubkey(owner);
async move {
let signer = client.signer().await.unwrap();
let public_key = signer.get_public_key().await.unwrap();
let recv = Filter::new()
.kind(Kind::PrivateDirectMessage)
.authors(members.clone())
.pubkey(public_key);
let send = Filter::new()
.kind(Kind::PrivateDirectMessage)
.author(public_key)
.pubkeys(members);
let events = async_cx
.background_executor()
.spawn(async move { client.database().query(vec![recv, send]).await })

View File

@@ -126,7 +126,7 @@ impl RenderOnce for Item {
)
.on_click(move |_, cx| {
cx.dispatch_action(Box::new(AddPanel {
room: self.room.clone(),
room: Arc::clone(&self.room),
position: coop_ui::dock::DockPlacement::Center,
}))
})
@@ -136,31 +136,15 @@ impl RenderOnce for Item {
pub struct InboxItem {
room: Arc<Room>,
metadata: Model<Option<Metadata>>,
pub(crate) sender: PublicKey,
}
impl InboxItem {
pub fn new(event: Event, cx: &mut ViewContext<'_, Self>) -> Self {
let sender = event.pubkey;
let last_seen = event.created_at;
// Get all members from event's tag
let mut members: Vec<PublicKey> = event.tags.public_keys().copied().collect();
// Add sender to members
members.insert(0, sender);
// Get title from event's tag
let title = if let Some(tag) = event.tags.find(TagKind::Title) {
tag.content().map(|s| s.to_string())
} else {
// TODO: create random name?
None
};
let room = Arc::new(Room::new(event));
let metadata = cx.new_model(|_| None);
// Request metadata
_ = cx.global::<SignalRegistry>().tx.send(sender);
_ = cx.global::<SignalRegistry>().tx.send(room.owner);
// Reload when received metadata
cx.observe_global::<MetadataRegistry>(|chat, cx| {
@@ -168,22 +152,11 @@ impl InboxItem {
})
.detach();
let room = Arc::new(Room {
title,
members,
last_seen,
owner: sender,
});
Self {
room,
sender,
metadata,
}
Self { room, metadata }
}
pub fn load_metadata(&mut self, cx: &mut ViewContext<Self>) {
let public_key = self.sender;
let public_key = self.room.owner;
let async_metadata = self.metadata.clone();
let mut async_cx = cx.to_async();
@@ -205,9 +178,13 @@ impl InboxItem {
.detach();
}
pub fn id(&self) -> String {
self.room.id.clone().into()
}
fn render_item(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
let metadata = self.metadata.read(cx).clone();
let room = self.room.clone();
let room = Arc::clone(&self.room);
Item::new(room, metadata)
}

View File

@@ -8,7 +8,7 @@ use nostr_sdk::prelude::*;
use prelude::FluentBuilder;
use std::cmp::Reverse;
use crate::{get_client, states::chat::ChatRegistry};
use crate::{get_client, states::chat::ChatRegistry, utils::get_room_id};
pub mod item;
@@ -35,15 +35,18 @@ impl Inbox {
let new_messages = state.new_messages.clone();
// Get all current chats
let current: Vec<PublicKey> = items
.iter()
.map(|item| item.model.read(cx).sender)
.collect();
let current_rooms: Vec<String> =
items.iter().map(|item| item.model.read(cx).id()).collect();
// Create view for only new chats
// Create view for new chats only
let new = new_messages
.into_iter()
.filter(|m| current.iter().any(|pk| pk == &m.event.pubkey))
.filter(|m| {
let keys = m.event.tags.public_keys().copied().collect::<Vec<_>>();
let nid = get_room_id(&m.event.pubkey, &keys);
current_rooms.iter().any(|id| id == &nid)
})
.map(|m| cx.new_view(|cx| InboxItem::new(m.event, cx)))
.collect::<Vec<_>>();

View File

@@ -1,6 +1,6 @@
use coop_ui::{
button::Button,
dock::{Panel, PanelEvent, PanelState, TitleStyle},
dock::{Panel, PanelEvent, PanelState},
popup_menu::PopupMenu,
scroll::ScrollbarAxis,
StyledExt,
@@ -40,18 +40,14 @@ impl LeftDock {
}
impl Panel for LeftDock {
fn panel_name(&self) -> &'static str {
"ChatPanel"
fn panel_name(&self) -> SharedString {
"LeftDock".into()
}
fn title(&self, _cx: &WindowContext) -> AnyElement {
self.name.clone().into_any_element()
}
fn title_style(&self, _cx: &WindowContext) -> Option<TitleStyle> {
None
}
fn closeable(&self, _cx: &WindowContext) -> bool {
self.closeable
}

View File

@@ -1,6 +1,6 @@
use coop_ui::{
button::Button,
dock::{Panel, PanelEvent, PanelState, TitleStyle},
dock::{Panel, PanelEvent, PanelState},
popup_menu::PopupMenu,
theme::{ActiveTheme, Colorize},
StyledExt,
@@ -30,18 +30,14 @@ impl WelcomePanel {
}
impl Panel for WelcomePanel {
fn panel_name(&self) -> &'static str {
"WelcomePanel"
fn panel_name(&self) -> SharedString {
"WelcomePanel".into()
}
fn title(&self, _cx: &WindowContext) -> AnyElement {
self.name.clone().into_any_element()
}
fn title_style(&self, _cx: &WindowContext) -> Option<TitleStyle> {
None
}
fn closeable(&self, _cx: &WindowContext) -> bool {
self.closeable
}