diff --git a/src/components/molecules/guest.rs b/src/components/molecules/guest.rs new file mode 100644 index 0000000..249f8bb --- /dev/null +++ b/src/components/molecules/guest.rs @@ -0,0 +1,31 @@ +use dioxus::prelude::*; + +use crate::components::atoms::Button; + +#[derive(Props)] +pub struct GuestProps<'a> { + description: &'a str, + cta: &'a str, + on_click: EventHandler<'a, MouseEvent>, +} + +pub fn Guest<'a>(cx: Scope<'a, GuestProps<'a>>) -> Element<'a> { + render!(rsx!( + section { + class: "guest", + span { + "{cx.props.description}", + } + div { + class: "guest__cta", + Button { + text: "{cx.props.cta}", + on_click: move |event| { + cx.props.on_click.call(event) + }, + status: None + } + } + } + )) +} diff --git a/src/components/molecules/mod.rs b/src/components/molecules/mod.rs index 40220b0..5d3ec7c 100644 --- a/src/components/molecules/mod.rs +++ b/src/components/molecules/mod.rs @@ -1,4 +1,5 @@ pub mod attach_preview; +pub mod guest; pub mod input_message; pub mod list; pub mod menu; @@ -6,6 +7,7 @@ pub mod modal; pub mod rooms; pub use attach_preview::AttachPreview; +pub use guest::Guest; pub use input_message::InputMessage; pub use list::List; pub use menu::Menu; diff --git a/src/pages/chat/room/group.rs b/src/pages/chat/room/group.rs index f03bd1a..5978a90 100644 --- a/src/pages/chat/room/group.rs +++ b/src/pages/chat/room/group.rs @@ -11,16 +11,17 @@ use crate::{ attach::AttachType, button::Variant, Attach, Avatar, Button, Close, Header, Icon, MessageInput, RoomView, }, - molecules::rooms::CurrentRoom, + molecules::{rooms::CurrentRoom, Guest}, }, hooks::{ use_attach::{use_attach, AttachError, AttachFile}, - use_client::{use_client, UseClientState}, + use_client::use_client, use_notification::use_notification, use_room::use_room, + use_session::use_session, }, pages::chat::room::new::CreationStatus, - services::matrix::matrix::create_room, + services::matrix::matrix::{create_room, find_user_by_id}, utils::{ i18n_get_key_value::i18n_get_key_value, matrix::{mxc_to_thumbnail_uri, ImageMethod, ImageSize}, @@ -74,6 +75,9 @@ pub fn RoomGroup(cx: Scope) -> Element { let key_input_message_file_type = translate!(i18, "chat.input_message.file_type"); let key_input_message_not_found = translate!(i18, "chat.input_message.not_found"); + let key_chat_guest_signup_description = translate!(i18, "chat.guest.signup.description"); + let key_chat_guest_signup_cta = translate!(i18, "chat.guest.signup.cta"); + let i18n_map = HashMap::from([ (key_group_title, translate!(i18, "group.title")), ( @@ -109,6 +113,7 @@ pub fn RoomGroup(cx: Scope) -> Element { let attach = use_attach(cx); let notification = use_notification(cx); let room = use_room(cx); + let session = use_session(cx); let selected_users = use_shared_state::(cx).expect("Unable to use SelectedProfile"); @@ -130,7 +135,7 @@ pub fn RoomGroup(cx: Scope) -> Element { let element = users.read().clone().into_iter().find(|u| u.id.eq(&id)); if let None = element { - match process_find_user_by_id(&id, &client).await { + match find_user_by_id(&id, &client.get()).await { Ok(profile) => users.with_mut(|user| user.push(profile)), Err(_) => { notification.handle_error(&key_group_error_not_found); @@ -414,25 +419,35 @@ pub fn RoomGroup(cx: Scope) -> Element { } ) } else { - rsx!( - div { - class: "group__cta__wrapper row", - Button { - text: "{i18n_get_key_value(&i18n_map, key_group_meta_cta_back)}", - status: None, - variant: &Variant::Secondary, - on_click: move |_| { - handle_complete_group.set(false) - } + if session.is_guest() { + rsx!( + Guest { + description: "{key_chat_guest_signup_description}", + cta: "{key_chat_guest_signup_cta}", + on_click: move |_| {} } - Button { - text: "{i18n_get_key_value(&i18n_map, key_group_meta_cta_create)}", - status: None, - disabled: group_name.get().len() == 0, - on_click: on_handle_create + ) + } else { + rsx!( + div { + class: "group__cta__wrapper row", + Button { + text: "{i18n_get_key_value(&i18n_map, key_group_meta_cta_back)}", + status: None, + variant: &Variant::Secondary, + on_click: move |_| { + handle_complete_group.set(false) + } + } + Button { + text: "{i18n_get_key_value(&i18n_map, key_group_meta_cta_create)}", + status: None, + disabled: group_name.get().len() == 0, + on_click: on_handle_create + } } - } - ) + ) + } } ) } else { @@ -493,11 +508,12 @@ pub fn RoomGroup(cx: Scope) -> Element { ) }) } + div { class: "group__cta__wrapper", Button { text: "{i18n_get_key_value(&i18n_map, key_group_select_cta)}", - disabled: if selected_users.read().profiles.len() == 0 { true } else { false }, + disabled: selected_users.read().profiles.is_empty(), status: None, on_click: move |_| { handle_complete_group.set(true) @@ -506,48 +522,6 @@ pub fn RoomGroup(cx: Scope) -> Element { } ) } - } -} -pub(crate) async fn process_find_user_by_id( - id: &str, - client: &UseClientState, -) -> Result { - let u = UserId::parse(&id).map_err(|_| CreateRoomError::InvalidUserId)?; - - let u = u.deref(); - - let request = matrix_sdk::ruma::api::client::profile::get_profile::v3::Request::new(u); - - let response = client - .get() - .send(request, None) - .await - .map_err(|_| CreateRoomError::UserNotFound)?; - - let displayname = response - .displayname - .ok_or(CreateRoomError::InvalidUsername)?; - - let avatar_uri = response - .avatar_url - .map(|uri| { - mxc_to_thumbnail_uri( - &uri, - ImageSize { - width: 48, - height: 48, - }, - ImageMethod::CROP, - ) - }) - .flatten(); - - let profile = Profile { - displayname, - avatar_uri, - id: id.to_string(), - }; - - Ok(profile) + } } diff --git a/src/pages/chat/room/new.rs b/src/pages/chat/room/new.rs index c5ef45f..798c30a 100644 --- a/src/pages/chat/room/new.rs +++ b/src/pages/chat/room/new.rs @@ -8,13 +8,13 @@ use matrix_sdk::ruma::UserId; use crate::{ components::{ atoms::{button::Variant, Button, Header, MessageInput, RoomView}, - molecules::rooms::CurrentRoom, + molecules::{rooms::CurrentRoom, Guest}, }, hooks::{ - use_client::use_client, use_notification::use_notification, use_room::use_room, + use_auth::use_auth, use_client::use_client, use_init_app::BeforeSession, use_notification::use_notification, use_room::use_room, use_session::use_session }, - pages::chat::room::group::{self, CreateRoomError, Profile}, - services::matrix::matrix::create_room, + pages::chat::room::group::{CreateRoomError, Profile}, + services::matrix::matrix::{create_room, find_user_by_id}, utils::{i18n_get_key_value::i18n_get_key_value, sync_room::sync_created_room}, }; use futures_util::{StreamExt, TryFutureExt}; @@ -35,6 +35,9 @@ pub fn RoomNew(cx: Scope) -> Element { let key_dm_error_not_found = translate!(i18, "dm.error.not_found"); let key_dm_error_profile = translate!(i18, "dm.error.profile"); + let key_chat_guest_signup_description = translate!(i18, "chat.guest.signup.description"); + let key_chat_guest_signup_cta = translate!(i18, "chat.guest.signup.cta"); + let key_dm_title = "dm-title"; let key_dm_label = "dm-label"; let key_dm_placeholder = "dm-placeholder"; @@ -51,8 +54,13 @@ pub fn RoomNew(cx: Scope) -> Element { let client = use_client(cx); let notification = use_notification(cx); let room = use_room(cx); + let session = use_session(cx); + let auth = use_auth(cx); + + let before_session = + use_shared_state::(cx).expect("Unable to use before session"); - let user_id = use_state::(cx, || String::from("")); + let user_id = use_state::(cx, || String::from("@edith-test-1:matrix.org")); let user = use_state::>(cx, || None); let error_field = use_state::>(cx, || None); let status = use_state::(cx, || CreationStatus::Start); @@ -62,7 +70,7 @@ pub fn RoomNew(cx: Scope) -> Element { async move { while let Some(id) = rx.next().await { - match group::process_find_user_by_id(&id, &client).await { + match find_user_by_id(&id, &client.get()).await { Ok(profile) => user.set(Some(profile)), Err(_) => { notification.handle_error(&key_dm_error_not_found); @@ -73,6 +81,10 @@ pub fn RoomNew(cx: Scope) -> Element { }); let on_handle_create = move |_| { + if session.is_guest() { + return; + } + cx.spawn({ to_owned![ client, @@ -172,6 +184,18 @@ pub fn RoomNew(cx: Scope) -> Element { } ) } + if session.is_guest() { + rsx!( + Guest { + description: "{key_chat_guest_signup_description}", + cta: "{key_chat_guest_signup_cta}", + on_click: move |_| { + auth.set_logged_in(false); + *before_session.write() = BeforeSession::Signup; + } + } + ) + } match status.get() { CreationStatus::Creating => { render!(rsx! {