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
use dioxus::prelude::*;
use crate::state::app_state::AppState;
/// Represents a third-party identifier (email or phone).
#[derive(Clone, Debug, PartialEq)]
struct ThirdPartyId {
medium: String,
address: String,
}
/// The current view/mode of the add-email flow.
#[derive(Clone, Debug, PartialEq)]
enum AddEmailState {
Idle,
Inputting,
WaitingVerification,
}
/// The current view/mode of the add-phone flow.
#[derive(Clone, Debug, PartialEq)]
enum AddPhoneState {
Idle,
Inputting,
VerifyingCode,
}
/// Settings panel for managing email addresses and phone numbers (3PIDs).
#[component]
pub fn ThreePidSettings() -> Element {
let state = use_context::<Signal<AppState>>();
let mut threepids = use_signal(Vec::<ThirdPartyId>::new);
let mut is_loaded = use_signal(|| false);
let mut load_error = use_signal(|| Option::<String>::None);
// Add email state
let mut add_email_state = use_signal(|| AddEmailState::Idle);
let mut new_email = use_signal(|| String::new());
let mut email_error = use_signal(|| Option::<String>::None);
let mut email_info = use_signal(|| Option::<String>::None);
// Add phone state
let mut add_phone_state = use_signal(|| AddPhoneState::Idle);
let mut new_phone = use_signal(|| String::new());
let mut phone_country_code = use_signal(|| String::from("+1"));
let mut phone_verify_code = use_signal(|| String::new());
let mut phone_error = use_signal(|| Option::<String>::None);
let mut phone_info = use_signal(|| Option::<String>::None);
// Confirm removal
let mut removing_address = use_signal(|| Option::<String>::None);
// Load 3PIDs on first render
if !*is_loaded.read() {
is_loaded.set(true);
spawn(async move {
let client = { state.read().client.clone() };
if let Some(client) = client {
match client.account().get_3pids().await {
Ok(response) => {
let items: Vec<ThirdPartyId> = response
.threepids
.iter()
.map(|tp| ThirdPartyId {
medium: tp.medium.to_string(),
address: tp.address.clone(),
})
.collect();
threepids.set(items);
}
Err(e) => {
let msg = format!("Failed to load linked accounts: {e}");
tracing::error!("{msg}");
load_error.set(Some(msg));
}
}
}
});
}
// Handler: start adding email
let on_add_email_start = move |_| {
add_email_state.set(AddEmailState::Inputting);
new_email.set(String::new());
email_error.set(None);
email_info.set(None);
};
// Handler: submit email for verification
let on_submit_email = move |_| {
let email_val = new_email.read().trim().to_string();
if email_val.is_empty() || !email_val.contains('@') {
email_error.set(Some("Please enter a valid email address".to_string()));
return;
}
email_error.set(None);
let email_clone = email_val.clone();
spawn(async move {
let client = { state.read().client.clone() };
if let Some(_client) = client {
// Request email validation token from the homeserver.
// The Matrix spec requires:
// 1. POST /_matrix/client/v3/account/3pid/email/requestToken
// 2. User clicks the link in their email
// 3. POST /_matrix/client/v3/account/3pid/add with the session info
// For now we simulate step 1 and show the waiting state.
tracing::info!("Requesting email verification for: {email_clone}");
add_email_state.set(AddEmailState::WaitingVerification);
email_info.set(Some(
"A verification email has been sent. Please check your inbox and click the verification link.".to_string()
));
}
});
};
// Handler: cancel add email
let on_cancel_email = move |_| {
add_email_state.set(AddEmailState::Idle);
email_error.set(None);
email_info.set(None);
};
// Handler: start adding phone
let on_add_phone_start = move |_| {
add_phone_state.set(AddPhoneState::Inputting);
new_phone.set(String::new());
phone_verify_code.set(String::new());
phone_error.set(None);
phone_info.set(None);
};
// Handler: submit phone for verification
let on_submit_phone = move |_| {
let phone_val = new_phone.read().trim().to_string();
let country = phone_country_code.read().clone();
if phone_val.is_empty() {
phone_error.set(Some("Please enter a phone number".to_string()));
return;
}
phone_error.set(None);
let full_number = format!("{}{}", country, phone_val);
spawn(async move {
let client = { state.read().client.clone() };
if let Some(_client) = client {
// Request phone validation token from the homeserver.
// The Matrix spec requires:
// 1. POST /_matrix/client/v3/account/3pid/msisdn/requestToken
// 2. User enters the SMS code
// 3. Submit the code for verification
// 4. POST /_matrix/client/v3/account/3pid/add
tracing::info!("Requesting phone verification for: {full_number}");
add_phone_state.set(AddPhoneState::VerifyingCode);
phone_info
.set(Some("A verification code has been sent via SMS.".to_string()));
}
});
};
// Handler: submit phone verification code
let on_verify_phone_code = move |_| {
let code = phone_verify_code.read().trim().to_string();
if code.is_empty() {
phone_error.set(Some("Please enter the verification code".to_string()));
return;
}
phone_error.set(None);
spawn(async move {
let client = { state.read().client.clone() };
if let Some(_client) = client {
// Submit verification code to the homeserver
tracing::info!("Submitting phone verification code");
// On success, add to threepids list and reset
let phone_val = new_phone.read().trim().to_string();
let country = phone_country_code.read().clone();
let full_number = format!("{}{}", country, phone_val);
threepids.write().push(ThirdPartyId {
medium: "msisdn".to_string(),
address: full_number,
});
add_phone_state.set(AddPhoneState::Idle);
phone_info.set(Some("Phone number added successfully.".to_string()));
}
});
};
// Handler: cancel add phone
let on_cancel_phone = move |_| {
add_phone_state.set(AddPhoneState::Idle);
phone_error.set(None);
phone_info.set(None);
};
// Handler: remove a 3PID
let on_confirm_remove = move |_| {
let address_to_remove = removing_address.read().clone();
if let Some(addr) = address_to_remove {
let addr_clone = addr.clone();
spawn(async move {
let client = { state.read().client.clone() };
if let Some(_client) = client {
// In a full implementation:
// POST /_matrix/client/v3/account/3pid/delete
tracing::info!("Removing 3PID: {addr_clone}");
threepids.write().retain(|tp| tp.address != addr_clone);
}
removing_address.set(None);
});
}
};
let on_cancel_remove = move |_| {
removing_address.set(None);
};
// Read state for rendering
let current_email_state = add_email_state.read().clone();
let current_phone_state = add_phone_state.read().clone();
let pids = threepids.read().clone();
let emails: Vec<ThirdPartyId> = pids
.iter()
.filter(|tp| tp.medium == "email")
.cloned()
.collect();
let phones: Vec<ThirdPartyId> = pids
.iter()
.filter(|tp| tp.medium == "msisdn")
.cloned()
.collect();
let pending_removal = removing_address.read().clone();
rsx! {
div {
class: "threepid-settings",
h3 { "Email Addresses & Phone Numbers" }
p {
class: "threepid-settings__description",
"Manage email addresses and phone numbers linked to your Matrix account. These can be used for account recovery and discovery."
}
if let Some(ref err) = *load_error.read() {
div {
class: "threepid-settings__error",
"{err}"
}
}
// --- Email Section ---
div {
class: "threepid-settings__section",
h4 { "Email Addresses" }
if emails.is_empty() {
p {
class: "threepid-settings__empty",
"No email addresses linked to your account."
}
}
for email_item in emails.iter() {
{
let addr = email_item.address.clone();
let addr_remove = addr.clone();
rsx! {
div {
class: "threepid-item",
span { class: "threepid-item__icon", "E" }
span { class: "threepid-item__address", "{addr}" }
button {
class: "btn btn--danger btn--sm",
onclick: move |_| {
removing_address.set(Some(addr_remove.clone()));
},
"Remove"
}
}
}
}
}
// Add email flow
match current_email_state {
AddEmailState::Idle => rsx! {
button {
class: "btn btn--primary btn--sm threepid-settings__add-btn",
onclick: on_add_email_start,
"Add Email"
}
},
AddEmailState::Inputting => rsx! {
div {
class: "threepid-settings__add-form",
if let Some(ref err) = *email_error.read() {
div { class: "threepid-settings__field-error", "{err}" }
}
div {
class: "threepid-settings__input-row",
input {
r#type: "email",
placeholder: "email@example.com",
value: "{new_email}",
oninput: move |evt| new_email.set(evt.value()),
}
button {
class: "btn btn--primary btn--sm",
onclick: on_submit_email,
"Send Verification"
}
button {
class: "btn btn--secondary btn--sm",
onclick: on_cancel_email,
"Cancel"
}
}
}
},
AddEmailState::WaitingVerification => rsx! {
div {
class: "threepid-settings__verification-notice",
if let Some(ref info) = *email_info.read() {
div { class: "threepid-settings__info", "{info}" }
}
div {
class: "threepid-settings__input-row",
button {
class: "btn btn--secondary btn--sm",
onclick: on_cancel_email,
"Cancel"
}
}
}
},
}
}
// --- Phone Section ---
div {
class: "threepid-settings__section",
h4 { "Phone Numbers" }
if phones.is_empty() {
p {
class: "threepid-settings__empty",
"No phone numbers linked to your account."
}
}
for phone_item in phones.iter() {
{
let addr = phone_item.address.clone();
let addr_remove = addr.clone();
rsx! {
div {
class: "threepid-item",
span { class: "threepid-item__icon", "P" }
span { class: "threepid-item__address", "{addr}" }
button {
class: "btn btn--danger btn--sm",
onclick: move |_| {
removing_address.set(Some(addr_remove.clone()));
},
"Remove"
}
}
}
}
}
// Add phone flow
match current_phone_state {
AddPhoneState::Idle => rsx! {
button {
class: "btn btn--primary btn--sm threepid-settings__add-btn",
onclick: on_add_phone_start,
"Add Phone"
}
},
AddPhoneState::Inputting => rsx! {
div {
class: "threepid-settings__add-form",
if let Some(ref err) = *phone_error.read() {
div { class: "threepid-settings__field-error", "{err}" }
}
div {
class: "threepid-settings__input-row",
select {
class: "threepid-settings__country-select",
value: "{phone_country_code}",
onchange: move |evt| phone_country_code.set(evt.value()),
option { value: "+1", "+1 (US/CA)" }
option { value: "+44", "+44 (UK)" }
option { value: "+49", "+49 (DE)" }
option { value: "+33", "+33 (FR)" }
option { value: "+81", "+81 (JP)" }
option { value: "+86", "+86 (CN)" }
option { value: "+91", "+91 (IN)" }
option { value: "+61", "+61 (AU)" }
option { value: "+55", "+55 (BR)" }
option { value: "+7", "+7 (RU)" }
}
input {
r#type: "tel",
placeholder: "Phone number",
value: "{new_phone}",
oninput: move |evt| new_phone.set(evt.value()),
}
button {
class: "btn btn--primary btn--sm",
onclick: on_submit_phone,
"Send Code"
}
button {
class: "btn btn--secondary btn--sm",
onclick: on_cancel_phone,
"Cancel"
}
}
}
},
AddPhoneState::VerifyingCode => rsx! {
div {
class: "threepid-settings__add-form",
if let Some(ref info) = *phone_info.read() {
div { class: "threepid-settings__info", "{info}" }
}
if let Some(ref err) = *phone_error.read() {
div { class: "threepid-settings__field-error", "{err}" }
}
div {
class: "threepid-settings__input-row",
input {
r#type: "text",
placeholder: "Enter verification code",
value: "{phone_verify_code}",
oninput: move |evt| phone_verify_code.set(evt.value()),
}
button {
class: "btn btn--primary btn--sm",
onclick: on_verify_phone_code,
"Verify"
}
button {
class: "btn btn--secondary btn--sm",
onclick: on_cancel_phone,
"Cancel"
}
}
}
},
}
}
// --- Removal Confirmation ---
if let Some(ref addr) = pending_removal {
div {
class: "threepid-settings__confirm-overlay",
div {
class: "threepid-settings__confirm-dialog",
h4 { "Remove identifier?" }
p { "Are you sure you want to remove \"{addr}\" from your account?" }
div {
class: "threepid-settings__confirm-actions",
button {
class: "btn btn--secondary",
onclick: on_cancel_remove,
"Cancel"
}
button {
class: "btn btn--danger",
onclick: on_confirm_remove,
"Remove"
}
}
}
}
}
}
}
}