@@ -27,18 +27,25 @@ 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- const [ communities , setCommunities ] = useState ( { } ) ;
32- const [ directMessages , setDirectMessages ] = useState ( [ ] ) ;
30+ const [ rooms , setRooms ] = useState ( {
31+ conversations : { } ,
32+ communities : { } ,
33+ directMessages : { } ,
34+ } ) ;
3335 const [ showSearch , setShowSearch ] = useState ( false ) ;
3436 const [ sortAnchorEl , setSortAnchorEl ] = useState ( null ) ;
3537 const [ groupByCommunity , setGroupByCommunity ] = useState ( true ) ;
3638
37- const sortRoomsAlphabetically = ( ) => {
38- let chatRooms = rooms ;
39+ const sortRoomsAlphabetically = ( conversations ) => {
40+ let chatRooms = [ ] ;
41+ for ( let roomId in conversations ) {
42+ chatRooms . push ( conversations [ roomId ] ) ;
43+ }
3944 chatRooms . sort ( ( a , b ) => {
40- let roomAname = a . name . split ( / _ ( .+ ) / ) [ 1 ] || a . name ;
41- let roomBname = b . name . split ( / _ ( .+ ) / ) [ 1 ] || b . name ;
45+ let roomAname =
46+ ( a . fname && ( a . fname . split ( / _ ( .+ ) / ) [ 1 ] || a . fname ) ) || a . name ;
47+ let roomBname =
48+ ( b . fname && ( b . fname . split ( / _ ( .+ ) / ) [ 1 ] || b . fname ) ) || b . name ;
4249 roomAname = roomAname . toUpperCase ( ) ;
4350 roomBname = roomBname . toUpperCase ( ) ;
4451 if ( roomAname < roomBname ) {
@@ -49,38 +56,45 @@ export default function SignedLeftSidebar(props) {
4956 }
5057 return 0 ;
5158 } ) ;
52- setChatRooms ( chatRooms ) ;
59+ formatAndSetRooms ( chatRooms ) ;
5360 } ;
5461
55- const setChatRooms = ( rooms ) => {
62+ const formatAndSetRooms = ( rooms ) => {
5663 let communities = { } ;
57- let directMessages = [ ] ;
64+ let directMessages = { } ;
65+ let conversations = { } ;
5866 rooms . forEach ( ( room ) => {
67+ conversations [ room . _id ] = room ;
5968 if ( room [ "t" ] === "c" || room [ "t" ] === "p" ) {
6069 let community_name = room . name . split ( / _ ( .+ ) / ) [ 0 ] ;
61- if ( ! communities [ community_name ] ) communities [ community_name ] = [ ] ;
62- communities [ community_name ] . push ( room ) ;
70+ if ( ! communities [ community_name ] ) communities [ community_name ] = { } ;
71+ communities [ community_name ] [ room . _id ] = room ;
6372 } else {
64- directMessages . push ( room ) ;
73+ directMessages [ room . _id ] = room ;
6574 }
6675 } ) ;
67- setRooms ( rooms ) ;
68- setCommunities ( communities ) ;
69- setDirectMessages ( directMessages ) ;
76+ setRooms ( {
77+ conversations,
78+ communities,
79+ directMessages,
80+ } ) ;
7081 } ;
7182
7283 const addRoom = ( room ) => {
73- setRooms ( [ ...rooms , room ] ) ;
84+ let newRooms = rooms ;
85+ newRooms [ "conversations" ] [ room . _id ] = room ;
86+ if ( document . getElementById ( "alpha-sort" ) . checked ) {
87+ return sortRoomsAlphabetically ( newRooms [ "conversations" ] ) ;
88+ }
7489 if ( room [ "t" ] === "c" || room [ "t" ] === "p" ) {
75- let newCommnunities = { ...communities } ;
7690 let community_name = room . name . split ( / _ ( .+ ) / ) [ 0 ] ;
77- if ( ! newCommnunities [ community_name ] )
78- newCommnunities [ community_name ] = [ ] ;
79- newCommnunities [ community_name ] . push ( room ) ;
80- setCommunities ( newCommnunities ) ;
91+ if ( ! newRooms [ "communities" ] [ community_name ] )
92+ newRooms [ "communities" ] [ community_name ] = { } ;
93+ newRooms [ "communities" ] [ community_name ] [ room . _id ] = room ;
8194 } else {
82- setDirectMessages ( [ ... directMessages , room ] ) ;
95+ newRooms [ " directMessages" ] [ room . _id ] = room ;
8396 }
97+ setRooms ( newRooms ) ;
8498 } ;
8599
86100 useEffect ( ( ) => {
@@ -95,7 +109,7 @@ export default function SignedLeftSidebar(props) {
95109 } )
96110 . then ( ( response ) => response . json ( ) )
97111 . then ( ( data ) => {
98- setChatRooms ( data . update ) ;
112+ formatAndSetRooms ( data . update ) ;
99113 } )
100114 . catch ( ( err ) => {
101115 console . log ( "Error Fetching Rooms from server --->" , err ) ;
@@ -162,7 +176,9 @@ export default function SignedLeftSidebar(props) {
162176 { externalCommand : "logout" } ,
163177 `${ rcApiDomain } `
164178 ) ;
165- document . getElementsByClassName ( "loading-chatWindow" ) [ 0 ] . classList . remove ( "hide-chatWindow" ) ;
179+ document
180+ . getElementsByClassName ( "loading-chatWindow" ) [ 0 ]
181+ . classList . remove ( "hide-chatWindow" ) ;
166182 }
167183 const loadingIcon = document . getElementById ( "logout-loading-icon" ) ;
168184 const logoutButton = document . getElementById ( "logout-menu-item" ) ;
@@ -293,7 +309,9 @@ export default function SignedLeftSidebar(props) {
293309 < Radio
294310 color = "primary"
295311 id = "alpha-sort"
296- onChange = { sortRoomsAlphabetically }
312+ onChange = { ( ) => {
313+ sortRoomsAlphabetically ( rooms [ "conversations" ] ) ;
314+ } }
297315 />
298316 </ div >
299317 </ div >
@@ -332,7 +350,7 @@ export default function SignedLeftSidebar(props) {
332350 organizations = { organizations }
333351 setSnackbar = { setSnackbar }
334352 addRoom = { addRoom }
335- rooms = { rooms }
353+ rooms = { rooms [ "conversations" ] }
336354 />
337355 ) }
338356 < Snackbar
@@ -349,24 +367,24 @@ export default function SignedLeftSidebar(props) {
349367 < div className = "signed-left-sidebar-body" >
350368 { ! groupByCommunity && (
351369 < CommunityListItem
352- community = { rooms }
370+ community = { rooms [ "conversations" ] }
353371 key = { "Conversations" }
354372 community_name = { "Conversations" }
355373 > </ CommunityListItem >
356374 ) }
357375 { groupByCommunity &&
358- Object . keys ( communities ) . map ( ( community_name ) => {
376+ Object . keys ( rooms [ " communities" ] ) . map ( ( community_name ) => {
359377 return (
360378 < CommunityListItem
361- community = { communities [ community_name ] }
379+ community = { rooms [ " communities" ] [ community_name ] }
362380 key = { community_name }
363381 community_name = { community_name }
364382 > </ CommunityListItem >
365383 ) ;
366384 } ) }
367- { groupByCommunity && directMessages . length > 0 ? (
385+ { groupByCommunity && Object . keys ( rooms [ " directMessages" ] ) . length ? (
368386 < CommunityListItem
369- community = { directMessages }
387+ community = { rooms [ " directMessages" ] }
370388 key = { "Direct Messages" }
371389 community_name = { "Direct Messages" }
372390 > </ CommunityListItem >
0 commit comments