wip: refactor

This commit is contained in:
2025-02-01 08:30:24 +07:00
parent 82f18fc478
commit a61fd27b9d
25 changed files with 547 additions and 507 deletions

View File

@@ -1,10 +1,12 @@
use super::{chat::ChatPanel, onboarding::Onboarding, sidebar::Sidebar, welcome::WelcomePanel};
use super::{chat::ChatPanel, sidebar::Sidebar, welcome::WelcomePanel};
use app_state::registry::AppRegistry;
use chat::registry::ChatRegistry;
use common::profile::NostrProfile;
use gpui::{
actions, div, img, impl_internal_actions, px, svg, App, AppContext, Axis, BorrowAppContext,
Context, Edges, Entity, InteractiveElement, IntoElement, ObjectFit, ParentElement, Render,
Styled, StyledImage, WeakEntity, Window,
actions, div, img, impl_internal_actions, px, AppContext, Axis, BorrowAppContext, Context,
Edges, Entity, InteractiveElement, IntoElement, ObjectFit, ParentElement, Render, Styled,
StyledImage, Window,
};
use registry::{app::AppRegistry, chat::ChatRegistry, contact::Contact};
use serde::Deserialize;
use state::get_client;
use std::sync::Arc;
@@ -13,8 +15,6 @@ use ui::{
dock_area::{dock::DockPlacement, DockArea, DockItem},
notification::NotificationType,
popup_menu::PopupMenuExt,
prelude::FluentBuilder,
theme::{scale::ColorScaleStep, ActiveTheme},
ContextModal, Icon, IconName, Root, Sizable, TitleBar,
};
@@ -45,48 +45,32 @@ pub const DOCK_AREA: DockAreaTab = DockAreaTab {
};
pub struct AppView {
onboarding: Entity<Onboarding>,
account: NostrProfile,
dock: Entity<DockArea>,
}
impl AppView {
pub fn new(window: &mut Window, cx: &mut Context<'_, Self>) -> AppView {
let onboarding = cx.new(|cx| Onboarding::new(window, cx));
pub fn new(account: NostrProfile, window: &mut Window, cx: &mut Context<'_, Self>) -> AppView {
let dock = cx.new(|cx| DockArea::new(DOCK_AREA.id, Some(DOCK_AREA.version), window, cx));
let weak_dock = dock.downgrade();
// Get current user from app state
let weak_user = cx.global::<AppRegistry>().user();
if let Some(user) = weak_user.upgrade() {
cx.observe_in(&user, window, |view, this, window, cx| {
if this.read(cx).is_some() {
Self::render_dock(view.dock.downgrade(), window, cx);
}
})
.detach();
}
AppView { onboarding, dock }
}
fn render_dock(dock_area: WeakEntity<DockArea>, window: &mut Window, cx: &mut App) {
let left = DockItem::panel(Arc::new(Sidebar::new(window, cx)));
let center = DockItem::split_with_sizes(
Axis::Vertical,
vec![DockItem::tabs(
vec![Arc::new(WelcomePanel::new(window, cx))],
None,
&dock_area,
&weak_dock,
window,
cx,
)],
vec![None],
&dock_area,
&weak_dock,
window,
cx,
);
_ = dock_area.update(cx, |view, cx| {
_ = weak_dock.update(cx, |view, cx| {
view.set_version(DOCK_AREA.version, window, cx);
view.set_left_dock(left, Some(px(240.)), true, window, cx);
view.set_center(center, window, cx);
@@ -101,16 +85,18 @@ impl AppView {
// TODO: support right dock?
// TODO: support bottom dock?
});
AppView { account, dock }
}
fn render_account(&self, account: Contact) -> impl IntoElement {
fn render_account(&self) -> impl IntoElement {
Button::new("account")
.ghost()
.xsmall()
.reverse()
.icon(Icon::new(IconName::ChevronDownSmall))
.child(
img(account.avatar())
img(self.account.avatar())
.size_5()
.rounded_full()
.object_fit(ObjectFit::Cover),
@@ -127,7 +113,7 @@ 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>().room(id, cx) {
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));
@@ -175,10 +161,8 @@ impl AppView {
// TODO
}
fn on_logout_action(&mut self, _action: &Logout, window: &mut Window, cx: &mut Context<Self>) {
cx.update_global::<AppRegistry, _>(|this, cx| {
this.logout(cx);
// Reset nostr client
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();
@@ -190,56 +174,35 @@ impl Render for AppView {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let modal_layer = Root::render_modal_layer(window, cx);
let notification_layer = Root::render_notification_layer(window, cx);
let state = cx.global::<AppRegistry>();
div()
.relative()
.size_full()
.flex()
.flex_col()
// Main
.map(|this| {
if state.is_loading {
this
// Placeholder
.child(div())
.child(
div().flex_1().flex().items_center().justify_center().child(
svg()
.path("brand/coop.svg")
.size_12()
.text_color(cx.theme().base.step(cx, ColorScaleStep::THREE)),
),
)
} else if let Some(contact) = state.current_user(window, cx) {
this.child(
TitleBar::new()
// Left side
.child(div())
// Right side
.child(
div()
.flex()
.items_center()
.justify_end()
.gap_1()
.px_2()
.child(self.render_account(contact)),
),
)
.child(self.dock.clone())
// Listener
.on_action(cx.listener(Self::on_panel_action))
.on_action(cx.listener(Self::on_logout_action))
.on_action(cx.listener(Self::on_profile_action))
.on_action(cx.listener(Self::on_contacts_action))
.on_action(cx.listener(Self::on_settings_action))
} else {
this.child(TitleBar::new()).child(self.onboarding.clone())
}
})
// Notification
.child(
TitleBar::new()
// Left side
.child(div())
// Right side
.child(
div()
.flex()
.items_center()
.justify_end()
.gap_1()
.px_2()
.child(self.render_account()),
),
)
.child(self.dock.clone())
.child(div().absolute().top_8().children(notification_layer))
// Modal
.children(modal_layer)
.on_action(cx.listener(Self::on_panel_action))
.on_action(cx.listener(Self::on_logout_action))
.on_action(cx.listener(Self::on_profile_action))
.on_action(cx.listener(Self::on_contacts_action))
.on_action(cx.listener(Self::on_settings_action))
}
}

View File

@@ -1,8 +1,8 @@
use common::profile::NostrProfile;
use gpui::{
div, img, px, App, InteractiveElement, IntoElement, ParentElement, RenderOnce, SharedString,
Styled, Window,
};
use registry::contact::Contact;
use ui::{
theme::{scale::ColorScaleStep, ActiveTheme},
StyledExt,
@@ -10,7 +10,7 @@ use ui::{
#[derive(Clone, Debug, IntoElement)]
pub struct Message {
member: Contact,
member: NostrProfile,
content: SharedString,
ago: SharedString,
}
@@ -26,7 +26,7 @@ impl PartialEq for Message {
}
impl Message {
pub fn new(member: Contact, content: SharedString, ago: SharedString) -> Self {
pub fn new(member: NostrProfile, content: SharedString, ago: SharedString) -> Self {
Self {
member,
content,

View File

@@ -1,4 +1,5 @@
use async_utility::task::spawn;
use chat::room::Room;
use common::{
constants::IMAGE_SERVICE,
utils::{compare, message_time, nip96_upload},
@@ -12,7 +13,6 @@ use gpui::{
use itertools::Itertools;
use message::Message;
use nostr_sdk::prelude::*;
use registry::room::Room;
use smol::fs;
use state::get_client;
use tokio::sync::oneshot;
@@ -140,7 +140,7 @@ impl ChatPanel {
let members = room.members.clone();
let owner = room.owner.clone();
// Get all public keys
let all_keys = room.get_all_keys();
let all_keys = room.get_pubkeys();
// Async
let async_state = self.state.clone();

View File

@@ -1,6 +1,7 @@
mod chat;
mod onboarding;
mod sidebar;
mod welcome;
pub mod app;
pub mod onboarding;
pub mod startup;

View File

@@ -1,9 +1,6 @@
use common::constants::KEYRING_SERVICE;
use gpui::{
div, App, AppContext, Context, Entity, IntoElement, ParentElement, Render, Styled, Window,
};
use common::{constants::KEYRING_SERVICE, profile::NostrProfile};
use gpui::{div, AppContext, Context, Entity, IntoElement, ParentElement, Render, Styled, Window};
use nostr_sdk::prelude::*;
use registry::{app::AppRegistry, contact::Contact};
use state::get_client;
use ui::input::{InputEvent, TextInput};
@@ -71,9 +68,7 @@ impl Onboarding {
.await;
if let Ok(metadata) = query {
_ = async_cx.update_global::<AppRegistry, _>(|state, cx| {
state.set_user(Contact::new(public_key, metadata), cx);
});
//
}
}
}

View File

@@ -1,11 +1,15 @@
use common::utils::{random_name, room_hash};
use app_state::registry::AppRegistry;
use chat::room::Room;
use common::{
profile::NostrProfile,
utils::{random_name, room_hash},
};
use gpui::{
div, img, impl_internal_actions, px, uniform_list, App, AppContext, Context, Entity,
FocusHandle, InteractiveElement, IntoElement, ParentElement, Render, SharedString,
StatefulInteractiveElement, Styled, Window,
};
use nostr_sdk::prelude::*;
use registry::{app::AppRegistry, contact::Contact, room::Room};
use serde::Deserialize;
use state::get_client;
use std::{collections::HashSet, time::Duration};
@@ -27,7 +31,7 @@ pub struct Compose {
title_input: Entity<TextInput>,
message_input: Entity<TextInput>,
user_input: Entity<TextInput>,
contacts: Entity<Option<Vec<Contact>>>,
contacts: Entity<Option<Vec<NostrProfile>>>,
selected: Entity<HashSet<PublicKey>>,
focus_handle: FocusHandle,
is_loading: bool,
@@ -78,15 +82,17 @@ impl Compose {
let client = get_client();
async move {
let query: anyhow::Result<Vec<Contact>, anyhow::Error> = async_cx
let query: anyhow::Result<Vec<NostrProfile>, anyhow::Error> = async_cx
.background_executor()
.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<Contact> = profiles
let members: Vec<NostrProfile> = profiles
.into_iter()
.map(|profile| Contact::new(profile.public_key(), profile.metadata()))
.map(|profile| {
NostrProfile::new(profile.public_key(), profile.metadata())
})
.collect();
Ok(members)
@@ -120,10 +126,8 @@ impl Compose {
}
}
pub fn room(&self, window: &Window, cx: &App) -> Option<Room> {
let current_user = cx.global::<AppRegistry>().current_user(window, cx);
if let Some(current_user) = current_user {
pub fn room(&self, _window: &Window, cx: &App) -> Option<Room> {
if let Some(current_user) = cx.global::<AppRegistry>().user() {
// Convert selected pubkeys into nostr tags
let tags: Vec<Tag> = self
.selected
@@ -134,19 +138,19 @@ impl Compose {
let tags = Tags::new(tags);
// Convert selected pubkeys into members
let members: Vec<Contact> = self
let members: Vec<NostrProfile> = self
.selected
.read(cx)
.clone()
.into_iter()
.map(|pk| Contact::new(pk, Metadata::new()))
.map(|pk| NostrProfile::new(pk, Metadata::new()))
.collect();
// Get room's id
let id = room_hash(&tags);
// Get room's owner (current user)
let owner = Contact::new(current_user.public_key(), Metadata::new());
let owner = NostrProfile::new(current_user.public_key(), Metadata::new());
// Get room's title
let title = self.title_input.read(cx).text().to_string().into();
@@ -192,7 +196,7 @@ impl Compose {
_ = async_cx.update_entity(&view, |this, cx| {
this.contacts.update(cx, |this, cx| {
if let Some(members) = this {
members.insert(0, Contact::new(public_key, metadata));
members.insert(0, NostrProfile::new(public_key, metadata));
}
cx.notify();
});

View File

@@ -1,10 +1,11 @@
use crate::views::app::{AddPanel, PanelKind};
use chat::registry::ChatRegistry;
use common::utils::message_ago;
use gpui::{
div, img, percentage, prelude::FluentBuilder, px, Context, InteractiveElement, IntoElement,
ParentElement, Render, SharedString, StatefulInteractiveElement, Styled, Window,
};
use registry::chat::ChatRegistry;
use std::sync::Arc;
use ui::{
dock_area::dock::DockPlacement,
skeleton::Skeleton,

View File

@@ -1,11 +1,11 @@
use crate::views::sidebar::inbox::Inbox;
use chat::registry::ChatRegistry;
use compose::Compose;
use gpui::{
div, px, AnyElement, App, AppContext, BorrowAppContext, Context, Entity, EntityId,
EventEmitter, FocusHandle, Focusable, InteractiveElement, IntoElement, ParentElement, Render,
SharedString, StatefulInteractiveElement, Styled, Window,
};
use registry::chat::ChatRegistry;
use ui::{
button::{Button, ButtonRounded, ButtonVariants},
dock_area::panel::{Panel, PanelEvent},

View File

@@ -0,0 +1,26 @@
use gpui::{div, svg, Context, IntoElement, ParentElement, Render, Styled, Window};
use ui::theme::{scale::ColorScaleStep, ActiveTheme};
pub struct Startup {}
impl Startup {
pub fn new(_window: &mut Window, _cx: &mut Context<'_, Self>) -> Self {
Self {}
}
}
impl Render for Startup {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
div()
.size_full()
.flex()
.items_center()
.justify_center()
.child(
svg()
.path("brand/coop.svg")
.size_12()
.text_color(cx.theme().base.step(cx, ColorScaleStep::THREE)),
)
}
}