2023-01-26 18:12:20 +00:00
|
|
|
import { useState, FC, Fragment } from "react";
|
|
|
|
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
// headless ui
|
|
|
|
import { Transition, Combobox } from "@headlessui/react";
|
2023-02-16 06:33:52 +00:00
|
|
|
// services
|
|
|
|
import projectServices from "services/project.service";
|
|
|
|
// ui
|
2023-02-17 13:37:36 +00:00
|
|
|
import { AssigneesList, Avatar } from "components/ui";
|
2023-03-01 10:40:48 +00:00
|
|
|
// icons
|
2023-03-02 02:33:41 +00:00
|
|
|
import { UserGroupIcon, MagnifyingGlassIcon, CheckIcon } from "@heroicons/react/24/outline";
|
2023-03-01 10:40:48 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
// fetch keys
|
|
|
|
import { PROJECT_MEMBERS } from "constants/fetch-keys";
|
|
|
|
|
|
|
|
export type IssueAssigneeSelectProps = {
|
|
|
|
projectId: string;
|
|
|
|
value: string[];
|
|
|
|
onChange: (value: string[]) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const IssueAssigneeSelect: FC<IssueAssigneeSelectProps> = ({
|
|
|
|
projectId,
|
|
|
|
value = [],
|
|
|
|
onChange,
|
|
|
|
}) => {
|
|
|
|
// states
|
|
|
|
const [query, setQuery] = useState("");
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug } = router.query;
|
|
|
|
|
|
|
|
// fetching project members
|
|
|
|
const { data: people } = useSWR(
|
|
|
|
workspaceSlug && projectId ? PROJECT_MEMBERS(projectId as string) : null,
|
|
|
|
workspaceSlug && projectId
|
|
|
|
? () => projectServices.projectMembers(workspaceSlug as string, projectId as string)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
|
|
|
const options = people?.map((person) => ({
|
|
|
|
value: person.member.id,
|
|
|
|
display:
|
|
|
|
person.member.first_name && person.member.first_name !== ""
|
|
|
|
? person.member.first_name
|
|
|
|
: person.member.email,
|
|
|
|
}));
|
|
|
|
|
|
|
|
const filteredOptions =
|
|
|
|
query === ""
|
|
|
|
? options
|
|
|
|
: options?.filter((option) => option.display.toLowerCase().includes(query.toLowerCase()));
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Combobox
|
|
|
|
as="div"
|
|
|
|
value={value}
|
|
|
|
onChange={(val) => onChange(val)}
|
|
|
|
className="relative flex-shrink-0"
|
|
|
|
multiple
|
|
|
|
>
|
|
|
|
{({ open }: any) => (
|
|
|
|
<>
|
2023-03-02 02:33:41 +00:00
|
|
|
<Combobox.Button
|
|
|
|
className={({ open }) =>
|
2023-03-02 08:06:20 +00:00
|
|
|
`flex items-center text-xs cursor-pointer border rounded-md shadow-sm duration-200
|
2023-03-02 02:33:41 +00:00
|
|
|
${
|
|
|
|
open
|
2023-03-02 08:53:38 +00:00
|
|
|
? "outline-none border-theme bg-theme/5 ring-1 ring-theme "
|
|
|
|
: "hover:bg-theme/5 "
|
2023-03-02 02:33:41 +00:00
|
|
|
}`
|
|
|
|
}
|
|
|
|
>
|
2023-03-02 04:08:13 +00:00
|
|
|
{value && value.length > 0 && Array.isArray(value) ? (
|
|
|
|
<span className="flex items-center justify-center text-xs gap-2 px-3 py-1">
|
|
|
|
<AssigneesList userIds={value} length={3} showLength={false} />
|
2023-03-02 08:53:38 +00:00
|
|
|
<span className=" text-gray-500">{value.length} Assignees</span>
|
2023-03-02 04:08:13 +00:00
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
<span className="flex items-center justify-center text-xs gap-2 px-3 py-1.5">
|
|
|
|
<UserGroupIcon className="h-4 w-4 text-gray-500 " />
|
2023-03-02 08:53:38 +00:00
|
|
|
<span className=" text-gray-500">Assignee</span>
|
2023-03-02 04:08:13 +00:00
|
|
|
</span>
|
|
|
|
)}
|
2023-01-26 18:12:20 +00:00
|
|
|
</Combobox.Button>
|
|
|
|
|
|
|
|
<Transition
|
|
|
|
show={open}
|
|
|
|
as={Fragment}
|
2023-03-02 08:06:20 +00:00
|
|
|
enter="transition ease-out duration-200"
|
|
|
|
enterFrom="opacity-0 translate-y-1"
|
|
|
|
enterTo="opacity-100 translate-y-0"
|
|
|
|
leave="transition ease-in duration-150"
|
|
|
|
leaveFrom="opacity-100 translate-y-0"
|
|
|
|
leaveTo="opacity-0 translate-y-1"
|
2023-01-26 18:12:20 +00:00
|
|
|
>
|
|
|
|
<Combobox.Options
|
2023-03-02 04:08:13 +00:00
|
|
|
className={`absolute z-10 max-h-52 min-w-[8rem] mt-1 px-2 py-2 text-xs
|
|
|
|
rounded-md shadow-md overflow-auto border-none bg-white focus:outline-none`}
|
2023-01-26 18:12:20 +00:00
|
|
|
>
|
2023-03-02 08:53:38 +00:00
|
|
|
<div className="flex justify-start items-center rounded-sm border-[0.6px] bg-gray-100 border-gray-200 w-full px-2">
|
2023-03-02 02:33:41 +00:00
|
|
|
<MagnifyingGlassIcon className="h-3 w-3 text-gray-500" />
|
|
|
|
<Combobox.Input
|
2023-03-02 08:53:38 +00:00
|
|
|
className="w-full bg-transparent py-1 px-2 text-xs text-gray-500 focus:outline-none"
|
2023-03-02 02:33:41 +00:00
|
|
|
onChange={(event) => setQuery(event.target.value)}
|
|
|
|
placeholder="Search for a person..."
|
|
|
|
displayValue={(assigned: any) => assigned?.name}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="py-1.5">
|
2023-01-26 18:12:20 +00:00
|
|
|
{filteredOptions ? (
|
|
|
|
filteredOptions.length > 0 ? (
|
|
|
|
filteredOptions.map((option) => (
|
|
|
|
<Combobox.Option
|
|
|
|
key={option.value}
|
2023-03-02 03:31:16 +00:00
|
|
|
className={({ active }) =>
|
2023-03-02 02:33:41 +00:00
|
|
|
`${
|
2023-03-02 08:53:38 +00:00
|
|
|
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-500`
|
2023-01-26 18:12:20 +00:00
|
|
|
}
|
|
|
|
value={option.value}
|
|
|
|
>
|
2023-03-02 02:33:41 +00:00
|
|
|
{({ selected, active }) => (
|
|
|
|
<div className="flex w-full gap-2 justify-between rounded">
|
|
|
|
<div className="flex justify-start items-center gap-1">
|
|
|
|
<Avatar
|
|
|
|
user={people?.find((p) => p.member.id === option.value)?.member}
|
|
|
|
/>
|
|
|
|
<span>{option.display}</span>
|
|
|
|
</div>
|
|
|
|
<div
|
2023-03-02 08:53:38 +00:00
|
|
|
className={`flex justify-center items-center p-1 rounded border border-gray-500 border-opacity-0 group-hover:border-opacity-100
|
2023-03-02 02:33:41 +00:00
|
|
|
${selected ? "border-opacity-100 " : ""}
|
2023-03-02 08:53:38 +00:00
|
|
|
${active ? "bg-gray-100" : ""} `}
|
2023-03-02 02:33:41 +00:00
|
|
|
>
|
|
|
|
<CheckIcon
|
|
|
|
className={`h-3 w-3 ${selected ? "opacity-100" : "opacity-0"}`}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-01-26 18:12:20 +00:00
|
|
|
)}
|
|
|
|
</Combobox.Option>
|
|
|
|
))
|
|
|
|
) : (
|
|
|
|
<p className="text-xs text-gray-500 px-2">No assignees found</p>
|
|
|
|
)
|
|
|
|
) : (
|
|
|
|
<p className="text-xs text-gray-500 px-2">Loading...</p>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</Combobox.Options>
|
|
|
|
</Transition>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Combobox>
|
|
|
|
);
|
|
|
|
};
|