import { Group } from 'common/group' import { Combobox } from '@headlessui/react' import { InfoTooltip } from 'web/components/info-tooltip' import { CheckIcon, PlusCircleIcon, SelectorIcon, UserIcon, } from '@heroicons/react/outline' import clsx from 'clsx' import { CreateGroupButton } from 'web/components/groups/create-group-button' import { useState } from 'react' import { useMemberGroups, useOpenGroups } from 'web/hooks/use-group' import { User } from 'common/user' import { searchInAny } from 'common/util/parse' import { Row } from 'web/components/layout/row' export function GroupSelector(props: { selectedGroup: Group | undefined setSelectedGroup: (group: Group) => void creator: User | null | undefined options: { showSelector: boolean showLabel: boolean ignoreGroupIds?: string[] } }) { const { selectedGroup, setSelectedGroup, creator, options } = props const [isCreatingNewGroup, setIsCreatingNewGroup] = useState(false) const { showSelector, showLabel, ignoreGroupIds } = options const [query, setQuery] = useState('') const openGroups = useOpenGroups() const memberGroups = useMemberGroups(creator?.id) const memberGroupIds = memberGroups?.map((g) => g.id) ?? [] const sortGroups = (groups: Group[]) => groups.sort( (a, b) => // weight group higher if user is a member (memberGroupIds.includes(b.id) ? 5 : 1) * b.totalContracts - (memberGroupIds.includes(a.id) ? 5 : 1) * a.totalContracts ) const availableGroups = sortGroups( openGroups .concat( (memberGroups ?? []).filter( (g) => !openGroups.map((og) => og.id).includes(g.id) ) ) .filter((group) => !ignoreGroupIds?.includes(group.id)) ) const filteredGroups = sortGroups( availableGroups.filter((group) => searchInAny(query, group.name)) ) if (!showSelector || !creator) { return ( <>
In Group: {selectedGroup ? ( {selectedGroup?.name} ) : ( (None) )}
) } return (
{() => ( <> {showLabel && ( Add to Group )}
setQuery(event.target.value)} displayValue={(group: Group) => group && group.name} placeholder={'E.g. Science, Politics'} /> {filteredGroups.map((group: Group) => ( clsx( 'relative h-12 cursor-pointer select-none py-2 pr-6', active ? 'bg-indigo-500 text-white' : 'text-gray-900' ) } > {({ active, selected }) => ( <> {selected && ( )} {memberGroupIds.includes(group.id) && ( )} {group.name} 99 ? 'w-[2.1rem]' : '' )} > {group.totalContracts > 99 ? '99+' : group.totalContracts} )} ))} } />
)}
) }