import { UserIcon, XIcon } from '@heroicons/react/outline' import { useUsers } from 'web/hooks/use-users' import { User } from 'common/user' import { Fragment, useMemo, useState } from 'react' import clsx from 'clsx' import { Menu, Transition } from '@headlessui/react' import { Avatar } from 'web/components/avatar' import { Row } from 'web/components/layout/row' import { UserLink } from 'web/components/user-page' export function FilterSelectUsers(props: { setSelectedUsers: (users: User[]) => void selectedUsers: User[] ignoreUserIds: string[] showSelectedUsersTitle?: boolean selectedUsersClassName?: string maxUsers?: number }) { const { ignoreUserIds, selectedUsers, setSelectedUsers, showSelectedUsersTitle, selectedUsersClassName, maxUsers, } = props const users = useUsers() const [query, setQuery] = useState('') const [filteredUsers, setFilteredUsers] = useState([]) const beginQuerying = query.length > 2 useMemo(() => { if (beginQuerying) setFilteredUsers( users.filter((user: User) => { return ( !selectedUsers.map((user) => user.name).includes(user.name) && !ignoreUserIds.includes(user.id) && (user.name.toLowerCase().includes(query.toLowerCase()) || user.username.toLowerCase().includes(query.toLowerCase())) ) }) ) }, [beginQuerying, users, selectedUsers, ignoreUserIds, query]) const shouldShow = maxUsers ? selectedUsers.length < maxUsers : true return (
{shouldShow && ( <>
setQuery(e.target.value)} className="input input-bordered block w-full pl-10 focus:border-gray-300 " placeholder="Austin Chen" />
{({}) => (
{filteredUsers.map((user: User) => ( {({ active }) => ( { setQuery('') setSelectedUsers([...selectedUsers, user]) }} > {user.name} )} ))}
)}
)} {selectedUsers.length > 0 && ( <>
{showSelectedUsersTitle && 'Added members:'}
{selectedUsers.map((user: User) => (
setSelectedUsers([ ...selectedUsers.filter((u) => u.id != user.id), ]) } className=" h-5 w-5 cursor-pointer text-gray-400" aria-hidden="true" />
))}
)}
) }