@@ -13,6 +13,7 @@ import CreateChannel from "../CreateChannel";
1313import axios from "axios" ;
1414import { rcApiDomain , githubApiDomain } from "./../../utils/constants" ;
1515import SidebarSearch from "../SidebarSearch" ;
16+ import { useHistory } from "react-router-dom" ;
1617
1718import "./index.css" ;
1819
@@ -31,6 +32,7 @@ export default function SignedLeftSidebar(props) {
3132 const [ showSearch , setShowSearch ] = useState ( false ) ;
3233 const [ sortAnchorEl , setSortAnchorEl ] = useState ( null ) ;
3334 const [ groupByCommunity , setGroupByCommunity ] = useState ( true ) ;
35+ const history = useHistory ( ) ;
3436
3537 const sortRoomsAlphabetically = ( conversations ) => {
3638 let chatRooms = [ ] ;
@@ -90,7 +92,7 @@ export default function SignedLeftSidebar(props) {
9092 } else {
9193 newRooms [ "directMessages" ] [ room . _id ] = room ;
9294 }
93- setRooms ( newRooms ) ;
95+ setRooms ( { ... newRooms } ) ;
9496 } ;
9597
9698 useEffect ( ( ) => {
@@ -113,8 +115,29 @@ export default function SignedLeftSidebar(props) {
113115 } , [ ] ) ;
114116
115117 useEffect ( ( ) => {
118+ if ( Notification . permission === "default" ) {
119+ Notification . requestPermission ( ) ;
120+ }
116121 const handleMessageEvents = ( e ) => {
117122 switch ( e . data . eventName ) {
123+ case "notification" :
124+ const { fromOpenedRoom, hasFocus, notification } = e . data . data ;
125+ const { title, text, payload } = notification ;
126+ const { name, type } = payload ;
127+ if ( fromOpenedRoom && hasFocus ) {
128+ break ;
129+ }
130+ let noty = new Notification ( `${ title } ` , {
131+ body : `${ text } ` ,
132+ } ) ;
133+ noty . onclick = function ( ) {
134+ let newUnreadUrl = `/${
135+ type === "c" ? "channel" : type === "p" ? "group" : "direct"
136+ } /${ type === "d" ? title : name } `;
137+ history . push ( newUnreadUrl ) ;
138+ window . focus ( ) ;
139+ } ;
140+ break ;
118141 case "unread-changed" :
119142 if ( typeof e . data . data === "number" ) {
120143 document . title = `(${ e . data . data } ) RCforCommunity` ;
@@ -130,7 +153,6 @@ export default function SignedLeftSidebar(props) {
130153 rooms [ "conversations" ] [ e . data . data . _id ] &&
131154 rooms [ "conversations" ] [ e . data . data . _id ] . alert !== e . data . data . alert
132155 ) {
133- console . log ( e . data . data ) ;
134156 let newRooms = rooms ;
135157 newRooms [ "conversations" ] [ e . data . data . _id ] [ "alert" ] =
136158 e . data . data . alert ;
@@ -143,6 +165,11 @@ export default function SignedLeftSidebar(props) {
143165 ] [ "alert" ] = e . data . data . alert ;
144166 }
145167 setRooms ( { ...newRooms } ) ;
168+ } else if (
169+ rooms [ "conversations" ] &&
170+ ! rooms [ "conversations" ] [ e . data . data . _id ]
171+ ) {
172+ addRoom ( e . data . data ) ;
146173 }
147174 break ;
148175 case "room-opened" :
0 commit comments