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

Commit

Permalink
Centralize notifications in a single management point (#46)
Browse files Browse the repository at this point in the history
* refactor: centralize notifications in a single management point

* chore: remove ambiguity

* chore: impl Default for NotificationItem

* fix(notification): adjust height

* chore: remove unused imports

* refactor: derive Default
  • Loading branch information
b-avb authored Feb 21, 2024
1 parent 7d945d4 commit a54e0d0
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 52 deletions.
2 changes: 1 addition & 1 deletion public/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ ul {
position: absolute;
z-index: 100;
box-shadow: 0px 4px 4px 0px rgba(0, 0, 0, 0.25);
height: 60px;
min-height: 60px;
border-radius: 8px;
left: 0;
}
Expand Down
19 changes: 0 additions & 19 deletions src/components/organisms/chat/utils/handle_notification.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/components/organisms/chat/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
pub mod handle_command;
pub mod handle_notification;

pub use handle_command::handle_command;
pub use handle_notification::handle_notification;
11 changes: 2 additions & 9 deletions src/hooks/use_init_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use matrix_sdk::encryption::verification::SasVerification;
use ruma::api::client::uiaa::AuthType;

use super::use_auth::CacheLogin;
use super::use_notification::{NotificationHandle, NotificationItem, NotificationType};
use super::use_notification::NotificationItem;
use super::use_send_attach::SendAttachStatus;
use super::use_session::UserSession;
use super::{use_attach::AttachFile, use_modal::ModalState};
Expand Down Expand Up @@ -47,14 +47,7 @@ pub fn use_init_app(cx: &ScopeState) {
use_shared_state_provider::<Messages>(cx, || Vec::new());
use_shared_state_provider::<Option<AttachFile>>(cx, || None);
use_shared_state_provider::<Option<ReplyingTo>>(cx, || None);
use_shared_state_provider::<NotificationItem>(cx, || NotificationItem {
title: String::from(""),
body: String::from(""),
show: false,
handle: NotificationHandle {
value: NotificationType::None,
},
});
use_shared_state_provider::<NotificationItem>(cx, || NotificationItem::default());

use_shared_state_provider::<Option<SasVerification>>(cx, || None);
use_shared_state_provider::<Option<TimelineThread>>(cx, || None);
Expand Down
18 changes: 7 additions & 11 deletions src/hooks/use_listen_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use matrix_sdk::{
use ruma::events::room::message::Relation;

use crate::{
components::organisms::chat::utils::handle_notification,
hooks::use_notification::{NotificationHandle, NotificationItem, NotificationType},
pages::chat::chat::MessageEvent,
services::matrix::matrix::{
Expand Down Expand Up @@ -248,17 +247,14 @@ pub fn use_listen_message(cx: &ScopeState) -> &UseListenMessagesState {
};

if let Some(content) = plain_message {
handle_notification(
NotificationItem {
title: String::from(room_name),
body: String::from(content),
show: true,
handle: NotificationHandle {
value: NotificationType::Click,
},
notification.handle_notification(NotificationItem {
title: room_name,
body: String::from(content),
show: true,
handle: NotificationHandle {
value: NotificationType::Click,
},
notification.to_owned(),
);
})
}
}
}
Expand Down
26 changes: 17 additions & 9 deletions src/hooks/use_notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ use matrix_sdk::encryption::verification::SasVerification;

use crate::pages::route::Route;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct NotificationItem {
pub title: String,
pub body: String,
pub show: bool,
pub handle: NotificationHandle,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct NotificationHandle {
pub value: NotificationType,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub enum NotificationType {
Click,
AcceptSas(SasVerification, Option<Route>),
#[default]
None,
}

Expand All @@ -42,20 +43,27 @@ impl UseNotificationState {
self.inner.read().clone()
}

pub fn set(&self, item: NotificationItem) {
let mut inner = self.inner.write();
*inner = item;
pub fn handle_notification(&self, item: NotificationItem) {
let this = self.clone();
let inner = self.inner.clone();
*inner.write() = item;

gloo::timers::callback::Timeout::new(3000, move || this.clear()).forget();
}

pub fn handle_error(&self, body: &str) {
let mut inner = self.inner.write();
*inner = NotificationItem {
self.handle_notification(NotificationItem {
title: String::from("Error"),
body: String::from(body),
show: true,
handle: NotificationHandle {
value: NotificationType::None,
},
};
});
}

pub fn clear(&self) {
let mut inner = self.inner.write();
*inner = NotificationItem::default();
}
}
2 changes: 1 addition & 1 deletion src/pages/chat/room/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ pub fn RoomGroup(cx: Scope) -> Element {

let _ = client.get().sync_once(SyncSettings::default()).await;

notification_success.set(NotificationItem {
notification_success.handle_notification(NotificationItem {
title: name,
body: key_group_success_description,
show: true,
Expand Down

0 comments on commit a54e0d0

Please sign in to comment.