import React, { useState } from "react"; import { useRouter } from "next/router"; import useSWR from "swr"; // services import stateService from "services/state.service"; // headless ui import { Squares2X2Icon, PlusIcon, MagnifyingGlassIcon, CheckIcon, } from "@heroicons/react/24/outline"; // icons import { Combobox, Transition } from "@headlessui/react"; // helpers import { getStatesList } from "helpers/state.helper"; // fetch keys import { STATE_LIST } from "constants/fetch-keys"; import { getStateGroupIcon } from "components/icons"; type Props = { setIsOpen: React.Dispatch>; value: string; onChange: (value: string) => void; projectId: string; }; export const IssueStateSelect: React.FC = ({ setIsOpen, value, onChange, projectId }) => { // states const [query, setQuery] = useState(""); const router = useRouter(); const { workspaceSlug } = router.query; const { data: stateGroups } = useSWR( workspaceSlug && projectId ? STATE_LIST(projectId) : null, workspaceSlug && projectId ? () => stateService.getStates(workspaceSlug as string, projectId) : null ); const states = getStatesList(stateGroups ?? {}); const options = states?.map((state) => ({ value: state.id, display: state.name, color: state.color, group: state.group, })); const filteredOptions = query === "" ? options : options?.filter((option) => option.display.toLowerCase().includes(query.toLowerCase())); const currentOption = options?.find((option) => option.value === value); return ( onChange(val)} className="relative flex-shrink-0" > {({ open }: any) => ( <> `flex items-center text-xs cursor-pointer border rounded-md shadow-sm duration-200 ${ open ? "outline-none border-theme bg-theme/5 ring-1 ring-theme " : "hover:bg-theme/5" }` } > {value && value !== "" ? ( {currentOption && currentOption.group ? getStateGroupIcon(currentOption.group, "16", "16", currentOption.color) : ""} {currentOption?.display} ) : ( {currentOption?.display || "State"} )}
setQuery(event.target.value)} placeholder="Search States" displayValue={(assigned: any) => assigned?.name} />
{filteredOptions ? ( filteredOptions.length > 0 ? ( filteredOptions.map((option) => ( `${ active ? "bg-gray-200" : "" } group flex min-w-[14rem] cursor-pointer select-none items-center gap-2 truncate rounded px-1 py-1.5 text-gray-600` } value={option.value} > {({ selected, active }) => states && (
{getStateGroupIcon(option.group, "16", "16", option.color)} {option.display}
) }
)) ) : (

No states found

) ) : (

Loading...

)}
)}
); };