import React, { useRef, useState } from "react"; import { useRouter } from "next/router"; // hooks import useDynamicDropdownPosition from "hooks/use-dynamic-dropdown"; import useProjectMembers from "hooks/use-project-members"; import useWorkspaceMembers from "hooks/use-workspace-members"; // headless ui import { Combobox } from "@headlessui/react"; // components import { AssigneesList, Avatar, Icon, Tooltip } from "components/ui"; // icons import { ChevronDownIcon } from "@heroicons/react/20/solid"; import { CheckIcon, MagnifyingGlassIcon } from "@heroicons/react/24/outline"; // types import { IUser } from "types"; type Props = { value: string | string[]; projectId: string; onChange: (data: any) => void; membersDetails: IUser[]; renderWorkspaceMembers?: boolean; className?: string; buttonClassName?: string; optionsClassName?: string; hideDropdownArrow?: boolean; disabled?: boolean; }; export const MembersSelect: React.FC = ({ value, projectId, onChange, membersDetails, renderWorkspaceMembers = false, className = "", buttonClassName = "", optionsClassName = "", hideDropdownArrow = false, disabled = false, }) => { const [query, setQuery] = useState(""); const [isOpen, setIsOpen] = useState(false); const [fetchStates, setFetchStates] = useState(false); const router = useRouter(); const { workspaceSlug } = router.query; const dropdownBtn = useRef(null); const dropdownOptions = useRef(null); const { members } = useProjectMembers( workspaceSlug?.toString(), projectId, fetchStates && !renderWorkspaceMembers ); const { workspaceMembers } = useWorkspaceMembers( workspaceSlug?.toString() ?? "", fetchStates && renderWorkspaceMembers ); const membersOptions = renderWorkspaceMembers ? workspaceMembers : members; const options = membersOptions?.map((member) => ({ value: member.member.id, query: member.member.display_name, content: (
{member.member.display_name}
), })); const filteredOptions = query === "" ? options : options?.filter((option) => option.query.toLowerCase().includes(query.toLowerCase())); const label = ( 0 ? membersDetails.map((assignee) => assignee?.display_name).join(", ") : "No Assignee" } position="top" >
{value && value.length > 0 && Array.isArray(value) ? ( ) : ( )}
); useDynamicDropdownPosition(isOpen, () => setIsOpen(false), dropdownBtn, dropdownOptions); return ( {({ open }: { open: boolean }) => { if (open) { if (!isOpen) setIsOpen(true); setFetchStates(true); } else if (isOpen) setIsOpen(false); return ( <> {label} {!hideDropdownArrow && !disabled && (
setQuery(e.target.value)} placeholder="Search" displayValue={(assigned: any) => assigned?.name} />
{filteredOptions ? ( filteredOptions.length > 0 ? ( filteredOptions.map((option) => ( `flex items-center justify-between gap-2 cursor-pointer select-none truncate rounded px-1 py-1.5 ${ active && !selected ? "bg-custom-background-80" : "" } ${selected ? "text-custom-text-100" : "text-custom-text-200"}` } > {({ selected }) => ( <> {option.content} {selected && } )} )) ) : (

No matching results

) ) : (

Loading...

)}
); }}
); };