Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
Fix/warns (#59)
Browse files Browse the repository at this point in the history
* refactor(signups): remove panics

* chore: remove warns

* chore: use params var

* chore: use let Err insteand map_err

* refactor: manage Results

* refactor: use stored user_id

* style: format code

* chore: minor changes

* chore: improve feedback when setting server
  • Loading branch information
b-avb authored Mar 4, 2024
1 parent 48d1a6f commit e30a6c1
Show file tree
Hide file tree
Showing 29 changed files with 798 additions and 621 deletions.
2 changes: 1 addition & 1 deletion src/components/atoms/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct HelperData {
#[derive(Props)]
pub struct HelperProps<'a> {
helper: HelperData,
on_click: EventHandler<'a, MouseEvent>,
_on_click: EventHandler<'a, MouseEvent>,
}

pub fn Helper<'a>(cx: Scope<'a, HelperProps<'a>>) -> Element<'a> {
Expand Down
2 changes: 1 addition & 1 deletion src/components/atoms/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn MessageInput<'a>(cx: Scope<'a, MessageInputProps<'a>>) -> Element<'a> {
)
}
div {
class: "input-wrapper input_error_container",
class: "input-wrapper {input_error_container}",
match cx.props.itype {
InputType::Search => {
render!(
Expand Down
6 changes: 0 additions & 6 deletions src/components/atoms/messages/content/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ pub struct FileProps {
}

pub fn File<'a>(cx: Scope<'a, FileProps>) -> Element<'a> {
let message_reply = if cx.props.is_reply {
"message-reply__content--file"
} else {
""
};

cx.render(rsx!(
section {
class: "file",
Expand Down
16 changes: 5 additions & 11 deletions src/components/molecules/input_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use dioxus_std::{i18n::use_i18, translate};
use futures_util::TryFutureExt;

use crate::{
components::{atoms::{header_main::{HeaderEvent, HeaderCallOptions}, hover_menu::{MenuEvent, MenuOption}, input::InputType, message::MessageView, Attach, Button, Close, Icon, Message, TextareaInput
components::{atoms::{header_main::{HeaderEvent, HeaderCallOptions}, hover_menu::{MenuEvent, MenuOption}, message::MessageView, Attach, Button, Close, Icon, Message, TextareaInput
}, molecules::AttachPreview},
services::matrix::matrix::{TimelineMessageType, EventOrigin, Attachment}, hooks::{use_attach::{use_attach, AttachError, AttachFile}, use_client::use_client, use_notification::use_notification, use_reply::use_reply, use_room::use_room, use_send_attach::SendAttachStatus},
services::matrix::matrix::{TimelineMessageType, EventOrigin, Attachment}, hooks::{use_attach::{use_attach, AttachError, AttachFile}, use_notification::use_notification, use_reply::use_reply},
};

#[derive(Debug, Clone)]
Expand All @@ -26,7 +26,6 @@ pub struct ReplyingTo {

#[derive(Props)]
pub struct InputMessageProps<'a> {
message_type: InputType,
placeholder: &'a str,
on_submit: EventHandler<'a, FormMessageEvent>,
on_event: EventHandler<'a, HeaderEvent>,
Expand All @@ -36,17 +35,13 @@ pub struct InputMessageProps<'a> {
pub fn InputMessage<'a>(cx: Scope<'a, InputMessageProps<'a>>) -> Element<'a> {
let i18 = use_i18(cx);
let attach = use_attach(cx);
let client = use_client(cx);
let room = use_room(cx);
let notification = use_notification(cx);

let key_input_message_unknown_content = translate!(i18, "chat.input_message.unknown_content");
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_input_message_cta = translate!(i18, "chat.input_message.cta");

let send_attach_status =
use_shared_state::<SendAttachStatus>(cx).expect("Unable to use SendAttachStatus");
let replying_to = use_reply(cx);

let message_field = use_state(cx, String::new);
Expand All @@ -73,7 +68,7 @@ pub fn InputMessage<'a>(cx: Scope<'a, InputMessageProps<'a>>) -> Element<'a> {
let infered_type = infer::get(content.deref()).ok_or(AttachError::UncoverType)?;

let content_type: Result<mime::Mime, _> = infered_type.mime_type().parse();
let content_type = content_type.map_err(|e|AttachError::UnknownContent)?;
let content_type = content_type.map_err(|_|AttachError::UnknownContent)?;

let blob = match content_type.type_() {
mime::IMAGE => {
Expand Down Expand Up @@ -168,7 +163,7 @@ pub fn InputMessage<'a>(cx: Scope<'a, InputMessageProps<'a>>) -> Element<'a> {
if let Some(_) = attach.get() {
rsx!(
AttachPreview {
on_event: move |event| {
on_event: move |_| {
on_handle_send_attach();
attach.reset();
}
Expand All @@ -191,7 +186,7 @@ pub fn InputMessage<'a>(cx: Scope<'a, InputMessageProps<'a>>) -> Element<'a> {
Button {
text: "{key_input_message_cta}",
status: None,
on_click: move |event| {
on_click: move |_| {
if let Some(l) = &cx.props.on_attach {
let attachment = Attachment {
body: file.name.clone(),
Expand All @@ -212,7 +207,6 @@ pub fn InputMessage<'a>(cx: Scope<'a, InputMessageProps<'a>>) -> Element<'a> {
value: "{message_field}",
placeholder: cx.props.placeholder,
on_input: move |event: FormEvent| {
let value = event.value.clone();
message_field.set(event.value.clone());
},
on_keypress: move |event: KeyboardEvent| {
Expand Down
4 changes: 2 additions & 2 deletions src/components/molecules/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn List<'a>(cx: Scope<'a, ListProps<'a>>) -> Element<'a> {

cx.render(rsx! {
div {
class:"messages-list messages_list_thread",
class:"messages-list {messages_list_thread}",
onmounted: move |event| {
event.data.get_raw_element()
.ok()
Expand All @@ -82,7 +82,7 @@ pub fn List<'a>(cx: Scope<'a, ListProps<'a>>) -> Element<'a> {

let mut old_value = 0;

let on_down = EventListener::new(&container.clone(), "scroll", move |_| {
EventListener::new(&container.clone(), "scroll", move |_| {
let container_height = container.client_height();
let scroll_top = container.scroll_top() * -1;
let list_height = list.client_height();
Expand Down
38 changes: 13 additions & 25 deletions src/components/molecules/menu.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use crate::hooks::use_notification::use_notification;
use crate::MatrixClientState;
use crate::{hooks::use_auth::use_auth, services::matrix::matrix::create_client};
use crate::hooks::use_client::use_client;
use dioxus::prelude::*;
use dioxus_std::{i18n::use_i18, translate};
use gloo::storage::LocalStorage;
use wasm_bindgen::prelude::wasm_bindgen;

use crate::components::atoms::{ChatConversation, Icon, LogOut, MenuItem, UserCircle};

use dioxus_router::prelude::*;
use futures::TryFutureExt;

use crate::components::atoms::{ChatConversation, Icon, LogOut, MenuItem, UserCircle};
use crate::hooks::use_auth::LogoutError;
use crate::hooks::use_notification::use_notification;
use crate::hooks::use_auth::use_auth;
use crate::hooks::use_client::use_client;
use crate::pages::route::Route;

#[derive(Props)]
Expand All @@ -36,24 +33,15 @@ pub fn Menu<'a>(cx: Scope<'a, MenuProps<'a>>) -> Element<'a> {
to_owned![client, auth, notification, key_logout_error_server, key_chat_common_error_default_server];

async move {
let response = client.get().logout().await;

let Ok(_) = response else {
return notification.handle_error(&key_logout_error_server)
auth.logout(&client).await
}.unwrap_or_else(move |e: LogoutError| {
let message = match e {
LogoutError::Failed |LogoutError::DefaultClient => key_logout_error_server,
LogoutError::RemoveSession => key_chat_common_error_default_server,
};

let _ = <LocalStorage as gloo::storage::Storage>::delete("session_file");

let Ok(c) = create_client("https://matrix.org").await else {
return notification.handle_error(&key_chat_common_error_default_server)
};

client.set(MatrixClientState {
client: Some(c.clone()),
});

auth.set_logged_in(false)
}
notification.handle_error(&message)
})
});
};

Expand Down
3 changes: 0 additions & 3 deletions src/components/organisms/chat/active_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
components::{
atoms::{
header_main::{HeaderCallOptions, HeaderEvent},
input::InputType,
Avatar, Close, Header, Icon,
},
molecules::{input_message::FormMessageEvent, rooms::CurrentRoom, InputMessage, List},
Expand Down Expand Up @@ -118,7 +117,6 @@ pub fn ActiveRoom(cx: Scope) -> Element {
}
},
InputMessage {
message_type: InputType::Message,
placeholder: input_placeholder.get().as_str(),
on_submit: move |event| {
on_push_message(event, false)
Expand Down Expand Up @@ -165,7 +163,6 @@ pub fn ActiveRoom(cx: Scope) -> Element {
}
},
InputMessage {
message_type: InputType::Message,
placeholder: input_placeholder.get().as_str(),
on_submit: move |event| {
on_push_message(event, true)
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/factory/message_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use dioxus::prelude::*;

#[allow(clippy::needless_return)]

pub fn use_message_factory(cx: &ScopeState) -> MessageFactoryType {
pub fn use_message_factory(_cx: &ScopeState) -> MessageFactoryType {
MessageFactoryType {}
}

Expand Down
7 changes: 3 additions & 4 deletions src/hooks/use_attach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ pub struct AttachFile {
pub size: u64,
}

#[derive(Clone)]
#[derive(Debug)]
#[derive(Clone, Debug)]
pub enum AttachError {
NotFound,
NotFound,
UncoverType,
UnknownContent
UnknownContent,
}

#[allow(clippy::needless_return)]
Expand Down
31 changes: 28 additions & 3 deletions src/hooks/use_auth.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use dioxus::prelude::*;
use gloo::storage::{errors::StorageError, LocalStorage};
use matrix_sdk::Client;
use ruma::api::client::discovery::discover_homeserver::Response as WellKnownResponse;
use ruma::api::IncomingResponse;
use serde::{Deserialize, Serialize};
use url::Url;

use ruma::api::client::discovery::discover_homeserver::Response as WellKnownResponse;

use crate::pages::login::LoggedIn;

use super::use_client::UseClientState;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AuthError {
BuildError,
Expand Down Expand Up @@ -180,7 +181,7 @@ impl UseAuthState {
let Some(domain) = server.domain() else {
return;
};

let domain_name = extract_domain_name(domain);
if !username_parse.ends_with(domain_name.as_str()) {
username_parse = format!("{}:{}", username_parse, domain_name);
Expand Down Expand Up @@ -248,6 +249,30 @@ impl UseAuthState {
pub fn set_logged_in(&self, option: bool) {
*self.logged_in.write() = LoggedIn(option);
}

pub async fn logout(&self, client: &UseClientState) -> Result<(), LogoutError> {
client
.get()
.logout()
.await
.map_err(|_| LogoutError::Failed)?;
<LocalStorage as gloo::storage::Storage>::delete("session_file");

client
.default()
.await
.map_err(|_| LogoutError::DefaultClient)?;

self.set_logged_in(false);

Ok(())
}
}

pub enum LogoutError {
DefaultClient,
RemoveSession,
Failed,
}

fn extract_domain_name(host: &str) -> String {
Expand Down
17 changes: 6 additions & 11 deletions src/hooks/use_chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ pub fn use_chat(cx: &ScopeState) -> &UseChatState {

let key_common_error_room_id = translate!(i18, "chat.common.error.room_id");
let key_chat_session_error_not_found = translate!(i18, "chat.session.error.not_found");
let key_chat_message_list_errors_thread_not_found =
translate!(i18, "chat.message_list.errors.thread_not_found");
let key_chat_message_list_errors_room_not_found =
translate!(i18, "chat.message_list.errors.room_not_found");
let key_chat_message_list_errors_timeline_invalid_limit =
Expand Down Expand Up @@ -74,7 +72,7 @@ pub fn use_chat(cx: &ScopeState) -> &UseChatState {
.unwrap_or(&15)
.clone();

process(
if let Err(e) = process(
current_events,
&session,
&messages,
Expand All @@ -83,7 +81,7 @@ pub fn use_chat(cx: &ScopeState) -> &UseChatState {
&current_room_id,
)
.await
.map_err(|e: ChatError| {
{
let message = match e {
ChatError::InvalidSession => &key_chat_session_error_not_found,
ChatError::InvalidRoom => &key_common_error_room_id,
Expand All @@ -100,9 +98,7 @@ pub fn use_chat(cx: &ScopeState) -> &UseChatState {

messages_loading.set(false);
notification.handle_error(&message);

return;
});
};

if let Some(thread) = threading_to.get() {
messages.get().iter().find_map(|m| {
Expand Down Expand Up @@ -137,7 +133,7 @@ pub fn use_chat(cx: &ScopeState) -> &UseChatState {
}
});

use_effect(cx, &(room.get().id), |id| {
use_effect(cx, &(room.get().id), |_| {
to_owned![task_timeline, from];
async move {
from.set(None);
Expand Down Expand Up @@ -192,9 +188,8 @@ impl UseChatState {
self.inner.clone()
}

pub fn set(&self, state: ChatState) {
let mut inner = &self.inner;
inner = &state;
pub fn set(&mut self, state: ChatState) {
self.inner = state;
}

pub fn loadmore(&self, current_room_id: &str) {
Expand Down
28 changes: 25 additions & 3 deletions src/hooks/use_client.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::ops::Deref;

use dioxus::prelude::*;

use matrix_sdk::Client;
use std::ops::Deref;

use crate::MatrixClientState;
use crate::{
services::matrix::matrix::create_client, utils::get_homeserver::Homeserver, MatrixClientState,
};

#[allow(clippy::needless_return)]
pub fn use_client(cx: &ScopeState) -> &UseClientState {
Expand Down Expand Up @@ -33,4 +35,24 @@ impl UseClientState {
let mut inner = self.inner.write();
*inner = client;
}

pub async fn default(&self) -> Result<(), ClientError> {
let homeserver = Homeserver::new().map_err(|_| ClientError::InvalidUrl)?;

let c = match create_client(&homeserver.get_base_url()).await {
Ok(c) => c,
Err(_) => create_client(&Homeserver::default().get_base_url())
.await
.map_err(|_| ClientError::DefaultServer)?,
};

self.set(MatrixClientState { client: Some(c) });

Ok(())
}
}

pub enum ClientError {
InvalidUrl,
DefaultServer,
}
Loading

0 comments on commit e30a6c1

Please sign in to comment.