Currently, our lobby only lists active users that we are tracking with presence. You may recall that we are also storing all of our users (not just the active users) in Postgres. What we would like to do is track presence and the lobbyList separately and use presence to highlight users in the list that are currently active and move them to the top of the list.
Fortunately, this is not difficult.
All we have to do is update our configureAdminChannel to handle lobby_list changes and update a state variable called lobbyList when things change.
We're also going to leave our channels when the component unmounts so we don't accidentally keep the connection alive for longer than necessary.
...export class Chat extends React.Component { constructor(props) { super(props) this.state = { presences: {}, messages: [], input: "", currentRoom: "", lobbyList: [] } ... } componentWillUnmount() { if (this.channel) this.channel.leave() if (this.adminChannel) this.adminChannel.leave() } configureAdminChannel() { this.adminChannel = this.socket.channel("admin:active_users") ... this.adminChannel.on("lobby_list", (user) => { if (!this.state.lobbyList.includes(user)) { this.setState({ lobbyList: this.state.lobbyList.concat([user]) }) } }) this.adminChannel.join() .receive("ok", ({ id, lobby_list }) => { console.log(`${id} succesfully joined the active_users topic.`) this.setState({ lobbyList: lobby_list }) }) } ...}Then we need to pass our lobbyList that we get from our backend and store in local state to our Sidebar component. We will eventually refactor this and move this data into Redux, but are handling it locally now to make it easier to see where the data is coming from.
...export class Chat extends React.Component { ... render() { return ( <div> <Sidebar currentRoom={this.state.currentRoom} presences={this.state.presences} lobbyList={this.state.lobbyList} onRoomClick={this.changeChatroom} /> <ChatRoom input={this.state.input} handleChange={this.handleChange} handleMessageSubmit={this.handleMessageSubmit} currentRoom={this.state.currentRoom} messages={this.state.messages} /> { this.props.children } </div> ) }}So at this point, we're getting a list of all anonymous users in the lobby and passing it along to our Sidebar. The next step is to take those users and render them in a list.
We're going to add a bunch of new functions that will allow us to order our users in a meaningful way, then find users who are active and give them some special indicator.
The first function we're changing is our listBy function. Since we don't care about the number of devices, we're just going to pass in an anonymous function that returns the id.
Next we're creating an orderByActivity function, which we're going to use to order our list of users. The way this works is it takes in two values. If both users have the same activity, it doesn't change the position. If the users are not the same (one is active, one is not), then if the current user is active (a.active === true), we push it up one on the list. Otherwise, the user is not active and we push it down one on the list.
...const orderByActivity = (a, b) => { if (a.active === b.active) return 0 if (b.active === true) return 1 return -1}...Now we need to update our renderList function to render our users. The activeList const is a list of all our active users. We're eventually going to use this list of users to compare against our list of all users. When there's a match, that means that the user is active and we will format that user differently. If no match, then the user belongs in the list but is not currently present on the socket.
The lobbyList function is where we merge the two lists together to determine which users are active and which are not. We start by mapping over each user in props.lobbyList (which contains all users that have joined the lobby), then we check to see if activeList (which contains all our present users) contains the current user with Array.includes. If the user ids match, then the user is deemed "active", so we set active to true (Array.includes returns true or false).
The last function is renderList, which now uses Array.sort and our orderByActivity function to put the most recently active users at the top of the list. Then we map over each and assign an inset box-shadow to all active users. Finally, we return the jsx object to render in the list.
Also, now that we have access to the name and avatar of each user, we can use that instead of the random series of number in the uuid to identify them.
...const renderList = (props) => { const activeList = Presence.list(props.presences, (id, _metas) => id) const lobbyList = props.lobbyList.map(({ id, name, avatar }) => { const active = activeList.includes(id) return { name, avatar, id, active } }) return lobbyList .sort(orderByActivity) .map(({ id, active, name, avatar }) => { const newStyle = {} if (active) newStyle.boxShadow = "inset 0px 0px 6px 4px rgba(58, 155, 207, 0.6)" if (props.currentRoom === id) newStyle.background = "#ddd" return ( <div style={newStyle} className={style.user} key={id} onClick={() => { props.onRoomClick(id) }}> <div> <img alt="user avatar" src={avatar} /> </div> <div> { name } </div> </div> ) })}...Let's also add some styling to make our users easier to distinguish.
... .user { display: flex; align-items: center; padding: 0.5rem; border-bottom: 1px solid rgba(0, 0, 0, 0.1); border-bottom-width: 80%; min-height: 50px; cursor: pointer; background: white; transition: background 0.1s ease; box-shadow: 0px 4px 4px -2px rgba(0, 0, 0, 0.2);}.user:hover { background: #ddd;}We now have a functional list of users, prioritized by activity level. The admin can also click on any of them and respond.
We should also give some indicator for when a no room is selected. Otherwise, people might just assume that the app is broken. We'll do this in a new renderEmpty function.
... renderEmpty() { if (this.props.currentRoom) return null return ( <div className={style.empty}> No chat selected </div> ) } ...And some styling for our placeholder.
... .empty { display: flex; align-items: center; justify-content: center; height: 80%; color: #ccc; font-size: 2em;}At this point, we can now see active and inactive users and respond to them properly. We're also sorting the users by giving priority to those who are currently present.
Previous: Change Room and Respond to MessagesNext: Style the Chat Component