import React, { useState } from "react"; import { useRouter } from "next/router"; import useSWR from "swr"; // headless ui import { Combobox, Transition } from "@headlessui/react"; // services import cyclesService from "services/cycles.service"; import modulesService from "services/modules.service"; // icons import { Search } from "lucide-react"; // types import { ICustomAttribute } from "types"; // fetch-keys import { CYCLES_LIST, MODULE_LIST } from "constants/fetch-keys"; import useProjectMembers from "hooks/use-project-members"; import { Avatar } from "components/ui"; type Props = { attributeDetails: ICustomAttribute; className?: string; issueId: string; projectId: string; value: string | undefined; onChange: (val: string | undefined) => void; }; export const CustomRelationAttribute: React.FC = ({ attributeDetails, className = "", onChange, projectId, value, }) => { const router = useRouter(); const { workspaceSlug } = router.query; const [query, setQuery] = useState(""); const { data: cycles } = useSWR( workspaceSlug && projectId && attributeDetails.unit === "cycle" ? CYCLES_LIST(projectId.toString()) : null, workspaceSlug && projectId && attributeDetails.unit === "cycle" ? () => cyclesService.getCyclesWithParams(workspaceSlug.toString(), projectId.toString(), "all") : null ); const { data: modules } = useSWR( workspaceSlug && projectId && attributeDetails.unit === "module" ? MODULE_LIST(projectId as string) : null, workspaceSlug && projectId && attributeDetails.unit === "module" ? () => modulesService.getModules(workspaceSlug as string, projectId as string) : null ); const { members } = useProjectMembers(workspaceSlug?.toString(), projectId); const optionsList = attributeDetails.unit === "cycle" ? cycles?.map((c) => ({ id: c.id, query: c.name, label: c.name })) : attributeDetails.unit === "module" ? modules?.map((m) => ({ id: m.id, query: m.name, label: m.name })) : attributeDetails.unit === "user" ? members?.map((m) => ({ id: m.member.id, query: m.member.display_name, label: (
{m.member.is_bot ? m.member.first_name : m.member.display_name}
), })) : []; const selectedOption = (optionsList ?? []).find((option) => option.id === value); const options = (optionsList ?? []).filter((option) => option.query.toLowerCase().includes(query.toLowerCase()) ); return ( onChange(val)} className="relative flex-shrink-0 text-left" > {({ open }: { open: boolean }) => ( <> {selectedOption?.label ?? `Select ${attributeDetails.unit}`}
setQuery(e.target.value)} placeholder="Type to search..." displayValue={(assigned: any) => assigned?.name} />
{options ? ( options.length > 0 ? ( options.map((option) => ( {option.label} )) ) : (

No {attributeDetails.unit}s found

) ) : (

Loading...

)}
)}
); };