clean up and add community invite
This commit is contained in:
@@ -3,14 +3,14 @@ use std::fmt;
|
||||
|
||||
use anyhow::Result;
|
||||
use community::{
|
||||
ChannelId, ChatMessage, Community, CommunityEvent, Epoch, Intent, LOAD_OLDER_PAGES,
|
||||
TIMELINE_PAGE, Timeline,
|
||||
ChannelId, ChatMessage, Community, CommunityEvent, CommunityRegistry, Epoch, Intent,
|
||||
LOAD_OLDER_PAGES, TIMELINE_PAGE, Timeline,
|
||||
};
|
||||
use gpui::prelude::FluentBuilder;
|
||||
use gpui::{
|
||||
AnyElement, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable, FollowMode,
|
||||
IntoElement, ListAlignment, ListScrollEvent, ListState, ParentElement, Render, SharedString,
|
||||
Styled, Subscription, Task, WeakEntity, Window, div, list, px,
|
||||
AnyElement, App, AppContext, ClipboardItem, Context, Entity, EventEmitter, FocusHandle,
|
||||
Focusable, FollowMode, IntoElement, ListAlignment, ListScrollEvent, ListState, ParentElement,
|
||||
Render, SharedString, Styled, Subscription, Task, WeakEntity, Window, div, list, px,
|
||||
};
|
||||
use nostr_sdk::prelude::EventId;
|
||||
use smallvec::{SmallVec, smallvec};
|
||||
@@ -112,11 +112,13 @@ pub struct CommunityPanel {
|
||||
/// Spawned reads and publishes, cancelled when the panel closes
|
||||
tasks: SmallVec<[Task<Result<()>>; 4]>,
|
||||
/// Event subscriptions
|
||||
_subscriptions: SmallVec<[Subscription; 2]>,
|
||||
_subscriptions: SmallVec<[Subscription; 3]>,
|
||||
}
|
||||
|
||||
impl CommunityPanel {
|
||||
pub fn new(community: Entity<Community>, window: &mut Window, cx: &mut Context<Self>) -> Self {
|
||||
let store = CommunityRegistry::global(cx);
|
||||
|
||||
let (id, name, channel) = {
|
||||
let community = community.read(cx);
|
||||
|
||||
@@ -166,6 +168,21 @@ impl CommunityPanel {
|
||||
},
|
||||
));
|
||||
|
||||
subscriptions.push(
|
||||
cx.subscribe_in(&store, window, |this, _, event, window, cx| {
|
||||
if let CommunityEvent::Invite { id, url } = event {
|
||||
let ours = this
|
||||
.community
|
||||
.read_with(cx, |community, _cx| community.id())
|
||||
.ok();
|
||||
if ours == Some(*id) {
|
||||
cx.write_to_clipboard(ClipboardItem::new_string(url.clone()));
|
||||
window.push_notification("Invite link copied", cx);
|
||||
}
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
cx.defer_in(window, |this, window, cx| {
|
||||
this.list_state.set_follow_mode(FollowMode::Tail);
|
||||
this.list_state.set_scroll_handler(cx.listener(
|
||||
@@ -772,7 +789,24 @@ impl Panel for CommunityPanel {
|
||||
}
|
||||
|
||||
fn toolbar_buttons(&self, _window: &Window, _cx: &App) -> Vec<Button> {
|
||||
vec![]
|
||||
let community = self.community.clone();
|
||||
|
||||
vec![
|
||||
Button::new("invite")
|
||||
.icon(IconName::Link)
|
||||
.tooltip("Copy invite link")
|
||||
.ghost()
|
||||
.on_click(move |_event, _window, cx: &mut App| {
|
||||
let Some(community) = community.upgrade() else {
|
||||
return;
|
||||
};
|
||||
|
||||
let state = CommunityRegistry::global(cx);
|
||||
let id = community.read(cx).id();
|
||||
|
||||
state.update(cx, |this, cx| this.invite(&id, cx));
|
||||
}),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user