2021-02-10 05:57:25 -05:00
|
|
|
import React from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { Dropdown, MenuItem, OverlayTrigger, Tooltip } from 'react-bootstrap'
|
|
|
|
import Icon from '../../../shared/components/icon'
|
2021-05-18 09:43:36 -04:00
|
|
|
import { getHueForUserId } from '../../../shared/utils/colors'
|
2021-06-18 05:29:06 -04:00
|
|
|
import ControlledDropdown from '../../../shared/components/controlled-dropdown'
|
2021-02-10 05:57:25 -05:00
|
|
|
|
|
|
|
function OnlineUsersWidget({ onlineUsers, goToUser }) {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
|
|
|
|
const shouldDisplayDropdown = onlineUsers.length >= 4
|
|
|
|
|
|
|
|
if (shouldDisplayDropdown) {
|
|
|
|
return (
|
2021-06-18 05:29:06 -04:00
|
|
|
<ControlledDropdown id="online-users" className="online-users" pullRight>
|
2021-02-10 05:57:25 -05:00
|
|
|
<DropDownToggleButton
|
|
|
|
bsRole="toggle"
|
|
|
|
onlineUserCount={onlineUsers.length}
|
|
|
|
/>
|
|
|
|
<Dropdown.Menu>
|
|
|
|
<MenuItem header>{t('connected_users')}</MenuItem>
|
2021-06-30 07:03:13 -04:00
|
|
|
{onlineUsers.map((user, index) => (
|
2021-02-10 05:57:25 -05:00
|
|
|
<MenuItem
|
|
|
|
as="button"
|
2021-06-30 07:03:13 -04:00
|
|
|
key={`${user.user_id}_${index}`}
|
2021-02-10 05:57:25 -05:00
|
|
|
eventKey={user}
|
|
|
|
onSelect={goToUser}
|
|
|
|
>
|
|
|
|
<UserIcon user={user} onClick={goToUser} showName />
|
|
|
|
</MenuItem>
|
|
|
|
))}
|
|
|
|
</Dropdown.Menu>
|
2021-06-18 05:29:06 -04:00
|
|
|
</ControlledDropdown>
|
2021-02-10 05:57:25 -05:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<div className="online-users">
|
2021-06-30 07:03:13 -04:00
|
|
|
{onlineUsers.map((user, index) => (
|
2021-02-10 05:57:25 -05:00
|
|
|
<OverlayTrigger
|
2021-06-30 07:03:13 -04:00
|
|
|
key={`${user.user_id}_${index}`}
|
2021-02-10 05:57:25 -05:00
|
|
|
placement="bottom"
|
|
|
|
trigger={['hover', 'focus']}
|
|
|
|
overlay={<Tooltip id="tooltip-online-user">{user.name}</Tooltip>}
|
|
|
|
>
|
|
|
|
<span>
|
|
|
|
{/* OverlayTrigger won't fire unless UserIcon is wrapped in a span */}
|
|
|
|
<UserIcon user={user} onClick={goToUser} />
|
|
|
|
</span>
|
|
|
|
</OverlayTrigger>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OnlineUsersWidget.propTypes = {
|
|
|
|
onlineUsers: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape({
|
|
|
|
user_id: PropTypes.string.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
name: PropTypes.string.isRequired,
|
2021-02-10 05:57:25 -05:00
|
|
|
})
|
|
|
|
).isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
goToUser: PropTypes.func.isRequired,
|
2021-02-10 05:57:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function UserIcon({ user, showName, onClick }) {
|
2021-05-18 09:43:36 -04:00
|
|
|
const backgroundColor = `hsl(${getHueForUserId(user.user_id)}, 70%, 50%)`
|
2021-02-10 05:57:25 -05:00
|
|
|
|
|
|
|
function handleOnClick() {
|
|
|
|
onClick(user)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
|
|
|
|
<span onClick={handleOnClick}>
|
|
|
|
<span className="online-user" style={{ backgroundColor }}>
|
|
|
|
{user.name.slice(0, 1)}
|
|
|
|
</span>
|
|
|
|
{showName && user.name}
|
|
|
|
</span>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
UserIcon.propTypes = {
|
|
|
|
user: PropTypes.shape({
|
|
|
|
user_id: PropTypes.string.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
name: PropTypes.string.isRequired,
|
2021-02-10 05:57:25 -05:00
|
|
|
}),
|
|
|
|
showName: PropTypes.bool,
|
2021-04-27 03:52:58 -04:00
|
|
|
onClick: PropTypes.func.isRequired,
|
2021-02-10 05:57:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const DropDownToggleButton = React.forwardRef((props, ref) => {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
return (
|
|
|
|
<OverlayTrigger
|
|
|
|
placement="left"
|
|
|
|
overlay={
|
|
|
|
<Tooltip id="tooltip-connected-users">{t('connected_users')}</Tooltip>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<button
|
|
|
|
className="btn online-user online-user-multi"
|
|
|
|
onClick={props.onClick} // required by Bootstrap Dropdown to trigger an opening
|
|
|
|
>
|
|
|
|
<strong>{props.onlineUserCount}</strong>
|
2022-01-19 06:56:57 -05:00
|
|
|
<Icon type="users" fw />
|
2021-02-10 05:57:25 -05:00
|
|
|
</button>
|
|
|
|
</OverlayTrigger>
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-05-13 06:21:35 -04:00
|
|
|
DropDownToggleButton.displayName = 'DropDownToggleButton'
|
|
|
|
|
2021-02-10 05:57:25 -05:00
|
|
|
DropDownToggleButton.propTypes = {
|
|
|
|
onlineUserCount: PropTypes.number.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
onClick: PropTypes.func,
|
2021-02-10 05:57:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default OnlineUsersWidget
|