chore: refactor chat registry

This commit is contained in:
2025-02-11 15:01:03 +07:00
parent 76d2c14870
commit 140a16e617
13 changed files with 297 additions and 319 deletions

View File

@@ -1,5 +1,4 @@
use cargo_packager_updater::{check_update, semver::Version, url::Url};
use chat_state::registry::ChatRegistry;
use common::{
constants::{UPDATER_PUBKEY, UPDATER_URL},
profile::NostrProfile,
@@ -217,14 +216,10 @@ impl AppView {
fn on_panel_action(&mut self, action: &AddPanel, window: &mut Window, cx: &mut Context<Self>) {
match &action.panel {
PanelKind::Room(id) => {
if let Some(weak_room) = cx.global::<ChatRegistry>().get_room(id, cx) {
if let Some(room) = weak_room.upgrade() {
let panel = Arc::new(chat::init(&room, window, cx));
self.dock.update(cx, |dock_area, cx| {
dock_area.add_panel(panel, action.position, window, cx);
});
}
if let Ok(panel) = chat::init(id, window, cx) {
self.dock.update(cx, |dock_area, cx| {
dock_area.add_panel(panel, action.position, window, cx);
});
}
}
PanelKind::Profile => {

View File

@@ -1,5 +1,9 @@
use std::sync::Arc;
use anyhow::anyhow;
use async_utility::task::spawn;
use chat_state::room::{LastSeen, Room};
use chats::registry::ChatRegistry;
use chats::room::{LastSeen, Room};
use common::{
constants::IMAGE_SERVICE,
profile::NostrProfile,
@@ -28,8 +32,20 @@ use ui::{
mod message;
pub fn init(room: &Entity<Room>, window: &mut Window, cx: &mut App) -> Entity<Chat> {
Chat::new(room, window, cx)
pub fn init(
id: &u64,
window: &mut Window,
cx: &mut App,
) -> Result<Arc<Entity<Chat>>, anyhow::Error> {
if let Some(chats) = ChatRegistry::global(cx) {
if let Some(room) = chats.read(cx).get(id, cx) {
Ok(Arc::new(Chat::new(&room, window, cx)))
} else {
Err(anyhow!("Chat room is not exist"))
}
} else {
Err(anyhow!("Chat Registry is not initialized"))
}
}
#[derive(Clone)]

View File

@@ -1,4 +1,4 @@
use chat_state::registry::ChatRegistry;
use chats::registry::ChatRegistry;
use common::{
constants::FAKE_SIG,
profile::NostrProfile,
@@ -6,8 +6,8 @@ use common::{
};
use gpui::{
div, img, impl_internal_actions, prelude::FluentBuilder, px, relative, uniform_list, App,
AppContext, BorrowAppContext, Context, Entity, FocusHandle, InteractiveElement, IntoElement,
ParentElement, Render, SharedString, StatefulInteractiveElement, Styled, TextAlign, Window,
AppContext, Context, Entity, FocusHandle, InteractiveElement, IntoElement, ParentElement,
Render, SharedString, StatefulInteractiveElement, Styled, TextAlign, Window,
};
use nostr_sdk::prelude::*;
use serde::Deserialize;
@@ -212,9 +212,11 @@ impl Compose {
if let Ok(event) = rx.await {
_ = cx.update_window(window_handle, |_, window, cx| {
cx.update_global::<ChatRegistry, _>(|this, cx| {
this.new_room_message(event, window, cx);
});
if let Some(chats) = ChatRegistry::global(cx) {
chats.update(cx, |this, cx| {
this.push_message(event, cx);
});
}
// Stop loading spinner
_ = this.update(cx, |this, cx| {

View File

@@ -1,5 +1,5 @@
use crate::views::app::{AddPanel, PanelKind};
use chat_state::registry::ChatRegistry;
use chats::registry::ChatRegistry;
use gpui::{
div, img, percentage, prelude::FluentBuilder, px, relative, Context, InteractiveElement,
IntoElement, ParentElement, Render, SharedString, StatefulInteractiveElement, Styled,
@@ -39,15 +39,14 @@ impl Inbox {
}
fn render_item(&self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let weak_model = cx.global::<ChatRegistry>().inbox();
if let Some(model) = weak_model.upgrade() {
if let Some(chats) = ChatRegistry::global(cx) {
div().map(|this| {
let inbox = model.read(cx);
let state = chats.read(cx);
let rooms = state.rooms();
if inbox.is_loading {
if state.is_loading() {
this.children(self.render_skeleton(5))
} else if inbox.rooms.is_empty() {
} else if rooms.is_empty() {
this.px_1()
.w_full()
.h_20()
@@ -72,7 +71,7 @@ impl Inbox {
.child("Recent chats will appear here."),
)
} else {
this.children(inbox.rooms.iter().map(|model| {
this.children(rooms.iter().map(|model| {
let room = model.read(cx);
let room_id: SharedString = room.id.to_string().into();