import React, { useState } from "react"; import { observer } from "mobx-react-lite"; import type { FieldError } from "react-hook-form"; import { usePopper } from "react-popper"; import { Combobox } from "@headlessui/react"; import { Check, Clipboard, Search } from "lucide-react"; // hooks import { useProject } from "hooks/store"; // helpers import { renderEmoji } from "helpers/emoji.helper"; export interface IssueProjectSelectProps { value: string; onChange: (value: string) => void; error?: FieldError; } export const IssueProjectSelect: React.FC = observer((props) => { const { value, onChange } = props; const [query, setQuery] = useState(""); const [referenceElement, setReferenceElement] = useState(null); const [popperElement, setPopperElement] = useState(null); const { styles, attributes } = usePopper(referenceElement, popperElement, { placement: "bottom-start", }); // store hooks const { joinedProjects, getProjectById } = useProject(); const selectedProject = getProjectById(value); const options = joinedProjects?.map((projectId) => { const projectDetails = getProjectById(projectId); return { value: `${projectDetails?.id}`, query: `${projectDetails?.name}`, content: (
{projectDetails?.emoji ? renderEmoji(projectDetails?.emoji) : projectDetails?.icon_prop ? renderEmoji(projectDetails?.icon_prop) : null} {projectDetails?.name}
), }; }); const filteredOptions = query === "" ? options : options?.filter((option) => option.query.toLowerCase().includes(query.toLowerCase())); const label = selectedProject ? (
{selectedProject.emoji ? renderEmoji(selectedProject.emoji) : selectedProject.icon_prop ? renderEmoji(selectedProject.icon_prop) : null}
{selectedProject.identifier}
) : ( <> Select Project ); return ( onChange(val)}>
setQuery(e.target.value)} placeholder="Search" displayValue={(assigned: any) => assigned?.name} />
{filteredOptions ? ( filteredOptions.length > 0 ? ( filteredOptions.map((option) => ( `flex cursor-pointer select-none items-center justify-between gap-2 truncate rounded px-1 py-1.5 ${ active && !selected ? "bg-custom-background-80" : "" } w-full truncate ${selected ? "text-custom-text-100" : "text-custom-text-200"}` } > {({ selected }) => ( <> {option.content} {selected && } )} )) ) : (

No matching results

) ) : (

Loading...

)}
); });