Skip to content

Commit 005f002

Browse files
committed
feat(multiverse): Start to render threads
1 parent 80b8a6d commit 005f002

File tree

1 file changed

+51
-13
lines changed

1 file changed

+51
-13
lines changed

labs/multiverse/src/widgets/room_view/timeline.rs

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use std::sync::Arc;
33
use imbl::Vector;
44
use matrix_sdk::ruma::events::room::message::MessageType;
55
use matrix_sdk_ui::timeline::{
6-
MembershipChange, MsgLikeContent, MsgLikeKind, TimelineItem, TimelineItemContent,
7-
TimelineItemKind, VirtualTimelineItem,
6+
MembershipChange, MsgLikeContent, MsgLikeKind, TimelineDetails, TimelineItem,
7+
TimelineItemContent, TimelineItemKind, VirtualTimelineItem,
88
};
99
use ratatui::{prelude::*, widgets::*};
1010

11-
use crate::{ALT_ROW_COLOR, NORMAL_ROW_COLOR, TEXT_COLOR};
11+
use crate::{ALT_ROW_COLOR, NORMAL_ROW_COLOR, SELECTED_STYLE_FG, TEXT_COLOR};
1212

1313
pub struct TimelineView<'a> {
1414
items: &'a Vector<Arc<TimelineItem>>,
@@ -27,7 +27,7 @@ impl StatefulWidget for &mut TimelineView<'_> {
2727
where
2828
Self: Sized,
2929
{
30-
let mut content = Vec::new();
30+
let mut content: Vec<ListItem<'_>> = Vec::new();
3131

3232
for item in self.items.iter() {
3333
match item.kind() {
@@ -39,20 +39,58 @@ impl StatefulWidget for &mut TimelineView<'_> {
3939
kind: MsgLikeKind::Message(message),
4040
..
4141
}) => {
42+
if ev.content().thread_root().is_some() {
43+
continue;
44+
}
45+
4246
if let MessageType::Text(text) = message.msgtype() {
43-
content.push(format!("{}: {}", sender, text.body))
47+
let mut lines = Vec::new();
48+
let first_line = Line::from(format!("{}: {}", sender, text.body));
49+
50+
lines.push(first_line);
51+
52+
if let Some(thread_summary) = ev.content().thread_summary() {
53+
match thread_summary.latest_event {
54+
TimelineDetails::Unavailable => {}
55+
TimelineDetails::Pending => {}
56+
TimelineDetails::Ready(e) => {
57+
let sender = e.sender;
58+
let content =
59+
e.content.as_message().map(|m| m.msgtype());
60+
61+
if let Some(MessageType::Text(text)) = content {
62+
let replies = if thread_summary.num_replies == 1 {
63+
"1 reply".to_owned()
64+
} else {
65+
format!("{} replies", {
66+
thread_summary.num_replies
67+
})
68+
};
69+
let thread_line = Line::from(format!(
70+
" 💬 {replies} {sender}: {}",
71+
text.body
72+
));
73+
74+
lines.push(thread_line);
75+
}
76+
}
77+
TimelineDetails::Error(_) => {}
78+
}
79+
}
80+
81+
content.push(ListItem::from(lines));
4482
}
4583
}
4684

4785
TimelineItemContent::MsgLike(MsgLikeContent {
4886
kind: MsgLikeKind::Redacted,
4987
..
50-
}) => content.push(format!("{sender}: -- redacted --")),
88+
}) => content.push(format!("{sender}: -- redacted --").into()),
5189

5290
TimelineItemContent::MsgLike(MsgLikeContent {
5391
kind: MsgLikeKind::UnableToDecrypt(_),
5492
..
55-
}) => content.push(format!("{sender}: (UTD)")),
93+
}) => content.push(format!("{sender}: (UTD)").into()),
5694

5795
TimelineItemContent::MembershipChange(m) => {
5896
if let Some(change) = m.change() {
@@ -91,7 +129,7 @@ impl StatefulWidget for &mut TimelineView<'_> {
91129
}
92130
};
93131

94-
content.push(format!("{display_name} {change}"));
132+
content.push(format!("{display_name} {change}").into());
95133
}
96134
}
97135

@@ -116,13 +154,13 @@ impl StatefulWidget for &mut TimelineView<'_> {
116154

117155
TimelineItemKind::Virtual(virt) => match virt {
118156
VirtualTimelineItem::DateDivider(unix_ts) => {
119-
content.push(format!("Date: {unix_ts:?}"));
157+
content.push(format!("Date: {unix_ts:?}").into());
120158
}
121159
VirtualTimelineItem::ReadMarker => {
122-
content.push("Read marker".to_owned());
160+
content.push("Read marker".to_owned().into());
123161
}
124162
VirtualTimelineItem::TimelineStart => {
125-
content.push("🥳 Timeline start! 🥳".to_owned());
163+
content.push("🥳 Timeline start! 🥳".to_owned().into());
126164
}
127165
},
128166
}
@@ -136,8 +174,8 @@ impl StatefulWidget for &mut TimelineView<'_> {
136174
0 => NORMAL_ROW_COLOR,
137175
_ => ALT_ROW_COLOR,
138176
};
139-
let line = Line::styled(line, TEXT_COLOR);
140-
ListItem::new(line).bg(bg_color)
177+
178+
line.fg(TEXT_COLOR).bg(bg_color)
141179
})
142180
.collect::<Vec<_>>();
143181

0 commit comments

Comments
 (0)