@@ -27,11 +27,7 @@ export default function SignedLeftSidebar(props) {
2727 const [ snackbarOpen , setSnackbarOpen ] = useState ( false ) ;
2828 const [ snackbarSeverity , setSnackbarSeverity ] = useState ( "success" ) ;
2929 const [ snackbarText , setSnackbarText ] = useState ( "" ) ;
30- const [ rooms , setRooms ] = useState ( {
31- conversations : { } ,
32- communities : { } ,
33- directMessages : { } ,
34- } ) ;
30+ const [ rooms , setRooms ] = useState ( { } ) ;
3531 const [ showSearch , setShowSearch ] = useState ( false ) ;
3632 const [ sortAnchorEl , setSortAnchorEl ] = useState ( null ) ;
3733 const [ groupByCommunity , setGroupByCommunity ] = useState ( true ) ;
@@ -116,6 +112,70 @@ export default function SignedLeftSidebar(props) {
116112 } ) ;
117113 } , [ ] ) ;
118114
115+ useEffect ( ( ) => {
116+ const handleMessageEvents = ( e ) => {
117+ switch ( e . data . eventName ) {
118+ case "unread-changed-by-subscription" :
119+ if (
120+ rooms [ "conversations" ] &&
121+ rooms [ "conversations" ] [ e . data . data . _id ] &&
122+ rooms [ "conversations" ] [ e . data . data . _id ] . alert !== e . data . data . alert
123+ ) {
124+ console . log ( e . data . data ) ;
125+ let newRooms = rooms ;
126+ newRooms [ "conversations" ] [ e . data . data . _id ] [ "alert" ] =
127+ e . data . data . alert ;
128+ if ( e . data . data . t === "d" ) {
129+ newRooms [ "directMessages" ] [ e . data . data . _id ] [ "alert" ] =
130+ e . data . data . alert ;
131+ } else {
132+ newRooms [ "communities" ] [ e . data . data . name . split ( / _ ( .+ ) / ) [ 0 ] ] [
133+ e . data . data . _id
134+ ] [ "alert" ] = e . data . data . alert ;
135+ }
136+ setRooms ( { ...newRooms } ) ;
137+ }
138+ break ;
139+ case "room-opened" :
140+ if (
141+ rooms [ "conversations" ] &&
142+ ! rooms [ "conversations" ] [ e . data . data . _id ]
143+ ) {
144+ rooms [ "conversations" ] [ e . data . data . _id ] = e . data . data ;
145+ fetch (
146+ `${ rcApiDomain } /api/v1/subscriptions.getOne?roomId=${ e . data . data . _id } ` ,
147+ {
148+ headers : {
149+ "X-Auth-Token" : Cookies . get ( "rc_token" ) ,
150+ "X-User-Id" : Cookies . get ( "rc_uid" ) ,
151+ "Content-Type" : "application/json" ,
152+ } ,
153+ method : "GET" ,
154+ }
155+ )
156+ . then ( ( response ) => response . json ( ) )
157+ . then ( ( data ) => {
158+ delete rooms [ "conversations" ] [ e . data . data . _id ] ;
159+ addRoom ( data . subscription ) ;
160+ } )
161+ . catch ( ( err ) => {
162+ console . log ( "Error Fetching Room from server --->" , err ) ;
163+ } ) ;
164+ break ;
165+ }
166+ break ;
167+ }
168+ } ;
169+ window . addEventListener ( "message" , handleMessageEvents , true ) ;
170+ return ( ) => {
171+ window . removeEventListener ( "message" , handleMessageEvents , true ) ;
172+ } ;
173+ } , [ rooms ] ) ;
174+
175+ const handleEndCreateCommunity = ( ) => {
176+ setStartCreateCommunity ( false ) ;
177+ } ;
178+
119179 const handleEndCreateChannel = ( ) => {
120180 setStartCreateChannel ( false ) ;
121181 } ;
@@ -218,7 +278,6 @@ export default function SignedLeftSidebar(props) {
218278 const openSortMenu = ( event ) => {
219279 setSortAnchorEl ( event . currentTarget ) ;
220280 } ;
221-
222281 return (
223282 < div className = "signed-left-sidebar-wrapper" >
224283 < div className = "signed-left-sidebar-header" >
@@ -373,6 +432,7 @@ export default function SignedLeftSidebar(props) {
373432 > </ CommunityListItem >
374433 ) }
375434 { groupByCommunity &&
435+ rooms [ "communities" ] &&
376436 Object . keys ( rooms [ "communities" ] ) . map ( ( community_name ) => {
377437 return (
378438 < CommunityListItem
@@ -382,13 +442,15 @@ export default function SignedLeftSidebar(props) {
382442 > </ CommunityListItem >
383443 ) ;
384444 } ) }
385- { groupByCommunity && Object . keys ( rooms [ "directMessages" ] ) . length ? (
386- < CommunityListItem
387- community = { rooms [ "directMessages" ] }
388- key = { "Direct Messages" }
389- community_name = { "Direct Messages" }
390- > </ CommunityListItem >
391- ) : null }
445+ { groupByCommunity &&
446+ rooms [ "directMessages" ] &&
447+ Object . keys ( rooms [ "directMessages" ] ) . length > 0 && (
448+ < CommunityListItem
449+ community = { rooms [ "directMessages" ] }
450+ key = { "Direct Messages" }
451+ community_name = { "Direct Messages" }
452+ > </ CommunityListItem >
453+ ) }
392454 </ div >
393455 < div className = "signed-left-sidebar-footer" >
394456 < img
0 commit comments