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 { searchInAny } from 'common/util/parse' import { UserLink } from 'web/components/user-link' import { Input } from './input' 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) && searchInAny(query, user.name, user.username) ) }) ) }, [beginQuerying, users, selectedUsers, ignoreUserIds, query]) const shouldShow = maxUsers ? selectedUsers.length < maxUsers : true return (
{shouldShow && ( <>
setQuery(e.target.value)} className="block w-full pl-10" 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" />
))}
)}
) }