add group preview mode to browse panel
This commit is contained in:
@@ -9,7 +9,7 @@ use gpui::{
|
||||
size,
|
||||
};
|
||||
use gpui_base::{VirtualListScrollHandle, v_virtual_list};
|
||||
use nip29::{Discovery, GroupCandidate, GroupKey, GroupsEvent, GroupsRegistry};
|
||||
use nip29::{Discovery, GroupCandidate, GroupKey, GroupsEvent, GroupsRegistry, Membership};
|
||||
use nostr_sdk::prelude::{RelayUrl, Timestamp};
|
||||
use settings::AppSettings;
|
||||
use smallvec::{SmallVec, smallvec};
|
||||
@@ -20,7 +20,7 @@ use ui::dock::{DockArea, DockPlacement, Panel, PanelEvent, PanelHandle};
|
||||
use ui::input::{Input, InputEvent, InputState};
|
||||
use ui::notification::Notification;
|
||||
use ui::scroll::Scrollbar;
|
||||
use ui::{Disableable, Icon, IconName, Sizable, StyledExt, WindowExtension, h_flex, v_flex};
|
||||
use ui::{Icon, IconName, Sizable, StyledExt, WindowExtension, h_flex, v_flex};
|
||||
|
||||
const TITLE: &str = "Browse";
|
||||
const INPUT_PLACEHOLDER: &str = "wss://relay.example.com";
|
||||
@@ -223,17 +223,10 @@ impl BrowsePanel {
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Join a browsed group and open it.
|
||||
fn join(dock: &WeakEntity<DockArea>, key: GroupKey, window: &mut Window, cx: &mut App) {
|
||||
/// Open the group's panel to preview (or revisit) it.
|
||||
fn open_panel(dock: &WeakEntity<DockArea>, key: GroupKey, window: &mut Window, cx: &mut App) {
|
||||
let registry = GroupsRegistry::global(cx);
|
||||
|
||||
registry.update(cx, |registry, cx| {
|
||||
registry.join_key(key.clone(), None, cx);
|
||||
});
|
||||
|
||||
let Some(group) = registry.read(cx).group(&key, cx) else {
|
||||
return;
|
||||
};
|
||||
let group = registry.update(cx, |registry, cx| registry.open(key, cx));
|
||||
|
||||
ui::dock::add_panel_to(
|
||||
dock,
|
||||
@@ -244,19 +237,20 @@ impl BrowsePanel {
|
||||
);
|
||||
}
|
||||
|
||||
/// The discovered groups the user has not joined yet.
|
||||
/// The discovered groups the user is not a member of.
|
||||
fn discovered_cards(&self, cx: &App) -> Vec<GroupCandidate> {
|
||||
let joined: Vec<GroupKey> = GroupsRegistry::global(cx)
|
||||
let member_of: Vec<GroupKey> = GroupsRegistry::global(cx)
|
||||
.read(cx)
|
||||
.groups()
|
||||
.iter()
|
||||
.filter(|group| matches!(group.read(cx).membership(), Membership::Member))
|
||||
.map(|group| group.read(cx).key().clone())
|
||||
.collect();
|
||||
|
||||
self.discovery
|
||||
.discovered()
|
||||
.iter()
|
||||
.filter(|candidate| !joined.contains(&candidate.key))
|
||||
.filter(|candidate| !member_of.contains(&candidate.key))
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
@@ -357,6 +351,10 @@ impl BrowsePanel {
|
||||
}
|
||||
|
||||
fn render_card(&self, candidate: &GroupCandidate, cx: &mut Context<Self>) -> AnyElement {
|
||||
let registry = GroupsRegistry::global(cx);
|
||||
let hide_avatar = AppSettings::get_hide_avatar(cx);
|
||||
let dock = self.dock.clone();
|
||||
|
||||
let key = candidate.key.clone();
|
||||
let seed = key.cache_tag();
|
||||
let members = self.discovery.members(&key).unwrap_or(0);
|
||||
@@ -372,33 +370,19 @@ impl BrowsePanel {
|
||||
.display_about()
|
||||
.map(|about| SharedString::from(about.to_owned()));
|
||||
|
||||
let registry = GroupsRegistry::global(cx);
|
||||
let hide_avatar = AppSettings::get_hide_avatar(cx);
|
||||
let joined = registry.read(cx).group(&key, cx).is_some();
|
||||
let member = registry
|
||||
.read(cx)
|
||||
.group(&key, cx)
|
||||
.is_some_and(|group| matches!(group.read(cx).membership(), Membership::Member));
|
||||
|
||||
let action = if joined {
|
||||
Button::new(format!("joined-{seed}"))
|
||||
.label("Joined")
|
||||
.small()
|
||||
.ghost()
|
||||
.disabled(true)
|
||||
.into_any_element()
|
||||
} else {
|
||||
let panel = cx.entity().downgrade();
|
||||
let dock = self.dock.clone();
|
||||
let clicked = key;
|
||||
|
||||
Button::new(format!("join-{seed}"))
|
||||
.label("Join")
|
||||
.small()
|
||||
.ghost()
|
||||
.on_click(move |_event, window, cx| {
|
||||
Self::join(&dock, clicked.clone(), window, cx);
|
||||
|
||||
let _ = panel.update(cx, |_, cx| cx.notify());
|
||||
})
|
||||
.into_any_element()
|
||||
};
|
||||
let action = Button::new(format!("open-{seed}"))
|
||||
.label(if member { "Open" } else { "Preview" })
|
||||
.small()
|
||||
.ghost()
|
||||
.on_click(move |_event, window, cx| {
|
||||
Self::open_panel(&dock, key.clone(), window, cx);
|
||||
})
|
||||
.into_any_element();
|
||||
|
||||
v_flex()
|
||||
.flex_1()
|
||||
|
||||
Reference in New Issue
Block a user