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

Commit

Permalink
Fix/chats (#61)
Browse files Browse the repository at this point in the history
* fix: reset current room when change space

* fix(listen-messages): prevent messages block when sync

* fix: prevent send empty messages

* chore: verify empty fields

* fix: deny if is_empty condition

* fix: move between pages breaks the message key

* refactor: remove unnecessary attribute

* style: minor change
  • Loading branch information
b-avb authored Mar 6, 2024
1 parent 6f89c28 commit f04b8b6
Show file tree
Hide file tree
Showing 29 changed files with 93 additions and 63 deletions.
1 change: 1 addition & 0 deletions public/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ ul {
.message__content {
display: flex;
flex-direction: column;
white-space: pre-line
}

.message__container__content {
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 @@ -74,7 +74,7 @@ pub fn MessageInput<'a>(cx: Scope<'a, MessageInputProps<'a>>) -> Element<'a> {
onkeypress: move |event| cx.props.on_keypress.call(event)
}

if cx.props.message.len() > 0 {
if !cx.props.message.is_empty() {
match cx.props.itype {
InputType::Message => render!(
rsx!(
Expand Down
22 changes: 13 additions & 9 deletions src/components/atoms/textarea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,19 @@ pub fn TextareaInput<'a>(cx: Scope<'a, TextareaInputProps<'a>>) -> Element<'a> {
}
}
}
button {
class: "textarea__cta input__cta",
onclick: move |event| cx.props.on_click.call(event),
Icon {
stroke: "var(--icon-subdued)",
icon: Send,
height: 20,
width: 20
}
if !cx.props.value.trim().is_empty() {
rsx!(
button {
class: "textarea__cta input__cta",
onclick: move |event| cx.props.on_click.call(event),
Icon {
stroke: "var(--icon-subdued)",
icon: Send,
height: 20,
width: 20
}
}
)
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/components/molecules/input_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,18 @@ pub fn InputMessage<'a>(cx: Scope<'a, InputMessageProps<'a>>) -> Element<'a> {
placeholder: cx.props.placeholder,
on_input: move |event: FormEvent| {
message_field.set(event.value.clone());
log::info!("{:?}", event.value.clone());
},
on_keypress: move |event: KeyboardEvent| {
let modifiers = event.modifiers();

match modifiers {
keyboard_types::Modifiers::SHIFT => {}
_ => {
if event.code() == keyboard_types::Code::Enter && message_field.get().len() > 0 {
cx.props.on_submit.call(FormMessageEvent { value: message_field.get().clone() });
if event.code() == keyboard_types::Code::Enter {
if !message_field.get().trim().is_empty() {
cx.props.on_submit.call(FormMessageEvent { value: message_field.get().clone() });
}
message_field.set(String::from(""));
}
}
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> {
async move {}
});

let messages_list_skeleton = if cx.props.messages.len() > 0 {
let messages_list_skeleton = if !cx.props.messages.is_empty() {
""
} else {
"messages-list--skeleton"
Expand Down Expand Up @@ -107,7 +107,7 @@ pub fn List<'a>(cx: Scope<'a, ListProps<'a>>) -> Element<'a> {
}
},
rsx!(
if cx.props.messages.len() > 0 {
if !cx.props.messages.is_empty() {
rsx!(cx.props.messages.iter().enumerate().map(|(i, m)| {
match m {
TimelineRelation::None(message) => {
Expand Down
6 changes: 3 additions & 3 deletions src/components/molecules/rooms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dioxus::prelude::*;

use crate::components::atoms::{room::RoomItem, RoomView, RoomViewSkeleton};

#[derive(Clone, Debug, PartialEq, Hash, Eq)]
#[derive(Clone, Debug, PartialEq, Hash, Eq, Default)]
pub struct CurrentRoom {
pub id: String,
pub name: String,
Expand All @@ -22,7 +22,7 @@ pub struct RoomsListProps<'a> {
}

pub fn RoomsList<'a>(cx: Scope<'a, RoomsListProps<'a>>) -> Element<'a> {
let rooms_list_skeleton = if cx.props.rooms.len() > 0 {
let rooms_list_skeleton = if !cx.props.rooms.is_empty() {
""
} else {
"rooms-list--skeleton"
Expand All @@ -31,7 +31,7 @@ pub fn RoomsList<'a>(cx: Scope<'a, RoomsListProps<'a>>) -> Element<'a> {
cx.render(rsx! {
section {
class:"rooms-list {rooms_list_skeleton} fade-in",
if cx.props.rooms.len() > 0 {
if !cx.props.rooms.is_empty() {
rsx!(cx.props.rooms.iter().map(|room| {
rsx!(RoomView {
key: "{room.id}",
Expand Down
6 changes: 1 addition & 5 deletions src/components/organisms/chat/active_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ pub fn ActiveRoom(cx: Scope) -> Element {
match evt.value {
HeaderCallOptions::CLOSE => {
nav.push(Route::ChatList {});
room.set(CurrentRoom {
id: String::new(),
name: String::new(),
avatar_uri: None,
});
room.set(CurrentRoom::default());
}
_ => {}
}
Expand Down
6 changes: 1 addition & 5 deletions src/components/organisms/menu/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ pub struct TitleHeaderMain {
}

pub fn IndexMenu(cx: Scope) -> Element {
use_shared_state_provider::<CurrentRoom>(cx, || CurrentRoom {
id: String::new(),
name: String::new(),
avatar_uri: None,
});
use_shared_state_provider::<CurrentRoom>(cx, || CurrentRoom::default());
use_shared_state_provider::<TitleHeaderMain>(cx, || TitleHeaderMain {
title: String::from("Chats"),
});
Expand Down
1 change: 0 additions & 1 deletion src/hooks/use_attach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub enum AttachError {
UnknownContent,
}

#[allow(clippy::needless_return)]
pub fn use_attach(cx: &ScopeState) -> &UseAttachState {
let attach = use_shared_state::<Option<AttachFile>>(cx).expect("Attach file not provided");

Expand Down
1 change: 0 additions & 1 deletion src/hooks/use_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ impl LoginInfoBuilder {
}
}

#[allow(clippy::needless_return)]
pub fn use_auth(cx: &ScopeState) -> &UseAuthState {
let logged_in = use_shared_state::<LoggedIn>(cx).expect("Unable to use LoggedIn");
let login_cache =
Expand Down
1 change: 0 additions & 1 deletion src/hooks/use_chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub enum ChatError {
TimelineError(TimelineError),
}

#[allow(clippy::needless_return)]
pub fn use_chat(cx: &ScopeState) -> &UseChatState {
let i18 = use_i18(cx);
let client = use_client(cx);
Expand Down
1 change: 0 additions & 1 deletion src/hooks/use_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::{
services::matrix::matrix::create_client, utils::get_homeserver::Homeserver, MatrixClientState,
};

#[allow(clippy::needless_return)]
pub fn use_client(cx: &ScopeState) -> &UseClientState {
let matrix = use_shared_state::<MatrixClientState>(cx).expect("Matrix client not provided");

Expand Down
7 changes: 1 addition & 6 deletions src/hooks/use_init_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub struct MessageDispatchId {
pub value: HashMap<String, Option<String>>,
}

#[allow(clippy::needless_return)]
pub fn use_init_app(cx: &ScopeState) {
use_shared_state_provider::<LoggedIn>(cx, || LoggedIn(false));
use_shared_state_provider::<MatrixClientState>(cx, || MatrixClientState { client: None });
Expand All @@ -39,11 +38,7 @@ pub fn use_init_app(cx: &ScopeState) {
// Temporarily moved here because Route has an unexpected
// change when we push a ChatRoom from a different nest route

use_shared_state_provider::<CurrentRoom>(cx, || CurrentRoom {
id: String::from(""),
name: String::from(""),
avatar_uri: None,
});
use_shared_state_provider::<CurrentRoom>(cx, || CurrentRoom::default());
use_shared_state_provider::<Messages>(cx, || Vec::new());
use_shared_state_provider::<Option<AttachFile>>(cx, || None);
use_shared_state_provider::<Option<ReplyingTo>>(cx, || None);
Expand Down
25 changes: 25 additions & 0 deletions src/hooks/use_lifecycle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use dioxus::prelude::*;

pub fn use_lifecycle<C: FnOnce(), D: FnOnce() + 'static>(
cx: &ScopeState,
create: C,
destroy: D,
) -> &LifeCycle<D> {
cx.use_hook(|| {
create();
LifeCycle {
ondestroy: Some(destroy),
}
})
}

pub struct LifeCycle<D: FnOnce()> {
ondestroy: Option<D>,
}

impl<D: FnOnce()> Drop for LifeCycle<D> {
fn drop(&mut self) {
let f = self.ondestroy.take().unwrap();
f();
}
}
8 changes: 3 additions & 5 deletions src/hooks/use_listen_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use super::{
use_thread::use_thread,
};

#[allow(clippy::needless_return)]
pub fn use_listen_message(cx: &ScopeState) -> &UseListenMessagesState {
let i18 = use_i18(cx);
let client = use_client(cx).get();
Expand Down Expand Up @@ -293,10 +292,9 @@ pub fn use_listen_message(cx: &ScopeState) -> &UseListenMessagesState {
];

async move {
client
.sync_once(SyncSettings::default())
.await
.map_err(|_| ListenMessageError::FailedSync)?;
if let Err(e) = client.sync_once(SyncSettings::default()).await {
log::warn!("{e:?}")
};

let me = session.get().ok_or(ListenMessageError::SessionNotFound)?;

Expand Down
1 change: 0 additions & 1 deletion src/hooks/use_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use dioxus::prelude::*;

use crate::{components::atoms::message::Messages, services::matrix::matrix::TimelineRelation};

#[allow(clippy::needless_return)]
pub fn use_messages(cx: &ScopeState) -> &UseMessagesState {
let messages = use_shared_state::<Messages>(cx).expect("Unable to use Messages");

Expand Down
1 change: 0 additions & 1 deletion src/hooks/use_modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use dioxus::prelude::*;

use crate::services::matrix::matrix::AccountInfo;

#[allow(clippy::needless_return)]
pub fn use_modal(cx: &ScopeState) -> &UseModalState {
let modal = use_shared_state::<ModalState>(cx).expect("Modal state not provided");

Expand Down
1 change: 0 additions & 1 deletion src/hooks/use_notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub enum NotificationType {
None,
}

#[allow(clippy::needless_return)]
pub fn use_notification(cx: &ScopeState) -> &UseNotificationState {
let notification = use_shared_state::<NotificationItem>(cx).expect("Notification not provided");

Expand Down
1 change: 0 additions & 1 deletion src/hooks/use_reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use dioxus::prelude::*;

use crate::components::molecules::input_message::ReplyingTo;

#[allow(clippy::needless_return)]
pub fn use_reply(cx: &ScopeState) -> &UseReplyState {
let replying_to =
use_shared_state::<Option<ReplyingTo>>(cx).expect("Unable to read replying_to");
Expand Down
5 changes: 4 additions & 1 deletion src/hooks/use_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use dioxus::prelude::*;

use crate::components::molecules::rooms::CurrentRoom;

#[allow(clippy::needless_return)]
pub fn use_room(cx: &ScopeState) -> &UseRoomState {
let current_room = use_shared_state::<CurrentRoom>(cx).expect("Unable to use CurrentRoom");

Expand All @@ -25,4 +24,8 @@ impl UseRoomState {
let mut inner = self.inner.write();
*inner = room;
}

pub fn default(&self) {
self.set(CurrentRoom::default())
}
}
1 change: 0 additions & 1 deletion src/hooks/use_send_attach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ pub enum SendAttachStatus {
None,
}

#[allow(clippy::needless_return)]
pub fn use_send_attach(cx: &ScopeState) -> &UseSendMessageState {
let i18 = use_i18(cx);
let client = use_client(cx);
Expand Down
1 change: 0 additions & 1 deletion src/hooks/use_send_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ pub enum MessageStatus {
None,
}

#[allow(clippy::needless_return)]
pub fn use_send_message(cx: &ScopeState) -> &UseSendMessageState {
let i18 = use_i18(cx);
let client = use_client(cx);
Expand Down
1 change: 0 additions & 1 deletion src/hooks/use_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::time::Duration;

use crate::services::matrix::matrix::FullSession;

#[allow(clippy::needless_return)]
pub fn use_session(cx: &ScopeState) -> &UseSessionState {
let user = use_shared_state::<Option<UserSession>>(cx).expect("Unable to use UserSession");

Expand Down
1 change: 0 additions & 1 deletion src/hooks/use_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use dioxus::prelude::*;

use crate::services::matrix::matrix::TimelineThread;

#[allow(clippy::needless_return)]
pub fn use_thread(cx: &ScopeState) -> &UseThreadState {
let replying_to =
use_shared_state::<Option<TimelineThread>>(cx).expect("Unable to read TimelineThread");
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod hooks {
pub mod use_chat;
pub mod use_client;
pub mod use_init_app;
pub mod use_lifecycle;
pub mod use_listen_message;
pub mod use_messages;
pub mod use_modal;
Expand Down
30 changes: 26 additions & 4 deletions src/pages/chat/chat_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use crate::{
organisms::{chat::ActiveRoom, main::TitleHeaderMain},
},
hooks::{
use_client::use_client, use_messages::use_messages, use_notification::use_notification,
use_room::use_room, use_session::use_session,
use_client::use_client, use_lifecycle::use_lifecycle, use_messages::use_messages, use_notification::use_notification, use_room::use_room, use_session::use_session
},
services::matrix::matrix::{list_rooms_and_spaces, Conversations},
};
Expand Down Expand Up @@ -46,6 +45,17 @@ pub fn ChatList(cx: Scope) -> Element {
let title_header =
use_shared_state::<TitleHeaderMain>(cx).expect("Unable to read title header");
let is_loading = use_state(cx, || false);

let r = room.clone();
use_lifecycle(
&cx,
|| {},
move || {
to_owned![r];

r.default();
},
);

let on_click_room = move |evt: FormRoomEvent| {
room.set(evt.room.clone());
Expand Down Expand Up @@ -108,7 +118,7 @@ pub fn ChatList(cx: Scope) -> Element {
section {
class: "chat-list options",
div {
if spaces.get().len() > 0 {
if !spaces.get().is_empty() {
rsx!(
ul {
class: "chat-list__wrapper",
Expand All @@ -120,6 +130,12 @@ pub fn ChatList(cx: Scope) -> Element {
rooms_filtered.set(rooms.get().clone());
selected_space.set(key_chat_list_home.clone());
title_header.write().title = key_chat_list_home.clone();

if !rooms.get().iter().any(|r| {
room.get().id.eq(&r.id)
}) {
room.default()
}
}
}

Expand All @@ -134,6 +150,12 @@ pub fn ChatList(cx: Scope) -> Element {
rooms_filtered.set(value.clone());
selected_space.set(space.name.clone());
title_header.write().title = space.name.clone();

if !value.iter().any(|r| {
room.get().id.eq(&r.id)
}) {
room.default()
}
}
}
)
Expand Down Expand Up @@ -171,7 +193,7 @@ pub fn ChatList(cx: Scope) -> Element {

let default_rooms = all_rooms.get().iter().cloned().collect::<Vec<_>>();

if event.value.len() > 0 {
if !event.value.is_empty() {
let x = default_rooms
.iter()
.filter(|r| r.name.to_lowercase().contains(&event.value.to_lowercase()))
Expand Down
Loading

0 comments on commit f04b8b6

Please sign in to comment.