synpad 0.1.0

A full-featured Matrix chat client built with Dioxus
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
use dioxus::prelude::*;
use matrix_sdk::ruma::{OwnedEventId, OwnedRoomId};

use crate::components::avatar::Avatar;
use crate::components::modal::Modal;
use crate::room::timeline::edit_history::EditHistoryModal;
use crate::room::timeline::forward_dialog::ForwardDialog;
use crate::room::timeline::message_content::MessageContentView;
use crate::room::timeline::reaction_list::ReactionList;
use crate::room::timeline::reply_preview::ReplyPreviewComponent;
use crate::state::app_state::AppState;
use crate::state::room_state::{EditingMessage, ReactionGroup, ReplyPreview, ReplyingTo, TimelineContent};
use crate::utils::time_format::format_time;

/// Quick emoji options for the reaction picker.
const QUICK_REACTIONS: &[&str] = &["👍", "❤️", "😂", "🎉", "😮", "👀"];

/// Send a reaction to a message.
async fn send_reaction(
    state: Signal<AppState>,
    room_id_str: &str,
    event_id_str: &str,
    emoji: &str,
) -> Result<(), String> {
    let client = { state.read().client.clone() };
    let client = client.ok_or_else(|| "Not logged in".to_string())?;

    let room_id: OwnedRoomId = room_id_str
        .try_into()
        .map_err(|e| format!("Invalid room ID: {e}"))?;
    let event_id: OwnedEventId = event_id_str
        .try_into()
        .map_err(|e| format!("Invalid event ID: {e}"))?;

    let room = client
        .get_room(&room_id)
        .ok_or_else(|| format!("Room not found: {room_id}"))?;

    use matrix_sdk::ruma::events::reaction::ReactionEventContent;
    use matrix_sdk::ruma::events::relation::Annotation;

    let annotation = Annotation::new(event_id, emoji.to_string());
    let content = ReactionEventContent::new(annotation);

    room.send(content)
        .await
        .map_err(|e| format!("Failed to send reaction: {e}"))?;

    Ok(())
}

/// Redact (delete) a message.
async fn redact_message(
    state: Signal<AppState>,
    room_id_str: &str,
    event_id_str: &str,
) -> Result<(), String> {
    let client = { state.read().client.clone() };
    let client = client.ok_or_else(|| "Not logged in".to_string())?;

    let room_id: OwnedRoomId = room_id_str
        .try_into()
        .map_err(|e| format!("Invalid room ID: {e}"))?;
    let event_id: OwnedEventId = event_id_str
        .try_into()
        .map_err(|e| format!("Invalid event ID: {e}"))?;

    let room = client
        .get_room(&room_id)
        .ok_or_else(|| format!("Room not found: {room_id}"))?;

    room.redact(&event_id, None, None)
        .await
        .map_err(|e| format!("Failed to delete message: {e}"))?;

    Ok(())
}

/// Toggle pin state for a message (add or remove from m.room.pinned_events).
async fn toggle_pin_message(
    state: Signal<AppState>,
    room_id_str: &str,
    event_id_str: &str,
) -> Result<(), String> {
    let client = { state.read().client.clone() };
    let client = client.ok_or_else(|| "Not logged in".to_string())?;

    let room_id: OwnedRoomId = room_id_str
        .try_into()
        .map_err(|e| format!("Invalid room ID: {e}"))?;
    let event_id: OwnedEventId = event_id_str
        .try_into()
        .map_err(|e| format!("Invalid event ID: {e}"))?;

    let room = client
        .get_room(&room_id)
        .ok_or_else(|| format!("Room not found: {room_id}"))?;

    // Get current pinned events
    let mut pinned_ids: Vec<OwnedEventId> = room
        .pinned_event_ids()
        .unwrap_or_default();

    // Toggle: remove if present, add if not
    if let Some(pos) = pinned_ids.iter().position(|id| *id == event_id) {
        pinned_ids.remove(pos);
        tracing::info!("Unpinning event {event_id}");
    } else {
        pinned_ids.push(event_id.clone());
        tracing::info!("Pinning event {event_id}");
    }

    // Send updated pinned events state
    use matrix_sdk::ruma::events::room::pinned_events::RoomPinnedEventsEventContent;
    let content = RoomPinnedEventsEventContent::new(pinned_ids);
    room.send_state_event(content)
        .await
        .map_err(|e| format!("Failed to update pinned events: {e}"))?;

    Ok(())
}

/// A single event/message tile in the timeline.
#[component]
pub fn EventTile(
    room_id: String,
    event_id: Option<String>,
    sender: String,
    sender_display_name: String,
    sender_avatar_url: Option<String>,
    timestamp: u64,
    content: TimelineContent,
    reactions: Vec<ReactionGroup>,
    is_edited: bool,
    reply_to: Option<Box<ReplyPreview>>,
    is_own_message: bool,
) -> Element {
    let mut state = use_context::<Signal<AppState>>();
    let mut show_actions = use_signal(|| false);
    let mut show_react_picker = use_signal(|| false);
    let mut show_more_menu = use_signal(|| false);
    let mut show_source = use_signal(|| false);
    let mut show_forward = use_signal(|| false);
    let mut show_context_menu = use_signal(|| false);
    let mut context_menu_pos = use_signal(|| (0i32, 0i32));
    let mut source_json = use_signal(|| String::new());
    let mut show_edit_history = use_signal(|| false);
    let mut show_report = use_signal(|| false);

    let time_str = format_time(timestamp);
    let tile_class = if is_own_message {
        "event-tile event-tile--own"
    } else {
        "event-tile"
    };

    // State events get a different layout
    if let TimelineContent::StateEvent { description } = &content {
        return rsx! {
            div {
                class: "event-tile event-tile--state",
                span { class: "event-tile__state-text", "{description}" }
            }
        };
    }

    // Prepare source text for View Source modal
    let source_text = format!(
        "{{\n  \"event_id\": \"{}\",\n  \"sender\": \"{}\",\n  \"sender_display_name\": \"{}\",\n  \"timestamp\": {},\n  \"is_edited\": {},\n  \"content\": {:#?},\n  \"reactions\": {:#?},\n  \"reply_to\": {:#?}\n}}",
        event_id.as_deref().unwrap_or("(pending)"),
        sender,
        sender_display_name,
        timestamp,
        is_edited,
        content,
        reactions,
        reply_to,
    );

    rsx! {
        div {
            class: "{tile_class}",
            onmouseenter: move |_| show_actions.set(true),
            onmouseleave: move |_| {
                show_actions.set(false);
                show_context_menu.set(false);
            },
            // Right-click context menu
            oncontextmenu: move |evt: Event<MouseData>| {
                evt.prevent_default();
                let coords = evt.page_coordinates();
                context_menu_pos.set((coords.x as i32, coords.y as i32));
                show_context_menu.set(true);
            },

            // Avatar column (clickable to open user profile)
            {
                let profile_sender = sender.clone();
                rsx! {
                    div {
                        class: "event-tile__avatar",
                        style: "cursor: pointer;",
                        onclick: move |_| {
                            state.write().right_panel = crate::state::app_state::RightPanelView::MemberDetail(profile_sender.clone());
                        },
                        Avatar {
                            name: sender_display_name.clone(),
                            url: sender_avatar_url,
                            size: 36,
                        }
                    }
                }
            }

            // Content column
            div {
                class: "event-tile__content",

                // Sender name and timestamp
                div {
                    class: "event-tile__header",
                    span {
                        class: "event-tile__sender",
                        "{sender_display_name}"
                    }
                    span {
                        class: "event-tile__time",
                        "{time_str}"
                    }
                    if is_edited {
                        span {
                            class: "event-tile__edited",
                            style: "cursor: pointer;",
                            title: "View edit history",
                            onclick: move |_| show_edit_history.set(true),
                            "(edited)"
                        }
                    }
                }

                // Reply preview
                if let Some(reply) = reply_to {
                    ReplyPreviewComponent {
                        sender_name: reply.sender_name.clone(),
                        body: reply.body.clone(),
                    }
                }

                // Message body
                MessageContentView {
                    content: content.clone(),
                }

                // Reactions
                if !reactions.is_empty() {
                    ReactionList {
                        room_id: room_id.clone(),
                        event_id: event_id.clone().unwrap_or_default(),
                        reactions: reactions,
                    }
                }
            }

            // Hover action buttons
            if *show_actions.read() {
                {
                    let reply_event_id = event_id.clone();
                    let reply_sender = sender_display_name.clone();
                    let reply_body = content.body_text();
                    let reply_room_id = room_id.clone();
                    let react_room_id = room_id.clone();
                    let react_event_id = event_id.clone();
                    rsx! {
                        div {
                            class: "event-tile__actions",
                            button {
                                class: "event-tile__action-btn",
                                title: "Reply",
                                onclick: move |_| {
                                    if let Some(eid) = &reply_event_id {
                                        if let Ok(event_id) = OwnedEventId::try_from(eid.as_str()) {
                                            if let Ok(room_id) = OwnedRoomId::try_from(reply_room_id.as_str()) {
                                                state.write().replying_to = Some(ReplyingTo {
                                                    event_id,
                                                    sender_name: reply_sender.clone(),
                                                    body: reply_body.clone(),
                                                    room_id,
                                                });
                                            }
                                        }
                                    }
                                },
                                ""
                            }
                            button {
                                class: "event-tile__action-btn",
                                title: "React",
                                onclick: move |_| {
                                        let current = *show_react_picker.read();
                                    show_react_picker.set(!current);
                                },
                                "😀"
                            }
                            {
                                let more_room_id = room_id.clone();
                                let more_event_id = event_id.clone();
                                let more_body = content.body_text();
                                rsx! {
                                    button {
                                        class: "event-tile__action-btn",
                                        title: "More",
                                        onclick: move |_| {
                                            let current = *show_more_menu.read();
                                            show_more_menu.set(!current);
                                        },
                                        ""
                                    }
                                    // More actions menu
                                    if *show_more_menu.read() {
                                        {
                                            let del_room_id = more_room_id.clone();
                                            let del_event_id = more_event_id.clone();
                                            let thread_event_id = more_event_id.clone();
                                            rsx! {
                                                div {
                                                    class: "event-tile__more-menu",
                                                    if is_own_message {
                                                        button {
                                                            class: "event-tile__more-item",
                                                            onclick: move |_| {
                                                                show_more_menu.set(false);
                                                                if let Some(eid) = &more_event_id {
                                                                    if let Ok(event_id) = OwnedEventId::try_from(eid.as_str()) {
                                                                        if let Ok(room_id) = OwnedRoomId::try_from(more_room_id.as_str()) {
                                                                            let mut s = state.write();
                                                                            s.replying_to = None;
                                                                            s.editing_message = Some(EditingMessage {
                                                                                event_id,
                                                                                room_id,
                                                                                original_body: more_body.clone(),
                                                                            });
                                                                        }
                                                                    }
                                                                }
                                                            },
                                                            "✏ Edit"
                                                        }
                                                        button {
                                                            class: "event-tile__more-item event-tile__more-item--danger",
                                                            onclick: move |_| {
                                                                show_more_menu.set(false);
                                                                if let Some(eid) = &del_event_id {
                                                                    let rid = del_room_id.clone();
                                                                    let eid = eid.clone();
                                                                    spawn(async move {
                                                                        if let Err(e) = redact_message(state, &rid, &eid).await {
                                                                            tracing::error!("Failed to delete message: {e}");
                                                                        }
                                                                    });
                                                                }
                                                            },
                                                            "🗑 Delete"
                                                        }
                                                    }
                                                    // Forward - available for all messages
                                                    button {
                                                        class: "event-tile__more-item",
                                                        onclick: move |_| {
                                                            show_more_menu.set(false);
                                                            show_forward.set(true);
                                                        },
                                                        "↪ Forward"
                                                    }
                                                    // Reply in Thread
                                                    {
                                                        rsx! {
                                                            button {
                                                                class: "event-tile__more-item",
                                                                onclick: move |_| {
                                                                    show_more_menu.set(false);
                                                                    if let Some(ref eid) = thread_event_id {
                                                                        state.write().right_panel = crate::state::app_state::RightPanelView::Thread(eid.clone());
                                                                    }
                                                                },
                                                                "💬 Reply in Thread"
                                                            }
                                                        }
                                                    }
                                                    // Pin/Unpin message
                                                    {
                                                        let pin_room_id = room_id.clone();
                                                        let pin_event_id = event_id.clone();
                                                        rsx! {
                                                            button {
                                                                class: "event-tile__more-item",
                                                                onclick: move |_| {
                                                                    show_more_menu.set(false);
                                                                    if let Some(ref eid) = pin_event_id {
                                                                        let rid = pin_room_id.clone();
                                                                        let eid = eid.clone();
                                                                        spawn(async move {
                                                                            if let Err(e) = toggle_pin_message(state, &rid, &eid).await {
                                                                                tracing::error!("Failed to toggle pin: {e}");
                                                                            }
                                                                        });
                                                                    }
                                                                },
                                                                "📌 Pin/Unpin"
                                                            }
                                                        }
                                                    }
                                                    // View Source - available for all messages
                                                    button {
                                                        class: "event-tile__more-item",
                                                        onclick: move |_| {
                                                            show_more_menu.set(false);
                                                            show_source.set(true);
                                                        },
                                                        "{{ }} View Source"
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        // Quick reaction picker
                        if *show_react_picker.read() {
                            if let Some(ref eid) = react_event_id {
                                div {
                                    class: "event-tile__react-picker",
                                    for emoji in QUICK_REACTIONS.iter() {
                                        {
                                            let emoji_str = emoji.to_string();
                                            let rid = react_room_id.clone();
                                            let eid = eid.clone();
                                            rsx! {
                                                button {
                                                    class: "event-tile__react-emoji",
                                                    title: "React with {emoji_str}",
                                                    onclick: move |_| {
                                                        let rid = rid.clone();
                                                        let eid = eid.clone();
                                                        let emoji = emoji_str.clone();
                                                        show_react_picker.set(false);
                                                        spawn(async move {
                                                            if let Err(e) = send_reaction(state, &rid, &eid, &emoji).await {
                                                                tracing::error!("Failed to send reaction: {e}");
                                                            }
                                                        });
                                                    },
                                                    "{emoji_str}"
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // View Source modal (with raw JSON from SDK)
            if *show_source.read() {
                {
                    let src_room_id = room_id.clone();
                    let src_event_id = event_id.clone();
                    let src_event_display = src_event_id.clone().unwrap_or_else(|| "(pending)".to_string());
                    // Load raw JSON from SDK if we haven't yet
                    if source_json.read().is_empty() {
                        if let Some(ref eid) = src_event_id {
                            let eid = eid.clone();
                            let rid = src_room_id.clone();
                            spawn(async move {
                                let client = { state.read().client.clone() };
                                if let Some(client) = client {
                                    if let Ok(room_id) = OwnedRoomId::try_from(rid.as_str()) {
                                        if let Some(room) = client.get_room(&room_id) {
                                            if let Ok(event_id) = OwnedEventId::try_from(eid.as_str()) {
                                                match room.event(&event_id, None).await {
                                                    Ok(timeline_event) => {
                                                        let raw = timeline_event.raw();
                                                        if let Ok(val) = raw.deserialize_as::<serde_json::Value>() {
                                                            if let Ok(pretty) = serde_json::to_string_pretty(&val) {
                                                                source_json.set(pretty);
                                                            }
                                                        }
                                                    }
                                                    Err(e) => {
                                                        source_json.set(format!("Failed to fetch event: {e}"));
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            });
                        }
                    }
                    let json_text = source_json.read().clone();
                    let display_text = if json_text.is_empty() { source_text.clone() } else { json_text };
                    let copy_text = display_text.clone();
                    rsx! {
                        Modal {
                            title: "Event Source".to_string(),
                            on_close: move |_| {
                                show_source.set(false);
                                source_json.set(String::new());
                            },
                            wide: true,
                            div {
                                class: "event-source",
                                div {
                                    class: "event-source__header",
                                    span {
                                        class: "event-source__event-id",
                                        "Event: {src_event_display}"
                                    }
                                    button {
                                        class: "btn btn--secondary btn--sm",
                                        onclick: move |_| {
                                            let t = copy_text.clone();
                                            spawn(async move {
                                                let _ = dioxus::prelude::document::eval(
                                                    &format!("navigator.clipboard.writeText({})", serde_json::to_string(&t).unwrap_or_default()),
                                                );
                                            });
                                        },
                                        "Copy JSON"
                                    }
                                }
                                pre {
                                    class: "event-source__content",
                                    "{display_text}"
                                }
                            }
                        }
                    }
                }
            }

            // Forward message dialog
            if *show_forward.read() {
                ForwardDialog {
                    message_body: content.body_text(),
                    on_close: move |_| show_forward.set(false),
                }
            }

            // Edit history modal (#46)
            if *show_edit_history.read() {
                if let Some(ref eid) = event_id {
                    EditHistoryModal {
                        room_id: room_id.clone(),
                        event_id: eid.clone(),
                        on_close: move |_| show_edit_history.set(false),
                    }
                }
            }

            // Report dialog
            if *show_report.read() {
                if let Some(ref eid) = event_id {
                    crate::room::report_dialog::ReportDialog {
                        room_id: room_id.clone(),
                        event_id: eid.clone(),
                        on_close: move |_| show_report.set(false),
                    }
                }
            }

            // Right-click context menu overlay
            if *show_context_menu.read() {
                {
                    let (cx, cy) = *context_menu_pos.read();
                    let ctx_room_id = room_id.clone();
                    let ctx_event_id = event_id.clone();
                    let ctx_body = content.body_text();
                    let ctx_sender = sender_display_name.clone();
                    rsx! {
                        div {
                            class: "context-menu-overlay",
                            onclick: move |_| show_context_menu.set(false),
                            div {
                                class: "context-menu",
                                style: "position: fixed; left: {cx}px; top: {cy}px;",
                                onclick: move |evt| evt.stop_propagation(),

                                // Copy text
                                {
                                    let body_for_copy = ctx_body.clone();
                                    rsx! {
                                        button {
                                            class: "context-menu__item",
                                            onclick: move |_| {
                                                show_context_menu.set(false);
                                                let t = body_for_copy.clone();
                                                spawn(async move {
                                                    let _ = dioxus::prelude::document::eval(
                                                        &format!("navigator.clipboard.writeText({})", serde_json::to_string(&t).unwrap_or_default()),
                                                    );
                                                });
                                            },
                                            "📋 Copy Text"
                                        }
                                    }
                                }

                                // Copy event ID
                                if let Some(ref eid) = ctx_event_id {
                                    {
                                        let eid_copy = eid.clone();
                                        rsx! {
                                            button {
                                                class: "context-menu__item",
                                                onclick: move |_| {
                                                    show_context_menu.set(false);
                                                    let t = eid_copy.clone();
                                                    spawn(async move {
                                                        let _ = dioxus::prelude::document::eval(
                                                            &format!("navigator.clipboard.writeText({})", serde_json::to_string(&t).unwrap_or_default()),
                                                        );
                                                    });
                                                },
                                                "🔗 Copy Event ID"
                                            }
                                        }
                                    }
                                }

                                // Reply
                                {
                                    let reply_eid = ctx_event_id.clone();
                                    let reply_room = ctx_room_id.clone();
                                    let reply_sender = ctx_sender.clone();
                                    let reply_body = ctx_body.clone();
                                    rsx! {
                                        button {
                                            class: "context-menu__item",
                                            onclick: move |_| {
                                                show_context_menu.set(false);
                                                if let Some(ref eid) = reply_eid {
                                                    if let Ok(event_id) = OwnedEventId::try_from(eid.as_str()) {
                                                        if let Ok(room_id) = OwnedRoomId::try_from(reply_room.as_str()) {
                                                            state.write().replying_to = Some(ReplyingTo {
                                                                event_id,
                                                                sender_name: reply_sender.clone(),
                                                                body: reply_body.clone(),
                                                                room_id,
                                                            });
                                                        }
                                                    }
                                                }
                                            },
                                            "↩ Reply"
                                        }
                                    }
                                }

                                // Edit (own messages only)
                                if is_own_message {
                                    {
                                        let edit_eid = ctx_event_id.clone();
                                        let edit_room = ctx_room_id.clone();
                                        let edit_body = ctx_body.clone();
                                        rsx! {
                                            button {
                                                class: "context-menu__item",
                                                onclick: move |_| {
                                                    show_context_menu.set(false);
                                                    if let Some(ref eid) = edit_eid {
                                                        if let Ok(event_id) = OwnedEventId::try_from(eid.as_str()) {
                                                            if let Ok(room_id) = OwnedRoomId::try_from(edit_room.as_str()) {
                                                                let mut s = state.write();
                                                                s.replying_to = None;
                                                                s.editing_message = Some(EditingMessage {
                                                                    event_id,
                                                                    room_id,
                                                                    original_body: edit_body.clone(),
                                                                });
                                                            }
                                                        }
                                                    }
                                                },
                                                "✏ Edit"
                                            }
                                        }
                                    }
                                }

                                // Forward
                                button {
                                    class: "context-menu__item",
                                    onclick: move |_| {
                                        show_context_menu.set(false);
                                        show_forward.set(true);
                                    },
                                    "↪ Forward"
                                }

                                // Pin/Unpin
                                {
                                    let pin_room = ctx_room_id.clone();
                                    let pin_eid = ctx_event_id.clone();
                                    rsx! {
                                        button {
                                            class: "context-menu__item",
                                            onclick: move |_| {
                                                show_context_menu.set(false);
                                                if let Some(ref eid) = pin_eid {
                                                    let rid = pin_room.clone();
                                                    let eid = eid.clone();
                                                    spawn(async move {
                                                        if let Err(e) = toggle_pin_message(state, &rid, &eid).await {
                                                            tracing::error!("Failed to toggle pin: {e}");
                                                        }
                                                    });
                                                }
                                            },
                                            "📌 Pin/Unpin"
                                        }
                                    }
                                }

                                // Copy Permalink
                                {
                                    let permalink_eid = ctx_event_id.clone();
                                    let permalink_rid = ctx_room_id.clone();
                                    rsx! {
                                        button {
                                            class: "context-menu__item",
                                            onclick: move |_| {
                                                show_context_menu.set(false);
                                                if let Some(ref eid) = permalink_eid {
                                                    let link = format!("https://matrix.to/#/{}/{}",
                                                        permalink_rid, eid);
                                                    let link_clone = link.clone();
                                                    spawn(async move {
                                                        let _ = dioxus::prelude::document::eval(
                                                            &format!("navigator.clipboard.writeText({})",
                                                                serde_json::to_string(&link_clone).unwrap_or_default()),
                                                        );
                                                    });
                                                }
                                            },
                                            "🔗 Copy Permalink"
                                        }
                                    }
                                }

                                // Report
                                if !is_own_message {
                                    button {
                                        class: "context-menu__item",
                                        onclick: move |_| {
                                            show_context_menu.set(false);
                                            show_report.set(true);
                                        },
                                        "⚠ Report"
                                    }
                                }

                                // View Source
                                button {
                                    class: "context-menu__item",
                                    onclick: move |_| {
                                        show_context_menu.set(false);
                                        show_source.set(true);
                                    },
                                    "{{ }} View Source"
                                }

                                // Delete (own messages only)
                                if is_own_message {
                                    {
                                        let del_eid = ctx_event_id.clone();
                                        let del_room = ctx_room_id.clone();
                                        rsx! {
                                            div { class: "context-menu__separator" }
                                            button {
                                                class: "context-menu__item context-menu__item--danger",
                                                onclick: move |_| {
                                                    show_context_menu.set(false);
                                                    if let Some(ref eid) = del_eid {
                                                        let rid = del_room.clone();
                                                        let eid = eid.clone();
                                                        spawn(async move {
                                                            if let Err(e) = redact_message(state, &rid, &eid).await {
                                                                tracing::error!("Failed to delete: {e}");
                                                            }
                                                        });
                                                    }
                                                },
                                                "🗑 Delete"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}