import { Fragment, useState } from "react"; import { observer } from "mobx-react-lite"; import { useMobxStore } from "lib/mobx/store-provider"; // hooks import { usePopper } from "react-popper"; // components import { Combobox } from "@headlessui/react"; import { Tooltip } from "@plane/ui"; import { Check, ChevronDown, Search } from "lucide-react"; // types import { Placement } from "@popperjs/core"; import { RootStore } from "store/root"; export interface IIssuePropertyLabels { view?: "profile" | "workspace" | "project"; projectId: string | null; value: string[]; onChange: (data: string[]) => void; disabled?: boolean; hideDropdownArrow?: boolean; className?: string; buttonClassName?: string; optionsClassName?: string; placement?: Placement; maxRender?: number; noLabelBorder?: boolean; } export const IssuePropertyLabels: React.FC = observer((props) => { const { view, projectId, value, onChange, disabled, hideDropdownArrow = false, className, buttonClassName, optionsClassName, placement, maxRender = 2, noLabelBorder = false, } = props; const { workspace: workspaceStore, project: projectStore }: RootStore = useMobxStore(); const workspaceSlug = workspaceStore?.workspaceSlug; const [query, setQuery] = useState(""); const [referenceElement, setReferenceElement] = useState(null); const [popperElement, setPopperElement] = useState(null); const [isLoading, setIsLoading] = useState(false); const projectLabels = projectId && projectStore?.labels?.[projectId]; const fetchProjectLabels = () => { setIsLoading(true); if (workspaceSlug && projectId) projectStore.fetchProjectLabels(workspaceSlug, projectId).then(() => setIsLoading(false)); }; const options = (projectLabels ? projectLabels : []).map((label) => ({ value: label.id, query: label.name, content: (
{label.name}
), })); const filteredOptions = query === "" ? options : options?.filter((option) => option.query.toLowerCase().includes(query.toLowerCase())); const { styles, attributes } = usePopper(referenceElement, popperElement, { placement: placement ?? "bottom-start", modifiers: [ { name: "preventOverflow", options: { padding: 12, }, }, ], }); const label = (
{value.length > 0 ? ( value.length <= maxRender ? ( <> {(projectLabels ? projectLabels : []) ?.filter((l) => value.includes(l.id)) .map((label) => (
{label.name}
))} ) : (
value.includes(l.id)) .map((l) => l.name) .join(", ")} >
{`${value.length} Labels`}
) ) : (
Select labels
)}
); return (
setQuery(e.target.value)} placeholder="Search" displayValue={(assigned: any) => assigned?.name} />
{isLoading ? (

Loading...

) : 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 ? "bg-custom-background-80" : "" } ${selected ? "text-custom-text-100" : "text-custom-text-200"}` } > {({ selected }) => ( <> {option.content} {selected && } )} )) ) : (

No matching results

)}
); });