add group preview mode to browse panel
This commit is contained in:
+47
-6
@@ -65,6 +65,7 @@ impl EventEmitter<GroupsEvent> for GroupsRegistry {}
|
||||
|
||||
pub struct GroupsRegistry {
|
||||
groups: Vec<Entity<Group>>,
|
||||
previews: HashMap<GroupKey, Entity<Group>>,
|
||||
list: GroupList,
|
||||
browse: HashMap<SubscriptionId, RelayUrl>,
|
||||
routes: HashMap<SubscriptionId, GroupKey>,
|
||||
@@ -120,6 +121,7 @@ impl GroupsRegistry {
|
||||
|
||||
Self {
|
||||
groups: Vec::new(),
|
||||
previews: HashMap::new(),
|
||||
list: GroupList::default(),
|
||||
browse: HashMap::new(),
|
||||
routes: HashMap::new(),
|
||||
@@ -145,6 +147,7 @@ impl GroupsRegistry {
|
||||
.iter()
|
||||
.find(|group| group.read(cx).key() == key)
|
||||
.cloned()
|
||||
.or_else(|| self.previews.get(key).cloned())
|
||||
}
|
||||
|
||||
pub fn join(&mut self, reference: GroupReference, cx: &mut Context<Self>) {
|
||||
@@ -268,6 +271,11 @@ impl GroupsRegistry {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Whether the group is one of the user's groups, not a preview.
|
||||
fn joined(&self, key: &GroupKey, cx: &App) -> bool {
|
||||
self.groups.iter().any(|group| group.read(cx).key() == key)
|
||||
}
|
||||
|
||||
/// Load the discoverable groups from the local database only.
|
||||
pub fn cached_discovery(&self, cx: &App) -> Task<Discovery> {
|
||||
let client = NostrRegistry::global(cx).read(cx).client();
|
||||
@@ -340,6 +348,7 @@ impl GroupsRegistry {
|
||||
let routes = std::mem::take(&mut self.routes);
|
||||
|
||||
self.groups.clear();
|
||||
self.previews.clear();
|
||||
self.observers.clear();
|
||||
self.forks.clear();
|
||||
self.fork_authors.clear();
|
||||
@@ -532,6 +541,12 @@ impl GroupsRegistry {
|
||||
}
|
||||
|
||||
fn ensure(&mut self, key: GroupKey, cx: &mut Context<Self>) {
|
||||
// A previewed group becomes one of the user's groups, keeping its state.
|
||||
if let Some(group) = self.previews.remove(&key) {
|
||||
self.groups.push(group);
|
||||
return;
|
||||
}
|
||||
|
||||
if self.group(&key, cx).is_some() {
|
||||
return;
|
||||
}
|
||||
@@ -539,9 +554,31 @@ impl GroupsRegistry {
|
||||
let group = cx.new(|_| Group::new(key.clone()));
|
||||
self.groups.push(group.clone());
|
||||
|
||||
self.observe_group(key.clone(), &group, cx);
|
||||
self.subscribe(key.clone(), cx);
|
||||
self.load_state(key, cx);
|
||||
}
|
||||
|
||||
/// Track a group opened for preview: connected, but not joined.
|
||||
pub fn open(&mut self, key: GroupKey, cx: &mut Context<Self>) -> Entity<Group> {
|
||||
if let Some(group) = self.group(&key, cx) {
|
||||
return group;
|
||||
}
|
||||
|
||||
let group = cx.new(|_| Group::new(key.clone()));
|
||||
self.previews.insert(key.clone(), group.clone());
|
||||
|
||||
self.observe_group(key.clone(), &group, cx);
|
||||
self.subscribe(key.clone(), cx);
|
||||
self.load_state(key, cx);
|
||||
|
||||
group
|
||||
}
|
||||
|
||||
fn observe_group(&mut self, key: GroupKey, group: &Entity<Group>, cx: &mut Context<Self>) {
|
||||
self.observers.insert(
|
||||
key.clone(),
|
||||
cx.subscribe(&group, |this, group, event, cx| match event {
|
||||
key,
|
||||
cx.subscribe(group, |this, group, event, cx| match event {
|
||||
GroupEvent::Sent(event) => {
|
||||
if event.kind == Kind::GroupDeleteEvent {
|
||||
this.forget_event(group, event.clone(), cx);
|
||||
@@ -551,7 +588,13 @@ impl GroupsRegistry {
|
||||
}
|
||||
}
|
||||
GroupEvent::Updated => {
|
||||
this.refresh_entry(group, cx);
|
||||
let key = group.read(cx).key().clone();
|
||||
|
||||
// Only the user's groups belong in their group list.
|
||||
if this.joined(&key, cx) {
|
||||
this.refresh_entry(group, cx);
|
||||
}
|
||||
|
||||
this.sync_forks(false, cx);
|
||||
|
||||
cx.emit(GroupsEvent::Updated);
|
||||
@@ -560,9 +603,6 @@ impl GroupsRegistry {
|
||||
_ => {}
|
||||
}),
|
||||
);
|
||||
|
||||
self.subscribe(key.clone(), cx);
|
||||
self.load_state(key, cx);
|
||||
}
|
||||
|
||||
fn subscribe(&mut self, key: GroupKey, cx: &mut Context<Self>) {
|
||||
@@ -586,6 +626,7 @@ impl GroupsRegistry {
|
||||
self.routes.remove(&page);
|
||||
self.observers.remove(&key);
|
||||
self.groups.retain(|group| group.read(cx).key() != &key);
|
||||
self.previews.remove(&key);
|
||||
self.list.remove(&key);
|
||||
|
||||
let nostr = NostrRegistry::global(cx);
|
||||
|
||||
Reference in New Issue
Block a user