Skip to content

Commit

Permalink
Show errors fetching space hierarchy when list is empty (Nheko-Reborn…
Browse files Browse the repository at this point in the history
  • Loading branch information
ulyssa committed May 28, 2023
1 parent 8d22b83 commit 0f87036
Showing 1 changed file with 60 additions and 27 deletions.
87 changes: 60 additions & 27 deletions src/windows/room/space.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use std::ops::{Deref, DerefMut};
use std::time::{Duration, Instant};

use matrix_sdk::{
room::Room as MatrixRoom,
ruma::{OwnedRoomId, RoomId},
};

use modalkit::tui::{buffer::Buffer, layout::Rect, widgets::StatefulWidget};
use modalkit::tui::{
buffer::Buffer,
layout::Rect,
style::{Color, Style},
text::{Span, Spans, Text},
widgets::StatefulWidget,
};

use modalkit::{
widgets::list::{List, ListState},
Expand All @@ -16,19 +23,23 @@ use crate::base::{IambBufferId, IambInfo, ProgramStore, RoomFocus};

use crate::windows::RoomItem;

const SPACE_HIERARCHY_DEBOUNCE: Duration = Duration::from_secs(5);

pub struct SpaceState {
room_id: OwnedRoomId,
room: MatrixRoom,
list: ListState<RoomItem, IambInfo>,
last_fetch: Option<Instant>,
}

impl SpaceState {
pub fn new(room: MatrixRoom) -> Self {
let room_id = room.room_id().to_owned();
let content = IambBufferId::Room(room_id.clone(), RoomFocus::Scrollback);
let list = ListState::new(content, vec![]);
let last_fetch = None;

SpaceState { room_id, room, list }
SpaceState { room_id, room, list, last_fetch }
}

pub fn refresh_room(&mut self, store: &mut ProgramStore) {
Expand All @@ -50,6 +61,7 @@ impl SpaceState {
room_id: self.room_id.clone(),
room: self.room.clone(),
list: self.list.dup(store),
last_fetch: self.last_fetch,
}
}
}
Expand Down Expand Up @@ -94,30 +106,51 @@ impl<'a> StatefulWidget for Space<'a> {
type State = SpaceState;

fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) {
let members =
if let Ok(m) = self.store.application.worker.space_members(state.room_id.clone()) {
m
} else {
return;
};

let items = members
.into_iter()
.filter_map(|id| {
let (room, name, tags) = self.store.application.worker.get_room(id.clone()).ok()?;

if id != state.room_id {
Some(RoomItem::new(room, name, tags, self.store))
} else {
None
}
})
.collect();

state.list.set(items);

List::new(self.store)
.focus(self.focused)
.render(area, buffer, &mut state.list)
let mut empty_message = None;
let need_fetch = match state.last_fetch {
Some(i) => i.elapsed() >= SPACE_HIERARCHY_DEBOUNCE,
None => true,
};

if need_fetch {
let res = self.store.application.worker.space_members(state.room_id.clone());

match res {
Ok(members) => {
let items = members
.into_iter()
.filter_map(|id| {
let (room, name, tags) =
self.store.application.worker.get_room(id.clone()).ok()?;

if id != state.room_id {
Some(RoomItem::new(room, name, tags, self.store))
} else {
None
}
})
.collect();

state.list.set(items);
state.last_fetch = Some(Instant::now());
},
Err(e) => {
let lines = vec![
Spans::from("Unable to fetch space room hierarchy:"),
Span::styled(e.to_string(), Style::default().fg(Color::Red)).into(),
];

empty_message = Text { lines }.into();
},
}
}

let mut list = List::new(self.store).focus(self.focused);

if let Some(text) = empty_message {
list = list.empty_message(text);
}

list.render(area, buffer, &mut state.list)
}
}

0 comments on commit 0f87036

Please sign in to comment.