wip: refactor
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -3,10 +3,6 @@
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
|
||||
7128
Cargo.lock
generated
Normal file
7128
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -9,6 +9,8 @@ name = "coop"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
coop-ui = { path = "../ui" }
|
||||
|
||||
gpui.workspace = true
|
||||
components.workspace = true
|
||||
reqwest_client.workspace = true
|
||||
|
||||
@@ -12,13 +12,13 @@ use std::{
|
||||
|
||||
use constants::{APP_NAME, FAKE_SIG};
|
||||
use states::account::AccountState;
|
||||
use ui::app::AppView;
|
||||
use views::app::AppView;
|
||||
|
||||
pub mod asset;
|
||||
pub mod constants;
|
||||
pub mod states;
|
||||
pub mod ui;
|
||||
pub mod utils;
|
||||
pub mod views;
|
||||
|
||||
actions!(main_menu, [Quit]);
|
||||
|
||||
@@ -138,6 +138,8 @@ async fn main() {
|
||||
|
||||
cx.open_window(opts, |cx| {
|
||||
let app_view = cx.new_view(AppView::new);
|
||||
|
||||
cx.activate(true);
|
||||
cx.new_view(|cx| Root::new(app_view.into(), cx))
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
pub mod account;
|
||||
pub mod room;
|
||||
|
||||
@@ -1,103 +0,0 @@
|
||||
use components::theme::ActiveTheme;
|
||||
use gpui::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
use prelude::FluentBuilder;
|
||||
|
||||
use crate::get_client;
|
||||
|
||||
#[derive(Clone)]
|
||||
#[allow(dead_code)]
|
||||
struct RoomLastMessage {
|
||||
content: Option<String>,
|
||||
time: Timestamp,
|
||||
}
|
||||
|
||||
#[derive(Clone, IntoElement)]
|
||||
pub struct Room {
|
||||
#[allow(dead_code)]
|
||||
public_key: PublicKey,
|
||||
metadata: Model<Option<Metadata>>,
|
||||
#[allow(dead_code)]
|
||||
last_message: RoomLastMessage,
|
||||
}
|
||||
|
||||
impl Room {
|
||||
pub fn new(event: Event, cx: &mut WindowContext) -> Self {
|
||||
let public_key = event.pubkey;
|
||||
|
||||
let last_message = RoomLastMessage {
|
||||
content: Some(event.content),
|
||||
time: event.created_at,
|
||||
};
|
||||
|
||||
let metadata = cx.new_model(|_| None);
|
||||
let async_metadata = metadata.clone();
|
||||
|
||||
let mut async_cx = cx.to_async();
|
||||
|
||||
cx.foreground_executor()
|
||||
.spawn(async move {
|
||||
let client = get_client();
|
||||
|
||||
let query = async_cx
|
||||
.background_executor()
|
||||
.spawn(async move { client.database().metadata(public_key).await })
|
||||
.await;
|
||||
|
||||
if let Ok(metadata) = query {
|
||||
_ = async_cx.update_model(&async_metadata, |a, b| {
|
||||
*a = metadata;
|
||||
b.notify();
|
||||
});
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
Self {
|
||||
public_key,
|
||||
metadata,
|
||||
last_message,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderOnce for Room {
|
||||
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
|
||||
let mut content = div();
|
||||
|
||||
if let Some(metadata) = self.metadata.read(cx).as_ref() {
|
||||
content = content
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap_2()
|
||||
.text_sm()
|
||||
.when_some(metadata.picture.clone(), |div, picture| {
|
||||
div.flex_shrink_0().child(
|
||||
img(picture)
|
||||
.size_6()
|
||||
.rounded_full()
|
||||
.object_fit(ObjectFit::Cover),
|
||||
)
|
||||
})
|
||||
.when_some(metadata.display_name.clone(), |div, display_name| {
|
||||
div.child(display_name)
|
||||
})
|
||||
} else {
|
||||
content = content
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap_2()
|
||||
.text_sm()
|
||||
.child(
|
||||
div()
|
||||
.flex_shrink_0()
|
||||
.size_6()
|
||||
.rounded_full()
|
||||
.bg(cx.theme().muted),
|
||||
)
|
||||
.child("Anon")
|
||||
}
|
||||
|
||||
div().child(content)
|
||||
}
|
||||
}
|
||||
@@ -1,157 +0,0 @@
|
||||
use components::{indicator::Indicator, Sizable};
|
||||
use gpui::*;
|
||||
use itertools::Itertools;
|
||||
use nostr_sdk::prelude::*;
|
||||
use std::{cmp::Reverse, time::Duration};
|
||||
|
||||
use super::Block;
|
||||
use crate::{
|
||||
get_client,
|
||||
states::{account::AccountState, room::Room},
|
||||
};
|
||||
|
||||
struct RoomList {
|
||||
rooms: Vec<Room>,
|
||||
}
|
||||
|
||||
impl RoomList {
|
||||
pub fn new(raw_events: Vec<Event>, cx: &mut ViewContext<'_, Self>) -> Self {
|
||||
let rooms: Vec<Room> = raw_events
|
||||
.into_iter()
|
||||
.map(|event| Room::new(event, cx))
|
||||
.collect();
|
||||
|
||||
Self { rooms }
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for RoomList {
|
||||
fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
div().flex().flex_col().gap_1().children(self.rooms.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Rooms {
|
||||
rooms: Model<Option<View<RoomList>>>,
|
||||
focus_handle: FocusHandle,
|
||||
}
|
||||
|
||||
impl Rooms {
|
||||
pub fn view(cx: &mut WindowContext) -> View<Self> {
|
||||
cx.new_view(Self::new)
|
||||
}
|
||||
|
||||
fn new(cx: &mut ViewContext<Self>) -> Self {
|
||||
let rooms = cx.new_model(|_| None);
|
||||
let async_rooms = rooms.clone();
|
||||
|
||||
if let Some(public_key) = cx.global::<AccountState>().in_use {
|
||||
let client = get_client();
|
||||
let filter = Filter::new()
|
||||
.kind(Kind::PrivateDirectMessage)
|
||||
.pubkey(public_key);
|
||||
|
||||
let mut async_cx = cx.to_async();
|
||||
|
||||
cx.foreground_executor()
|
||||
.spawn(async move {
|
||||
let events = async_cx
|
||||
.background_executor()
|
||||
.spawn(async move {
|
||||
if let Ok(events) = client.database().query(vec![filter]).await {
|
||||
events
|
||||
.into_iter()
|
||||
.sorted_by_key(|ev| Reverse(ev.created_at))
|
||||
.filter(|ev| ev.pubkey != public_key)
|
||||
.unique_by(|ev| ev.pubkey)
|
||||
.collect::<Vec<_>>()
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
})
|
||||
.await;
|
||||
|
||||
// Get all public keys
|
||||
let public_keys: Vec<PublicKey> =
|
||||
events.iter().map(|event| event.pubkey).collect();
|
||||
|
||||
// Calculate total public keys
|
||||
let total = public_keys.len();
|
||||
|
||||
// Create subscription for metadata events
|
||||
let filter = Filter::new()
|
||||
.kind(Kind::Metadata)
|
||||
.authors(public_keys)
|
||||
.limit(total);
|
||||
|
||||
let opts = SubscribeAutoCloseOptions::default()
|
||||
.filter(FilterOptions::WaitDurationAfterEOSE(Duration::from_secs(2)));
|
||||
|
||||
async_cx
|
||||
.background_executor()
|
||||
.spawn(async move {
|
||||
if let Err(e) = client.subscribe(vec![filter], Some(opts)).await {
|
||||
println!("Error: {}", e);
|
||||
}
|
||||
})
|
||||
.await;
|
||||
|
||||
let view = async_cx.new_view(|cx| RoomList::new(events, cx)).unwrap();
|
||||
|
||||
_ = async_cx.update_model(&async_rooms, |a, b| {
|
||||
*a = Some(view);
|
||||
b.notify();
|
||||
});
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
Self {
|
||||
rooms,
|
||||
focus_handle: cx.focus_handle(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Block for Rooms {
|
||||
fn title() -> &'static str {
|
||||
"Rooms"
|
||||
}
|
||||
|
||||
fn new_view(cx: &mut WindowContext) -> View<impl FocusableView> {
|
||||
Self::view(cx)
|
||||
}
|
||||
|
||||
fn zoomable() -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl FocusableView for Rooms {
|
||||
fn focus_handle(&self, _: &gpui::AppContext) -> gpui::FocusHandle {
|
||||
self.focus_handle.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for Rooms {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
let mut content = div();
|
||||
|
||||
if let Some(room_list) = self.rooms.read(cx).as_ref() {
|
||||
content = content
|
||||
.flex()
|
||||
.flex_col()
|
||||
.gap_1()
|
||||
.px_2()
|
||||
.child(room_list.clone());
|
||||
} else {
|
||||
content = content
|
||||
.w_full()
|
||||
.flex()
|
||||
.justify_center()
|
||||
.child(Indicator::new().small())
|
||||
}
|
||||
|
||||
div().child(content)
|
||||
}
|
||||
}
|
||||
@@ -15,12 +15,31 @@ pub fn get_all_accounts_from_keyring() -> Vec<PublicKey> {
|
||||
accounts
|
||||
}
|
||||
|
||||
pub fn show_npub(public_key: PublicKey, len: usize) -> anyhow::Result<String, anyhow::Error> {
|
||||
let bech32 = public_key.to_bech32()?;
|
||||
let separator = " ... ";
|
||||
|
||||
let sep_len = separator.len();
|
||||
let chars_to_show = len - sep_len;
|
||||
let front_chars = (chars_to_show + 1) / 2; // ceil
|
||||
let back_chars = chars_to_show / 2; // floor
|
||||
|
||||
Ok(format!(
|
||||
"{}{}{}",
|
||||
&bech32[..front_chars],
|
||||
separator,
|
||||
&bech32[bech32.len() - back_chars..]
|
||||
))
|
||||
}
|
||||
|
||||
pub fn ago(time: u64) -> String {
|
||||
let now = Local::now();
|
||||
let input_time = Local.timestamp_opt(time as i64, 0).unwrap();
|
||||
let diff = (now - input_time).num_hours();
|
||||
|
||||
if diff < 24 {
|
||||
if diff == 0 {
|
||||
"now".to_owned()
|
||||
} else if diff < 24 {
|
||||
let duration = now.signed_duration_since(input_time);
|
||||
format!("{} ago", duration.num_hours())
|
||||
} else {
|
||||
|
||||
@@ -1,18 +1,27 @@
|
||||
use components::{
|
||||
dock::{DockArea, DockItem},
|
||||
indicator::Indicator,
|
||||
dock::{DockArea, DockItem, DockPlacement, PanelStyle},
|
||||
theme::{ActiveTheme, Theme},
|
||||
Root, Sizable, TitleBar,
|
||||
Root, TitleBar,
|
||||
};
|
||||
use coop_ui::block::BlockContainer;
|
||||
use gpui::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
use serde::Deserialize;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::states::account::AccountState;
|
||||
|
||||
use super::{
|
||||
block::{rooms::Rooms, welcome::WelcomeBlock, BlockContainer},
|
||||
dock::{left_dock::LeftDock, welcome::WelcomeBlock},
|
||||
onboarding::Onboarding,
|
||||
};
|
||||
use crate::states::account::AccountState;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Deserialize)]
|
||||
pub struct AddPanel {
|
||||
pub title: Option<String>,
|
||||
pub receiver: PublicKey,
|
||||
}
|
||||
|
||||
impl_actions!(dock, [AddPanel]);
|
||||
|
||||
pub struct DockAreaTab {
|
||||
id: &'static str,
|
||||
@@ -26,7 +35,7 @@ pub const DOCK_AREA: DockAreaTab = DockAreaTab {
|
||||
|
||||
pub struct AppView {
|
||||
onboarding: View<Onboarding>,
|
||||
dock: Model<Option<View<DockArea>>>,
|
||||
dock: View<DockArea>,
|
||||
}
|
||||
|
||||
impl AppView {
|
||||
@@ -41,25 +50,14 @@ impl AppView {
|
||||
let onboarding = cx.new_view(Onboarding::new);
|
||||
|
||||
// Dock
|
||||
let dock = cx.new_model(|_| None);
|
||||
let async_dock = dock.clone();
|
||||
|
||||
// Observe UserState
|
||||
// If current user is present, fetching all gift wrap events
|
||||
cx.observe_global::<AccountState>(move |_, cx| {
|
||||
if cx.global::<AccountState>().in_use.is_some() {
|
||||
// Setup dock area
|
||||
let dock_area =
|
||||
cx.new_view(|cx| DockArea::new(DOCK_AREA.id, Some(DOCK_AREA.version), cx));
|
||||
|
||||
// Setup dock layout
|
||||
Self::init_layout(dock_area.downgrade(), cx);
|
||||
|
||||
// Update dock model
|
||||
cx.update_model(&async_dock, |a, b| {
|
||||
*a = Some(dock_area);
|
||||
b.notify();
|
||||
let dock = cx.new_view(|cx| {
|
||||
DockArea::new(DOCK_AREA.id, Some(DOCK_AREA.version), cx).panel_style(PanelStyle::TabBar)
|
||||
});
|
||||
|
||||
cx.observe_global::<AccountState>(|view, cx| {
|
||||
// TODO: save dock state and load previous state on startup
|
||||
if cx.global::<AccountState>().in_use.is_some() {
|
||||
Self::init_layout(view.dock.downgrade(), cx);
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
@@ -68,25 +66,13 @@ impl AppView {
|
||||
}
|
||||
|
||||
fn init_layout(dock_area: WeakView<DockArea>, cx: &mut WindowContext) {
|
||||
let dock_item = Self::init_dock_items(&dock_area, cx);
|
||||
|
||||
let left_panels = DockItem::split_with_sizes(
|
||||
Axis::Vertical,
|
||||
vec![DockItem::tabs(
|
||||
vec![Arc::new(BlockContainer::panel::<Rooms>(cx))],
|
||||
None,
|
||||
&dock_area,
|
||||
cx,
|
||||
)],
|
||||
vec![None, None],
|
||||
&dock_area,
|
||||
cx,
|
||||
);
|
||||
let left = DockItem::panel(Arc::new(BlockContainer::panel::<LeftDock>(cx)));
|
||||
let center = Self::init_dock_items(&dock_area, cx);
|
||||
|
||||
_ = dock_area.update(cx, |view, cx| {
|
||||
view.set_version(DOCK_AREA.version, cx);
|
||||
view.set_left_dock(left_panels, Some(px(260.)), true, cx);
|
||||
view.set_center(dock_item, cx);
|
||||
view.set_left_dock(left, Some(px(260.)), true, cx);
|
||||
view.set_center(center, cx);
|
||||
view.set_dock_collapsible(
|
||||
Edges {
|
||||
left: false,
|
||||
@@ -116,6 +102,15 @@ impl AppView {
|
||||
cx,
|
||||
)
|
||||
}
|
||||
|
||||
fn on_action_add_panel(&mut self, _action: &AddPanel, cx: &mut ViewContext<Self>) {
|
||||
// TODO: add chat panel
|
||||
let panel = Arc::new(BlockContainer::panel::<WelcomeBlock>(cx));
|
||||
|
||||
self.dock.update(cx, |dock_area, cx| {
|
||||
dock_area.add_panel(panel, DockPlacement::Center, cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for AppView {
|
||||
@@ -128,22 +123,13 @@ impl Render for AppView {
|
||||
if cx.global::<AccountState>().in_use.is_none() {
|
||||
content = content.size_full().child(self.onboarding.clone())
|
||||
} else {
|
||||
#[allow(clippy::collapsible_else_if)]
|
||||
if let Some(dock) = self.dock.read(cx).as_ref() {
|
||||
content = content
|
||||
.on_action(cx.listener(Self::on_action_add_panel))
|
||||
.size_full()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.child(TitleBar::new())
|
||||
.child(dock.clone())
|
||||
} else {
|
||||
content = content
|
||||
.size_full()
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.child(Indicator::new().small())
|
||||
}
|
||||
.child(self.dock.clone())
|
||||
}
|
||||
|
||||
div()
|
||||
49
crates/app/src/views/dock/chat.rs
Normal file
49
crates/app/src/views/dock/chat.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
use coop_ui::block::Block;
|
||||
use gpui::*;
|
||||
|
||||
pub struct ChatBlock {
|
||||
focus_handle: FocusHandle,
|
||||
}
|
||||
|
||||
impl ChatBlock {
|
||||
pub fn view(cx: &mut WindowContext) -> View<Self> {
|
||||
cx.new_view(Self::new)
|
||||
}
|
||||
|
||||
fn new(cx: &mut ViewContext<Self>) -> Self {
|
||||
Self {
|
||||
focus_handle: cx.focus_handle(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Block for ChatBlock {
|
||||
fn title() -> &'static str {
|
||||
"Chat"
|
||||
}
|
||||
|
||||
fn new_view(cx: &mut WindowContext) -> View<impl FocusableView> {
|
||||
Self::view(cx)
|
||||
}
|
||||
|
||||
fn zoomable() -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl FocusableView for ChatBlock {
|
||||
fn focus_handle(&self, _: &gpui::AppContext) -> gpui::FocusHandle {
|
||||
self.focus_handle.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for ChatBlock {
|
||||
fn render(&mut self, _cx: &mut gpui::ViewContext<Self>) -> impl IntoElement {
|
||||
div()
|
||||
.size_full()
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.child("Test")
|
||||
}
|
||||
}
|
||||
164
crates/app/src/views/dock/inbox/chat.rs
Normal file
164
crates/app/src/views/dock/inbox/chat.rs
Normal file
@@ -0,0 +1,164 @@
|
||||
use components::{theme::ActiveTheme, Collapsible, Selectable, StyledExt};
|
||||
use gpui::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
use prelude::FluentBuilder;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::{
|
||||
utils::{ago, show_npub},
|
||||
views::app::AddPanel,
|
||||
};
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Deserialize)]
|
||||
pub struct ChatDelegate {
|
||||
title: Option<String>,
|
||||
public_key: PublicKey,
|
||||
metadata: Option<Metadata>,
|
||||
last_seen: Timestamp,
|
||||
}
|
||||
|
||||
impl ChatDelegate {
|
||||
pub fn new(
|
||||
title: Option<String>,
|
||||
public_key: PublicKey,
|
||||
metadata: Option<Metadata>,
|
||||
last_seen: Timestamp,
|
||||
) -> Self {
|
||||
Self {
|
||||
title,
|
||||
public_key,
|
||||
metadata,
|
||||
last_seen,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(IntoElement)]
|
||||
pub struct Chat {
|
||||
id: ElementId,
|
||||
pub item: ChatDelegate,
|
||||
// Interactive
|
||||
base: Div,
|
||||
selected: bool,
|
||||
is_collapsed: bool,
|
||||
}
|
||||
|
||||
impl Chat {
|
||||
pub fn new(item: ChatDelegate) -> Self {
|
||||
let id = SharedString::from(item.public_key.to_hex()).into();
|
||||
|
||||
Self {
|
||||
id,
|
||||
item,
|
||||
base: div(),
|
||||
selected: false,
|
||||
is_collapsed: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Selectable for Chat {
|
||||
fn selected(mut self, selected: bool) -> Self {
|
||||
self.selected = selected;
|
||||
self
|
||||
}
|
||||
|
||||
fn element_id(&self) -> &gpui::ElementId {
|
||||
&self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl Collapsible for Chat {
|
||||
fn is_collapsed(&self) -> bool {
|
||||
self.is_collapsed
|
||||
}
|
||||
|
||||
fn collapsed(mut self, collapsed: bool) -> Self {
|
||||
self.is_collapsed = collapsed;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl InteractiveElement for Chat {
|
||||
fn interactivity(&mut self) -> &mut gpui::Interactivity {
|
||||
self.base.interactivity()
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderOnce for Chat {
|
||||
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
|
||||
let ago = ago(self.item.last_seen.as_u64());
|
||||
|
||||
let mut content = div()
|
||||
.font_medium()
|
||||
.text_color(cx.theme().sidebar_accent_foreground);
|
||||
|
||||
if let Some(metadata) = self.item.metadata.clone() {
|
||||
content = content
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap_2()
|
||||
.map(|this| {
|
||||
if let Some(picture) = metadata.picture {
|
||||
this.flex_shrink_0().child(
|
||||
img(picture)
|
||||
.size_6()
|
||||
.rounded_full()
|
||||
.object_fit(ObjectFit::Cover),
|
||||
)
|
||||
} else {
|
||||
this.flex_shrink_0()
|
||||
.child(div().size_6().rounded_full().bg(cx.theme().muted))
|
||||
}
|
||||
})
|
||||
.map(|this| {
|
||||
if let Some(display_name) = metadata.display_name {
|
||||
this.child(display_name)
|
||||
} else if let Ok(npub) = show_npub(self.item.public_key, 16) {
|
||||
this.child(npub)
|
||||
} else {
|
||||
this.child("Anon")
|
||||
}
|
||||
})
|
||||
} else {
|
||||
content = content
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap_2()
|
||||
.child(
|
||||
div()
|
||||
.flex_shrink_0()
|
||||
.size_6()
|
||||
.rounded_full()
|
||||
.bg(cx.theme().muted),
|
||||
)
|
||||
.child("Anon")
|
||||
}
|
||||
|
||||
self.base
|
||||
.id(self.id)
|
||||
.h_8()
|
||||
.px_1()
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_between()
|
||||
.text_xs()
|
||||
.rounded_md()
|
||||
.hover(|this| {
|
||||
this.bg(cx.theme().sidebar_accent)
|
||||
.text_color(cx.theme().sidebar_accent_foreground)
|
||||
})
|
||||
.child(content)
|
||||
.child(
|
||||
div()
|
||||
.child(ago)
|
||||
.text_color(cx.theme().sidebar_accent_foreground.opacity(0.7)),
|
||||
)
|
||||
.on_click(move |_, cx| {
|
||||
cx.dispatch_action(Box::new(AddPanel {
|
||||
title: self.item.title.clone(),
|
||||
receiver: self.item.public_key,
|
||||
}))
|
||||
})
|
||||
}
|
||||
}
|
||||
128
crates/app/src/views/dock/inbox/mod.rs
Normal file
128
crates/app/src/views/dock/inbox/mod.rs
Normal file
@@ -0,0 +1,128 @@
|
||||
use chat::{Chat, ChatDelegate};
|
||||
use components::{theme::ActiveTheme, v_flex, StyledExt};
|
||||
use gpui::*;
|
||||
use itertools::Itertools;
|
||||
use nostr_sdk::prelude::*;
|
||||
use std::{cmp::Reverse, time::Duration};
|
||||
|
||||
use crate::{get_client, states::account::AccountState};
|
||||
|
||||
pub mod chat;
|
||||
|
||||
pub struct Inbox {
|
||||
label: SharedString,
|
||||
chats: Model<Option<Vec<ChatDelegate>>>,
|
||||
}
|
||||
|
||||
impl Inbox {
|
||||
pub fn new(cx: &mut ViewContext<'_, Self>) -> Self {
|
||||
let chats = cx.new_model(|_| None);
|
||||
let async_chats = chats.clone();
|
||||
|
||||
if let Some(public_key) = cx.global::<AccountState>().in_use {
|
||||
let client = get_client();
|
||||
let filter = Filter::new()
|
||||
.kind(Kind::PrivateDirectMessage)
|
||||
.pubkey(public_key);
|
||||
|
||||
let mut async_cx = cx.to_async();
|
||||
|
||||
cx.foreground_executor()
|
||||
.spawn(async move {
|
||||
let events = async_cx
|
||||
.background_executor()
|
||||
.spawn(async move {
|
||||
if let Ok(events) = client.database().query(vec![filter]).await {
|
||||
events
|
||||
.into_iter()
|
||||
.sorted_by_key(|ev| Reverse(ev.created_at))
|
||||
.filter(|ev| ev.pubkey != public_key)
|
||||
.unique_by(|ev| ev.pubkey)
|
||||
.collect::<Vec<_>>()
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
})
|
||||
.await;
|
||||
|
||||
// Get all public keys
|
||||
let public_keys: Vec<PublicKey> =
|
||||
events.iter().map(|event| event.pubkey).collect();
|
||||
|
||||
// Calculate total public keys
|
||||
let total = public_keys.len();
|
||||
|
||||
// Create subscription for metadata events
|
||||
let filter = Filter::new()
|
||||
.kind(Kind::Metadata)
|
||||
.authors(public_keys)
|
||||
.limit(total);
|
||||
|
||||
let mut chats = Vec::new();
|
||||
let mut stream = async_cx
|
||||
.background_executor()
|
||||
.spawn(async move {
|
||||
client
|
||||
.stream_events(vec![filter], Some(Duration::from_secs(15)))
|
||||
.await
|
||||
.unwrap()
|
||||
})
|
||||
.await;
|
||||
|
||||
while let Some(event) = stream.next().await {
|
||||
// TODO: generate some random name?
|
||||
let title = if let Some(tag) = event.tags.find(TagKind::Title) {
|
||||
tag.content().map(|s| s.to_string())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let metadata = Metadata::from_json(event.content).ok();
|
||||
let chat =
|
||||
ChatDelegate::new(title, event.pubkey, metadata, event.created_at);
|
||||
|
||||
chats.push(chat);
|
||||
}
|
||||
|
||||
_ = async_cx.update_model(&async_chats, |a, b| {
|
||||
*a = Some(chats);
|
||||
b.notify();
|
||||
});
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
Self {
|
||||
label: "Inbox".into(),
|
||||
chats,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for Inbox {
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
let mut content = div();
|
||||
|
||||
if let Some(chats) = self.chats.read(cx).as_ref() {
|
||||
content = content.children(chats.iter().map(move |item| Chat::new(item.clone())))
|
||||
}
|
||||
|
||||
v_flex()
|
||||
.pt_3()
|
||||
.px_2()
|
||||
.gap_2()
|
||||
.child(
|
||||
div()
|
||||
.id("inbox")
|
||||
.h_7()
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap_2()
|
||||
.text_xs()
|
||||
.font_semibold()
|
||||
.text_color(cx.theme().sidebar_foreground.opacity(0.7))
|
||||
.child(self.label.clone()),
|
||||
)
|
||||
.child(content)
|
||||
}
|
||||
}
|
||||
55
crates/app/src/views/dock/left_dock.rs
Normal file
55
crates/app/src/views/dock/left_dock.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
use components::{scroll::ScrollbarAxis, StyledExt};
|
||||
use coop_ui::block::Block;
|
||||
use gpui::*;
|
||||
|
||||
use super::inbox::Inbox;
|
||||
|
||||
pub struct LeftDock {
|
||||
inbox: View<Inbox>,
|
||||
focus_handle: FocusHandle,
|
||||
view_id: EntityId,
|
||||
}
|
||||
|
||||
impl LeftDock {
|
||||
pub fn view(cx: &mut WindowContext) -> View<Self> {
|
||||
cx.new_view(Self::new)
|
||||
}
|
||||
|
||||
fn new(cx: &mut ViewContext<Self>) -> Self {
|
||||
let inbox = cx.new_view(Inbox::new);
|
||||
|
||||
Self {
|
||||
inbox,
|
||||
focus_handle: cx.focus_handle(),
|
||||
view_id: cx.view().entity_id(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Block for LeftDock {
|
||||
fn title() -> &'static str {
|
||||
"Left Dock"
|
||||
}
|
||||
|
||||
fn new_view(cx: &mut WindowContext) -> View<impl FocusableView> {
|
||||
Self::view(cx)
|
||||
}
|
||||
|
||||
fn zoomable() -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl FocusableView for LeftDock {
|
||||
fn focus_handle(&self, _: &gpui::AppContext) -> gpui::FocusHandle {
|
||||
self.focus_handle.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for LeftDock {
|
||||
fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
div()
|
||||
.child(self.inbox.clone())
|
||||
.scrollable(self.view_id, ScrollbarAxis::Vertical)
|
||||
}
|
||||
}
|
||||
5
crates/app/src/views/dock/mod.rs
Normal file
5
crates/app/src/views/dock/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
pub mod chat;
|
||||
pub mod left_dock;
|
||||
pub mod welcome;
|
||||
|
||||
pub mod inbox;
|
||||
@@ -2,10 +2,9 @@ use components::{
|
||||
theme::{ActiveTheme, Colorize},
|
||||
StyledExt,
|
||||
};
|
||||
use coop_ui::block::Block;
|
||||
use gpui::*;
|
||||
|
||||
use super::Block;
|
||||
|
||||
pub struct WelcomeBlock {
|
||||
focus_handle: FocusHandle,
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
pub mod app;
|
||||
pub mod block;
|
||||
pub mod dock;
|
||||
pub mod onboarding;
|
||||
9
crates/ui/Cargo.toml
Normal file
9
crates/ui/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "coop-ui"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
gpui.workspace = true
|
||||
components.workspace = true
|
||||
@@ -9,9 +9,6 @@ use components::{
|
||||
use gpui::*;
|
||||
use prelude::FluentBuilder;
|
||||
|
||||
pub mod rooms;
|
||||
pub mod welcome;
|
||||
|
||||
actions!(block, [PanelInfo]);
|
||||
|
||||
pub fn section(title: impl IntoElement, cx: &WindowContext) -> Div {
|
||||
1
crates/ui/src/lib.rs
Normal file
1
crates/ui/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod block;
|
||||
Reference in New Issue
Block a user