feat: add contacts panel
This commit is contained in:
@@ -1,11 +1,20 @@
|
|||||||
|
use common::profile::NostrProfile;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
div, AnyElement, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable,
|
div, img, px, uniform_list, AnyElement, App, AppContext, Context, Entity, EventEmitter,
|
||||||
IntoElement, ParentElement, Render, SharedString, Styled, Window,
|
FocusHandle, Focusable, InteractiveElement, IntoElement, ParentElement, Render, SharedString,
|
||||||
|
Styled, Window,
|
||||||
};
|
};
|
||||||
|
use nostr_sdk::prelude::*;
|
||||||
|
use state::get_client;
|
||||||
|
use tokio::sync::oneshot;
|
||||||
use ui::{
|
use ui::{
|
||||||
button::Button,
|
button::Button,
|
||||||
dock_area::panel::{Panel, PanelEvent},
|
dock_area::panel::{Panel, PanelEvent},
|
||||||
|
indicator::Indicator,
|
||||||
popup_menu::PopupMenu,
|
popup_menu::PopupMenu,
|
||||||
|
prelude::FluentBuilder,
|
||||||
|
theme::{scale::ColorScaleStep, ActiveTheme},
|
||||||
|
Sizable,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn init(window: &mut Window, cx: &mut App) -> Entity<Contacts> {
|
pub fn init(window: &mut Window, cx: &mut App) -> Entity<Contacts> {
|
||||||
@@ -13,6 +22,8 @@ pub fn init(window: &mut Window, cx: &mut App) -> Entity<Contacts> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct Contacts {
|
pub struct Contacts {
|
||||||
|
contacts: Entity<Option<Vec<NostrProfile>>>,
|
||||||
|
// Panel
|
||||||
name: SharedString,
|
name: SharedString,
|
||||||
closable: bool,
|
closable: bool,
|
||||||
zoomable: bool,
|
zoomable: bool,
|
||||||
@@ -21,7 +32,42 @@ pub struct Contacts {
|
|||||||
|
|
||||||
impl Contacts {
|
impl Contacts {
|
||||||
pub fn new(_window: &mut Window, cx: &mut App) -> Entity<Self> {
|
pub fn new(_window: &mut Window, cx: &mut App) -> Entity<Self> {
|
||||||
|
let contacts = cx.new(|_| None);
|
||||||
|
let async_contact = contacts.clone();
|
||||||
|
|
||||||
|
cx.spawn(|mut cx| async move {
|
||||||
|
let client = get_client();
|
||||||
|
let (tx, rx) = oneshot::channel::<Vec<NostrProfile>>();
|
||||||
|
|
||||||
|
cx.background_executor()
|
||||||
|
.spawn(async move {
|
||||||
|
let signer = client.signer().await.unwrap();
|
||||||
|
let public_key = signer.get_public_key().await.unwrap();
|
||||||
|
|
||||||
|
if let Ok(profiles) = client.database().contacts(public_key).await {
|
||||||
|
let members: Vec<NostrProfile> = profiles
|
||||||
|
.into_iter()
|
||||||
|
.map(|profile| {
|
||||||
|
NostrProfile::new(profile.public_key(), profile.metadata())
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
_ = tx.send(members);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
|
if let Ok(contacts) = rx.await {
|
||||||
|
_ = cx.update_entity(&async_contact, |this, cx| {
|
||||||
|
*this = Some(contacts);
|
||||||
|
cx.notify();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
cx.new(|cx| Self {
|
cx.new(|cx| Self {
|
||||||
|
contacts,
|
||||||
name: "Contacts".into(),
|
name: "Contacts".into(),
|
||||||
closable: true,
|
closable: true,
|
||||||
zoomable: true,
|
zoomable: true,
|
||||||
@@ -65,12 +111,60 @@ impl Focusable for Contacts {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Render for Contacts {
|
impl Render for Contacts {
|
||||||
fn render(&mut self, _window: &mut gpui::Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
fn render(&mut self, _window: &mut gpui::Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||||
div()
|
div().size_full().pt_2().px_2().map(|this| {
|
||||||
.size_full()
|
if let Some(contacts) = self.contacts.read(cx).clone() {
|
||||||
.flex()
|
this.child(
|
||||||
.items_center()
|
uniform_list(
|
||||||
.justify_center()
|
cx.entity().clone(),
|
||||||
.child("Contacts")
|
"contacts",
|
||||||
|
contacts.len(),
|
||||||
|
move |_, range, _window, cx| {
|
||||||
|
let mut items = Vec::new();
|
||||||
|
|
||||||
|
for ix in range {
|
||||||
|
let item = contacts.get(ix).unwrap().clone();
|
||||||
|
|
||||||
|
items.push(
|
||||||
|
div()
|
||||||
|
.w_full()
|
||||||
|
.h_9()
|
||||||
|
.px_2()
|
||||||
|
.flex()
|
||||||
|
.items_center()
|
||||||
|
.justify_between()
|
||||||
|
.rounded(px(cx.theme().radius))
|
||||||
|
.child(
|
||||||
|
div()
|
||||||
|
.flex()
|
||||||
|
.items_center()
|
||||||
|
.gap_2()
|
||||||
|
.text_xs()
|
||||||
|
.child(
|
||||||
|
div()
|
||||||
|
.flex_shrink_0()
|
||||||
|
.child(img(item.avatar()).size_6()),
|
||||||
|
)
|
||||||
|
.child(item.name()),
|
||||||
|
)
|
||||||
|
.hover(|this| {
|
||||||
|
this.bg(cx.theme().base.step(cx, ColorScaleStep::THREE))
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
items
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.h_full(),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
this.flex()
|
||||||
|
.items_center()
|
||||||
|
.justify_center()
|
||||||
|
.h_16()
|
||||||
|
.child(Indicator::new().small())
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ use nostr_sdk::prelude::*;
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use state::get_client;
|
use state::get_client;
|
||||||
use std::{collections::HashSet, time::Duration};
|
use std::{collections::HashSet, time::Duration};
|
||||||
|
use tokio::sync::oneshot;
|
||||||
use ui::{
|
use ui::{
|
||||||
button::{Button, ButtonRounded},
|
button::{Button, ButtonRounded},
|
||||||
indicator::Indicator,
|
indicator::Indicator,
|
||||||
@@ -78,31 +79,35 @@ impl Compose {
|
|||||||
)
|
)
|
||||||
.detach();
|
.detach();
|
||||||
|
|
||||||
cx.spawn(|this, mut async_cx| async move {
|
cx.spawn(|this, mut cx| async move {
|
||||||
let query: anyhow::Result<Vec<NostrProfile>, anyhow::Error> = async_cx
|
let client = get_client();
|
||||||
.background_executor()
|
let (tx, rx) = oneshot::channel::<Vec<NostrProfile>>();
|
||||||
|
|
||||||
|
cx.background_executor()
|
||||||
.spawn(async move {
|
.spawn(async move {
|
||||||
let client = get_client();
|
let signer = client.signer().await.unwrap();
|
||||||
let signer = client.signer().await?;
|
let public_key = signer.get_public_key().await.unwrap();
|
||||||
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()))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
Ok(members)
|
if let Ok(profiles) = client.database().contacts(public_key).await {
|
||||||
|
let members: Vec<NostrProfile> = profiles
|
||||||
|
.into_iter()
|
||||||
|
.map(|profile| {
|
||||||
|
NostrProfile::new(profile.public_key(), profile.metadata())
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
_ = tx.send(members);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.await;
|
.detach();
|
||||||
|
|
||||||
if let Ok(contacts) = query {
|
if let Ok(contacts) = rx.await {
|
||||||
if let Some(view) = this.upgrade() {
|
if let Some(view) = this.upgrade() {
|
||||||
_ = async_cx.update_entity(&view, |this, cx| {
|
_ = cx.update_entity(&view, |this, cx| {
|
||||||
this.contacts.update(cx, |this, cx| {
|
this.contacts.update(cx, |this, cx| {
|
||||||
*this = Some(contacts);
|
*this = Some(contacts);
|
||||||
cx.notify();
|
cx.notify();
|
||||||
});
|
});
|
||||||
|
|
||||||
cx.notify();
|
cx.notify();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user