1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
use dioxus::prelude::*;
use crate::room::media::image_viewer::ImageViewer;
use crate::state::room_state::TimelineContent;
use crate::utils::html_render::sanitize_matrix_html;
use crate::utils::link_preview::LinkPreview;
use crate::utils::room_helpers::format_file_size;
/// Detect URLs in plain text and return them.
fn extract_urls(text: &str) -> Vec<String> {
let finder = linkify::LinkFinder::new();
finder
.links(text)
.filter(|link| link.kind() == &linkify::LinkKind::Url)
.map(|link| link.as_str().to_string())
.collect()
}
/// Code block with syntax highlighting via CSS classes.
#[component]
fn CodeBlock(code: String, language: Option<String>) -> Element {
let lang_class = language
.as_ref()
.map(|l| format!("language-{l}"))
.unwrap_or_default();
let lang_label = language.unwrap_or_default();
rsx! {
div {
class: "code-block",
if !lang_label.is_empty() {
div {
class: "code-block__header",
span { class: "code-block__language", "{lang_label}" }
}
}
pre {
class: "code-block__pre",
code {
class: "code-block__code {lang_class}",
"{code}"
}
}
}
}
}
/// Link preview card component.
#[component]
fn LinkPreviewCard(url: String) -> Element {
let mut preview = use_signal(|| Option::<LinkPreview>::None);
let mut loaded = use_signal(|| false);
// Attempt to load preview via Matrix URL preview API
if !*loaded.read() {
loaded.set(true);
let url_clone = url.clone();
spawn(async move {
// Use Matrix media preview API if available
// For now, show a simple link card
preview.set(Some(LinkPreview {
url: url_clone,
title: None,
description: None,
image_url: None,
site_name: None,
}));
});
}
let p = preview.read();
if let Some(ref lp) = *p {
let domain = lp.url.split('/').nth(2).unwrap_or(&lp.url);
rsx! {
a {
class: "link-preview-card",
href: "{lp.url}",
target: "_blank",
rel: "noopener noreferrer",
if let Some(ref img) = lp.image_url {
img {
class: "link-preview-card__image",
src: "{img}",
alt: "",
}
}
div {
class: "link-preview-card__info",
if let Some(ref title) = lp.title {
span { class: "link-preview-card__title", "{title}" }
}
if let Some(ref desc) = lp.description {
span { class: "link-preview-card__description", "{desc}" }
}
span { class: "link-preview-card__domain", "{domain}" }
}
}
}
} else {
rsx! {}
}
}
/// Parse code blocks from text: returns (has_code_block, language, code, remaining_text).
fn parse_code_blocks(body: &str) -> Option<(String, Option<String>, String)> {
if let Some(start) = body.find("```") {
let after_backticks = &body[start + 3..];
// Extract language hint (first line after ```)
let (lang, code_start) = if let Some(nl) = after_backticks.find('\n') {
let lang_str = after_backticks[..nl].trim();
let lang = if lang_str.is_empty() { None } else { Some(lang_str.to_string()) };
(lang, nl + 1)
} else {
(None, 0)
};
// Find closing ```
if let Some(end) = after_backticks[code_start..].find("```") {
let code = after_backticks[code_start..code_start + end].to_string();
let before = body[..start].to_string();
let after = after_backticks[code_start + end + 3..].to_string();
let remaining = format!("{before}{after}").trim().to_string();
return Some((code, lang, remaining));
}
}
None
}
/// Message content renderer - renders different message types.
#[component]
pub fn MessageContentView(content: TimelineContent) -> Element {
match content {
TimelineContent::Text { body, formatted_body } => {
// Check for code blocks in the body
if let Some((code, lang, remaining)) = parse_code_blocks(&body) {
return rsx! {
div {
class: "message-content message-content--text",
if !remaining.is_empty() {
p { "{remaining}" }
}
CodeBlock {
code: code,
language: lang,
}
}
};
}
// Extract URLs for link previews
let urls = extract_urls(&body);
let first_url = urls.first().cloned();
if let Some(html) = formatted_body {
let sanitized = sanitize_matrix_html(&html);
rsx! {
div {
class: "message-content message-content--text",
dangerous_inner_html: "{sanitized}",
if let Some(url) = first_url {
LinkPreviewCard { url: url }
}
}
}
} else {
rsx! {
div {
class: "message-content message-content--text",
"{body}"
if let Some(url) = first_url {
LinkPreviewCard { url: url }
}
}
}
}
}
TimelineContent::Image { body, url, thumbnail_url, blurhash, width, height } => {
let display_url = thumbnail_url.clone().or(url.clone());
let full_url = url.or(thumbnail_url);
let (w, h) = crate::utils::media_helpers::fit_dimensions(
width.unwrap_or(400),
height.unwrap_or(300),
400,
300,
);
let mut show_viewer = use_signal(|| false);
rsx! {
div {
class: "message-content message-content--image",
if let Some(ref src) = display_url {
img {
src: "{src}",
alt: "{body}",
width: "{w}",
height: "{h}",
loading: "lazy",
style: "cursor: pointer;",
onclick: move |_| show_viewer.set(true),
}
} else if let Some(_hash) = blurhash {
div {
class: "message-content__blurhash",
style: "width: {w}px; height: {h}px;",
}
}
if !body.is_empty() {
span {
class: "message-content__caption",
"{body}"
}
}
// Full-screen image viewer
if *show_viewer.read() {
if let Some(ref viewer_url) = full_url {
ImageViewer {
image_url: viewer_url.clone(),
alt_text: body.clone(),
original_width: width,
original_height: height,
on_close: move |_| show_viewer.set(false),
}
}
}
}
}
}
TimelineContent::File { body, url, size, mimetype } => {
let size_text = size.map(format_file_size).unwrap_or_default();
let mime_text = mimetype.unwrap_or_else(|| "Unknown type".to_string());
rsx! {
div {
class: "message-content message-content--file",
div {
class: "message-content__file-icon",
"📄"
}
div {
class: "message-content__file-info",
span {
class: "message-content__file-name",
"{body}"
}
span {
class: "message-content__file-meta",
"{mime_text} · {size_text}"
}
}
if let Some(href) = url {
a {
class: "message-content__file-download",
href: "{href}",
target: "_blank",
"Download"
}
}
}
}
}
TimelineContent::Audio { body, url, duration_ms } => {
let duration = duration_ms
.map(|ms| {
let secs = ms / 1000;
format!("{}:{:02}", secs / 60, secs % 60)
})
.unwrap_or_default();
rsx! {
div {
class: "message-content message-content--audio",
if let Some(ref src) = url {
audio {
class: "message-content__audio-player",
controls: true,
preload: "metadata",
source {
src: "{src}",
}
}
} else {
span { class: "message-content__audio-icon", "🎵" }
}
span { class: "message-content__audio-name", "{body}" }
if !duration.is_empty() {
span { class: "message-content__audio-duration", "{duration}" }
}
}
}
}
TimelineContent::Video { body, url, thumbnail_url, width, height, duration_ms: _ } => {
let (w, h) = crate::utils::media_helpers::fit_dimensions(
width.unwrap_or(400),
height.unwrap_or(300),
400,
300,
);
rsx! {
div {
class: "message-content message-content--video",
if let Some(ref src) = url {
{
let poster_url = thumbnail_url.clone().unwrap_or_default();
rsx! {
video {
class: "message-content__video-player",
controls: true,
preload: "metadata",
width: "{w}",
height: "{h}",
poster: "{poster_url}",
source {
src: "{src}",
}
}
}
}
} else if let Some(thumb) = thumbnail_url {
div {
class: "message-content__video-thumb",
style: "width: {w}px; height: {h}px;",
img {
src: "{thumb}",
alt: "{body}",
width: "{w}",
height: "{h}",
}
div {
class: "message-content__play-overlay",
"▶"
}
}
}
if !body.is_empty() {
span { class: "message-content__video-name", "{body}" }
}
}
}
}
TimelineContent::Emote { body, formatted_body: _ } => {
rsx! {
div {
class: "message-content message-content--emote",
"* {body}"
}
}
}
TimelineContent::Notice { body, formatted_body } => {
if let Some(html) = formatted_body {
let sanitized = sanitize_matrix_html(&html);
rsx! {
div {
class: "message-content message-content--notice",
dangerous_inner_html: "{sanitized}",
}
}
} else {
rsx! {
div {
class: "message-content message-content--notice",
"{body}"
}
}
}
}
TimelineContent::Redacted { reason } => {
rsx! {
div {
class: "message-content message-content--redacted",
span { "This message was deleted" }
if let Some(r) = reason {
span { class: "message-content__redact-reason", " ({r})" }
}
}
}
}
TimelineContent::EncryptionError { message } => {
rsx! {
div {
class: "message-content message-content--error",
span { class: "message-content__error-icon", "🔒" }
span { "Unable to decrypt: {message}" }
}
}
}
TimelineContent::Sticker { body, url } => {
rsx! {
div {
class: "message-content message-content--sticker",
if let Some(src) = url {
img {
src: "{src}",
alt: "{body}",
class: "message-content__sticker-img",
}
}
}
}
}
TimelineContent::StateEvent { description } => {
rsx! {
div {
class: "message-content message-content--state",
"{description}"
}
}
}
TimelineContent::Poll { question, answers, kind: _, is_ended } => {
let total_votes: u32 = answers.iter().map(|a| a.vote_count).sum();
let vote_label = if total_votes != 1 { format!("{total_votes} votes") } else { "1 vote".to_string() };
rsx! {
div {
class: "message-content message-content--poll",
div {
class: "poll-display__header",
span { class: "poll-display__icon", "📊" }
span { class: "poll-display__question", "{question}" }
if is_ended {
span { class: "poll-display__ended-badge", "Ended" }
}
}
div {
class: "poll-display__answers",
for answer in answers.iter() {
{
let text = answer.text.clone();
let count = answer.vote_count;
let pct = if total_votes > 0 { (count as f64 / total_votes as f64 * 100.0) as u32 } else { 0 };
let voted = answer.voted_by_me;
rsx! {
div {
class: if voted { "poll-display__answer poll-display__answer--voted" } else { "poll-display__answer" },
div {
class: "poll-display__answer-bar",
style: "width: {pct}%;",
}
span { class: "poll-display__answer-text", "{text}" }
span { class: "poll-display__answer-count", "{count}" }
}
}
}
}
}
div {
class: "poll-display__footer",
"{vote_label}"
}
}
}
}
TimelineContent::Location { body, geo_uri, description } => {
// Parse geo: URI to extract lat/lon
let coords = geo_uri.strip_prefix("geo:").unwrap_or(&geo_uri);
let parts: Vec<&str> = coords.split(',').collect();
let lat = parts.first().unwrap_or(&"0");
let lon = parts.get(1).and_then(|s| s.split(';').next()).unwrap_or("0");
let desc = description.unwrap_or_else(|| body.clone());
let map_url = format!("https://www.openstreetmap.org/?mlat={lat}&mlon={lon}#map=15/{lat}/{lon}");
rsx! {
div {
class: "message-content message-content--location",
div {
class: "location-display",
span { class: "location-display__icon", "📍" }
div {
class: "location-display__info",
span { class: "location-display__desc", "{desc}" }
span { class: "location-display__coords", "{lat}, {lon}" }
}
a {
class: "btn btn--secondary btn--sm",
href: "{map_url}",
target: "_blank",
rel: "noopener noreferrer",
"Open Map"
}
}
}
}
}
TimelineContent::VoiceMessage { body, url, duration_ms, waveform } => {
let duration = duration_ms
.map(|ms| {
let secs = ms / 1000;
format!("{}:{:02}", secs / 60, secs % 60)
})
.unwrap_or_else(|| "0:00".to_string());
rsx! {
div {
class: "message-content message-content--voice",
div {
class: "voice-display",
if let Some(ref src) = url {
audio {
class: "message-content__audio-player",
controls: true,
preload: "metadata",
source {
src: "{src}",
}
}
} else {
button {
class: "voice-display__play-btn",
disabled: true,
"▶"
}
}
div {
class: "voice-display__waveform",
for (idx, sample) in waveform.iter().enumerate() {
{
let height = (*sample as f32 / 1024.0 * 100.0).max(5.0).min(100.0);
rsx! {
div {
key: "wave-{idx}",
class: "voice-display__bar",
style: "height: {height}%;",
}
}
}
}
}
span {
class: "voice-display__duration",
"{duration}"
}
}
if !body.is_empty() {
span {
class: "message-content__audio-name",
"{body}"
}
}
}
}
}
}
}