multiverse/widgets/room_view/details/
events.rs1use itertools::Itertools;
2use matrix_sdk::Room;
3use matrix_sdk_common::executor::Handle;
4use ratatui::{
5 prelude::*,
6 widgets::{Paragraph, Wrap},
7};
8
9use crate::TEXT_COLOR;
10
11pub struct EventsView<'a> {
12 room: Option<&'a Room>,
13}
14
15impl<'a> EventsView<'a> {
16 pub fn new(room: Option<&'a Room>) -> Self {
17 Self { room }
18 }
19}
20
21impl Widget for &mut EventsView<'_> {
22 fn render(self, area: Rect, buf: &mut Buffer)
23 where
24 Self: Sized,
25 {
26 match self.room {
27 Some(room) => {
28 let events = tokio::task::block_in_place(|| {
29 Handle::current().block_on(async {
30 let (room_event_cache, _drop_handles) = room
31 .client()
32 .event_cache()
33 .unwrap()
34 .room(room.room_id())
35 .await
36 .unwrap();
37 room_event_cache
38 .events()
39 .await
40 .expect("Failed to fetch the events because of a store error")
41 })
42 });
43
44 let separator = Line::from("\n");
45 let events = events
46 .into_iter()
47 .map(|sync_timeline_item| sync_timeline_item.raw().json().to_string())
48 .map(Line::from);
49
50 let events = Itertools::intersperse(events, separator);
51 let lines: Vec<_> = [Line::from("")].into_iter().chain(events).collect();
52
53 Paragraph::new(lines).fg(TEXT_COLOR).wrap(Wrap { trim: false }).render(area, buf);
54 }
55
56 None => {
57 Paragraph::new("(room disappeared in the room list service)")
58 .fg(TEXT_COLOR)
59 .wrap(Wrap { trim: false })
60 .render(area, buf);
61 }
62 }
63 }
64}