import clsx from 'clsx' import { Avatar } from './avatar' import { Col } from './layout/col' import { Row } from './layout/row' import { UserLink } from './user-page' import { User } from 'common/user' import { UserCircleIcon } from '@heroicons/react/solid' import { useUsers } from 'web/hooks/use-users' import { partition } from 'lodash' import { useWindowSize } from 'web/hooks/use-window-size' import { useState } from 'react' const isOnline = (user?: User) => user && user.lastPingTime && user.lastPingTime > Date.now() - 5 * 60 * 1000 export function OnlineUserList(props: { users: User[] }) { let { users } = props const liveUsers = useUsers().filter((user) => users.map((u) => u.id).includes(user.id) ) if (liveUsers) users = liveUsers const [onlineUsers, offlineUsers] = partition(users, (user) => isOnline(user)) const { width, height } = useWindowSize() const [containerRef, setContainerRef] = useState(null) // Subtract bottom bar when it's showing (less than lg screen) const bottomBarHeight = (width ?? 0) < 1024 ? 58 : 0 const remainingHeight = (height ?? 0) - (containerRef?.offsetTop ?? 0) - bottomBarHeight return ( {onlineUsers .concat( offlineUsers.sort( (a, b) => (b.lastPingTime ?? 0) - (a.lastPingTime ?? 0) ) ) .slice(0, 15) .map((user) => ( ))} ) } export function OnlineUserAvatar(props: { user?: User className?: string size?: 'sm' | 'xs' | number }) { const { user, className, size } = props return ( {user && ( )} {isOnline(user) && (
)}
) }