diff --git a/client/src/components/Home/WithNewMessageNotification.js b/client/src/components/Home/WithNewMessageNotification.js index ad08ca8..51096fe 100644 --- a/client/src/components/Home/WithNewMessageNotification.js +++ b/client/src/components/Home/WithNewMessageNotification.js @@ -45,11 +45,41 @@ const WithNewMessageNotification = WrappedComponent => { } const lastMessage = activities[activities.length - 1]; - const { username, text } = lastMessage; + const { username, type, text, fileName, locked, newUsername, currentUsername, action } = lastMessage; if (lastMessage !== prevState.lastMessage && !windowIsFocused) { - const title = `Message from ${username} (${roomId})`; - if (notificationIsAllowed && notificationIsEnabled) notify(title, text); + if (notificationIsAllowed && notificationIsEnabled) { + // Generate the proper notification according to the message type + switch (type) { + case 'USER_ENTER': + notify(`User ${username} joined`); + break; + case 'USER_EXIT': + notify(`User ${username} left`); + break; + case 'RECEIVE_FILE': + notify(`${username} sent file <${fileName}>`); + break; + case 'TEXT_MESSAGE': + notify(`${username} said:`, text); + break; + case 'USER_ACTION': + notify(`${username} ${action}`); + break; + case 'CHANGE_USERNAME': + notify(`${currentUsername} changed their name to ${newUsername}`); + break; + case 'TOGGLE_LOCK_ROOM': + if (locked) { + notify(`Room ${roomId} is now locked`); + } else { + notify(`Room ${roomId} is now unlocked`); + } + break; + default: + break; + } + } if (soundIsEnabled) beep.play(); } diff --git a/client/src/components/Home/index.test.js b/client/src/components/Home/index.test.js index fa31269..1a7b0eb 100644 --- a/client/src/components/Home/index.test.js +++ b/client/src/components/Home/index.test.js @@ -180,7 +180,7 @@ describe('Connected Home component', () => { , ); expect(store.getState().app.unreadMessageCount).toBe(1); - expect(notify).toHaveBeenLastCalledWith('Message from sender ()', 'new message'); + expect(notify).toHaveBeenLastCalledWith('sender said:', 'new message'); expect(beep.play).toHaveBeenLastCalledWith(); expect(Tinycon.setBubble).toHaveBeenLastCalledWith(1); diff --git a/client/src/utils/notifications.js b/client/src/utils/notifications.js index 18317bb..b770d94 100644 --- a/client/src/utils/notifications.js +++ b/client/src/utils/notifications.js @@ -23,7 +23,7 @@ const showNotification = (title, message, avatarUrl) => { document.addEventListener('visibilitychange', handleVisibilityChange); }; -export const notify = (title, content) => { +export const notify = (title, content = '') => { if (!('Notification' in window)) { alert('This browser does not support desktop notification'); }