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
use dioxus::prelude::*;

use crate::components::modal::Modal;
use crate::state::app_state::AppState;

/// History visibility options.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum HistoryVisibility {
    WorldReadable,
    Shared,
    Invited,
    Joined,
}

/// Guest access options.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum GuestAccess {
    CanJoin,
    Forbidden,
}

/// Join rule options.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum JoinRule {
    Public,
    Invite,
    Knock,
    Restricted,
}

/// Room permission action with power level requirement.
#[derive(Clone, Debug, PartialEq)]
pub struct PermissionAction {
    pub name: String,
    pub description: String,
    pub level: i64,
}

/// Comprehensive Room Settings Dialog.
#[component]
pub fn RoomSettingsDialog(room_id: String, on_close: EventHandler<()>) -> Element {
    let state = use_context::<Signal<AppState>>();
    let mut active_tab = use_signal(|| SettingsTab::General);
    let mut room_name = use_signal(|| String::new());
    let mut room_topic = use_signal(|| String::new());
    let mut history_visibility = use_signal(|| HistoryVisibility::Shared);
    let mut guest_access = use_signal(|| GuestAccess::Forbidden);
    let mut join_rule = use_signal(|| JoinRule::Invite);
    let mut room_addresses = use_signal(Vec::<String>::new);
    let mut new_address = use_signal(|| String::new());
    let mut permissions = use_signal(Vec::<PermissionAction>::new);
    let mut loading = use_signal(|| true);
    let mut saving = use_signal(|| false);
    let mut error = use_signal(|| Option::<String>::None);
    let mut success = use_signal(|| Option::<String>::None);

    // Load room info
    let rid = room_id.clone();
    use_effect(move || {
        let rid = rid.clone();
        spawn(async move {
            let client = { state.read().client.clone() };
            if let Some(client) = client {
                if let Ok(room_id) = matrix_sdk::ruma::OwnedRoomId::try_from(rid.as_str()) {
                    if let Some(room) = client.get_room(&room_id) {
                        if let Ok(name) = room.display_name().await {
                            room_name.set(name.to_string());
                        }
                        if let Some(topic) = room.topic() {
                            room_topic.set(topic);
                        }
                        // Initialize default permissions
                        permissions.set(vec![
                            PermissionAction { name: "Send messages".into(), description: "Send text messages".into(), level: 0 },
                            PermissionAction { name: "Send reactions".into(), description: "React to messages".into(), level: 0 },
                            PermissionAction { name: "Send media".into(), description: "Upload images and files".into(), level: 0 },
                            PermissionAction { name: "Invite users".into(), description: "Invite people to this room".into(), level: 0 },
                            PermissionAction { name: "Kick users".into(), description: "Remove users from the room".into(), level: 50 },
                            PermissionAction { name: "Ban users".into(), description: "Permanently ban users".into(), level: 50 },
                            PermissionAction { name: "Redact messages".into(), description: "Delete others' messages".into(), level: 50 },
                            PermissionAction { name: "Send state events".into(), description: "Modify room state".into(), level: 50 },
                            PermissionAction { name: "Change room name".into(), description: "Set the room name".into(), level: 50 },
                            PermissionAction { name: "Change room topic".into(), description: "Set the room topic".into(), level: 50 },
                            PermissionAction { name: "Change room avatar".into(), description: "Set the room avatar".into(), level: 50 },
                            PermissionAction { name: "Change history visibility".into(), description: "Who can see history".into(), level: 100 },
                            PermissionAction { name: "Enable encryption".into(), description: "Enable E2E encryption".into(), level: 100 },
                            PermissionAction { name: "Modify widgets".into(), description: "Add or remove widgets".into(), level: 50 },
                            PermissionAction { name: "Pin messages".into(), description: "Pin/unpin messages".into(), level: 50 },
                            PermissionAction { name: "Upgrade room".into(), description: "Upgrade to new room version".into(), level: 100 },
                            PermissionAction { name: "Change permissions".into(), description: "Modify power levels".into(), level: 100 },
                            PermissionAction { name: "Change join rules".into(), description: "Who can join the room".into(), level: 100 },
                            PermissionAction { name: "Change guest access".into(), description: "Allow or deny guests".into(), level: 100 },
                            PermissionAction { name: "Change server ACLs".into(), description: "Server access control".into(), level: 100 },
                            PermissionAction { name: "Send @room notifications".into(), description: "Notify all members".into(), level: 50 },
                            PermissionAction { name: "Change room address".into(), description: "Set room aliases".into(), level: 50 },
                            PermissionAction { name: "Manage integrations".into(), description: "Add bots and integrations".into(), level: 50 },
                            PermissionAction { name: "Set space children".into(), description: "Add rooms to space".into(), level: 50 },
                        ]);
                    }
                }
            }
            loading.set(false);
        });
    });

    let on_save = {
        let rid = room_id.clone();
        move |_| {
            saving.set(true);
            error.set(None);
            success.set(None);
            let rid = rid.clone();
            let name = room_name.read().clone();
            let topic = room_topic.read().clone();
            spawn(async move {
                let client = { state.read().client.clone() };
                if let Some(client) = client {
                    if let Ok(room_id) = matrix_sdk::ruma::OwnedRoomId::try_from(rid.as_str()) {
                        if let Some(room) = client.get_room(&room_id) {
                            // Save room name
                            let name_content = matrix_sdk::ruma::events::room::name::RoomNameEventContent::new(name);
                            if let Err(e) = room.send_state_event(name_content).await {
                                error.set(Some(format!("Failed to save name: {e}")));
                                saving.set(false);
                                return;
                            }
                            // Save topic
                            let topic_content = matrix_sdk::ruma::events::room::topic::RoomTopicEventContent::new(topic);
                            if let Err(e) = room.send_state_event(topic_content).await {
                                error.set(Some(format!("Failed to save topic: {e}")));
                                saving.set(false);
                                return;
                            }
                            success.set(Some("Settings saved successfully".to_string()));
                        }
                    }
                }
                saving.set(false);
            });
        }
    };

    rsx! {
        Modal {
            title: "Room Settings".to_string(),
            on_close: move |_| on_close.call(()),
            wide: true,

            div {
                class: "room-settings",

                // Tab bar
                div {
                    class: "room-settings__tabs",
                    button {
                        class: if *active_tab.read() == SettingsTab::General { "room-settings__tab room-settings__tab--active" } else { "room-settings__tab" },
                        onclick: move |_| active_tab.set(SettingsTab::General),
                        "General"
                    }
                    button {
                        class: if *active_tab.read() == SettingsTab::Security { "room-settings__tab room-settings__tab--active" } else { "room-settings__tab" },
                        onclick: move |_| active_tab.set(SettingsTab::Security),
                        "Security"
                    }
                    button {
                        class: if *active_tab.read() == SettingsTab::Roles { "room-settings__tab room-settings__tab--active" } else { "room-settings__tab" },
                        onclick: move |_| active_tab.set(SettingsTab::Roles),
                        "Roles & Permissions"
                    }
                    button {
                        class: if *active_tab.read() == SettingsTab::Advanced { "room-settings__tab room-settings__tab--active" } else { "room-settings__tab" },
                        onclick: move |_| active_tab.set(SettingsTab::Advanced),
                        "Advanced"
                    }
                }

                if let Some(ref err) = *error.read() {
                    div { class: "room-settings__error", "{err}" }
                }
                if let Some(ref msg) = *success.read() {
                    div { class: "room-settings__success", "{msg}" }
                }

                // General tab
                if *active_tab.read() == SettingsTab::General {
                    div {
                        class: "room-settings__section",
                        div {
                            class: "room-settings__field",
                            label { "Room Name" }
                            input {
                                r#type: "text",
                                class: "settings-input",
                                value: "{room_name}",
                                oninput: move |evt| room_name.set(evt.value()),
                            }
                        }
                        div {
                            class: "room-settings__field",
                            label { "Room Topic" }
                            textarea {
                                class: "settings-textarea",
                                rows: "3",
                                value: "{room_topic}",
                                oninput: move |evt| room_topic.set(evt.value()),
                            }
                        }

                        h4 { "Room Addresses" }
                        div {
                            class: "room-settings__addresses",
                            for addr in room_addresses.read().iter() {
                                {
                                    let a = addr.clone();
                                    rsx! {
                                        div {
                                            class: "room-settings__address",
                                            span { "{a}" }
                                            button {
                                                class: "btn btn--sm btn--danger",
                                                onclick: move |_| {
                                                    room_addresses.write().retain(|x| x != &a);
                                                },
                                                "Remove"
                                            }
                                        }
                                    }
                                }
                            }
                            div {
                                class: "room-settings__address-add",
                                input {
                                    r#type: "text",
                                    class: "settings-input",
                                    placeholder: "#alias:server.com",
                                    value: "{new_address}",
                                    oninput: move |evt| new_address.set(evt.value()),
                                }
                                button {
                                    class: "btn btn--secondary btn--sm",
                                    onclick: move |_| {
                                        let addr = new_address.read().trim().to_string();
                                        if !addr.is_empty() {
                                            room_addresses.write().push(addr);
                                            new_address.set(String::new());
                                        }
                                    },
                                    "Add"
                                }
                            }
                        }

                        div {
                            class: "room-settings__actions",
                            button {
                                class: "btn btn--primary",
                                disabled: *saving.read(),
                                onclick: on_save,
                                if *saving.read() { "Saving..." } else { "Save Changes" }
                            }
                        }
                    }
                }

                // Security & Privacy tab
                if *active_tab.read() == SettingsTab::Security {
                    div {
                        class: "room-settings__section",

                        h4 { "Join Rules" }
                        div {
                            class: "room-settings__radio-group",
                            label {
                                input { r#type: "radio", name: "join_rule", checked: *join_rule.read() == JoinRule::Invite, oninput: move |_| join_rule.set(JoinRule::Invite) }
                                div {
                                    strong { "Private (Invite Only)" }
                                    p { "Only invited users can join" }
                                }
                            }
                            label {
                                input { r#type: "radio", name: "join_rule", checked: *join_rule.read() == JoinRule::Public, oninput: move |_| join_rule.set(JoinRule::Public) }
                                div {
                                    strong { "Public" }
                                    p { "Anyone can find and join this room" }
                                }
                            }
                            label {
                                input { r#type: "radio", name: "join_rule", checked: *join_rule.read() == JoinRule::Knock, oninput: move |_| join_rule.set(JoinRule::Knock) }
                                div {
                                    strong { "Knock (Ask to Join)" }
                                    p { "Users can request to join and must be approved" }
                                }
                            }
                            label {
                                input { r#type: "radio", name: "join_rule", checked: *join_rule.read() == JoinRule::Restricted, oninput: move |_| join_rule.set(JoinRule::Restricted) }
                                div {
                                    strong { "Space Members" }
                                    p { "Only members of a parent space can join" }
                                }
                            }
                        }

                        h4 { "Guest Access" }
                        div {
                            class: "room-settings__radio-group",
                            label {
                                input { r#type: "radio", name: "guest", checked: *guest_access.read() == GuestAccess::Forbidden, oninput: move |_| guest_access.set(GuestAccess::Forbidden) }
                                div {
                                    strong { "Forbidden" }
                                    p { "Guests cannot join this room" }
                                }
                            }
                            label {
                                input { r#type: "radio", name: "guest", checked: *guest_access.read() == GuestAccess::CanJoin, oninput: move |_| guest_access.set(GuestAccess::CanJoin) }
                                div {
                                    strong { "Can Join" }
                                    p { "Guest accounts can join (note: guests cannot decrypt E2EE)" }
                                }
                            }
                        }

                        h4 { "Who can read history?" }
                        div {
                            class: "room-settings__radio-group",
                            label {
                                input { r#type: "radio", name: "history", checked: *history_visibility.read() == HistoryVisibility::WorldReadable, oninput: move |_| history_visibility.set(HistoryVisibility::WorldReadable) }
                                div {
                                    strong { "Anyone" }
                                    p { "Anyone, including guests, can read the full history" }
                                }
                            }
                            label {
                                input { r#type: "radio", name: "history", checked: *history_visibility.read() == HistoryVisibility::Shared, oninput: move |_| history_visibility.set(HistoryVisibility::Shared) }
                                div {
                                    strong { "Members (full history)" }
                                    p { "New members can see all past messages" }
                                }
                            }
                            label {
                                input { r#type: "radio", name: "history", checked: *history_visibility.read() == HistoryVisibility::Invited, oninput: move |_| history_visibility.set(HistoryVisibility::Invited) }
                                div {
                                    strong { "Members (since invited)" }
                                    p { "New members see messages from their invite onwards" }
                                }
                            }
                            label {
                                input { r#type: "radio", name: "history", checked: *history_visibility.read() == HistoryVisibility::Joined, oninput: move |_| history_visibility.set(HistoryVisibility::Joined) }
                                div {
                                    strong { "Members (since joined)" }
                                    p { "New members see messages only from when they joined" }
                                }
                            }
                        }
                    }
                }

                // Roles & Permissions tab
                if *active_tab.read() == SettingsTab::Roles {
                    div {
                        class: "room-settings__section",

                        h4 { "Power Levels" }
                        p { class: "room-settings__desc", "Admin = 100, Moderator = 50, Default = 0" }

                        div {
                            class: "room-settings__permissions",
                            for perm in permissions.read().iter() {
                                {
                                    let name = perm.name.clone();
                                    let desc = perm.description.clone();
                                    let level = perm.level;
                                    let role = match level {
                                        100 => "Admin",
                                        50 => "Moderator",
                                        _ => "Default",
                                    };
                                    rsx! {
                                        div {
                                            class: "room-settings__permission",
                                            div {
                                                class: "room-settings__permission-info",
                                                span { class: "room-settings__permission-name", "{name}" }
                                                span { class: "room-settings__permission-desc", "{desc}" }
                                            }
                                            span { class: "room-settings__permission-level", "{role} ({level})" }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // Advanced tab
                if *active_tab.read() == SettingsTab::Advanced {
                    div {
                        class: "room-settings__section",
                        h4 { "Room Information" }
                        div {
                            class: "room-settings__info-row",
                            span { class: "room-settings__info-label", "Internal Room ID:" }
                            code { class: "room-settings__info-value", "{room_id}" }
                        }

                        h4 { "Room Upgrade" }
                        p { class: "room-settings__desc", "Upgrade this room to a newer room version. This creates a new room and archives the old one." }
                        button {
                            class: "btn btn--secondary",
                            onclick: move |_| {
                                tracing::info!("Room upgrade requested for {}", room_id);
                            },
                            "Upgrade Room"
                        }
                    }
                }
            }
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
enum SettingsTab {
    General,
    Security,
    Roles,
    Advanced,
}