2023-01-26 18:12:20 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
// services
|
|
|
|
import stateService from "services/state.service";
|
|
|
|
// headless ui
|
2023-03-02 03:31:47 +00:00
|
|
|
import {
|
|
|
|
Squares2X2Icon,
|
|
|
|
PlusIcon,
|
|
|
|
MagnifyingGlassIcon,
|
|
|
|
CheckIcon,
|
|
|
|
} from "@heroicons/react/24/outline";
|
2023-01-26 18:12:20 +00:00
|
|
|
// icons
|
|
|
|
import { Combobox, Transition } from "@headlessui/react";
|
2023-02-08 13:21:03 +00:00
|
|
|
// helpers
|
|
|
|
import { getStatesList } from "helpers/state.helper";
|
2023-01-26 18:12:20 +00:00
|
|
|
// fetch keys
|
|
|
|
import { STATE_LIST } from "constants/fetch-keys";
|
2023-03-02 06:02:18 +00:00
|
|
|
import { getStateGroupIcon } from "components/icons";
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
setIsOpen: React.Dispatch<React.SetStateAction<boolean>>;
|
|
|
|
value: string;
|
|
|
|
onChange: (value: string) => void;
|
|
|
|
projectId: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const IssueStateSelect: React.FC<Props> = ({ setIsOpen, value, onChange, projectId }) => {
|
|
|
|
// states
|
|
|
|
const [query, setQuery] = useState("");
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug } = router.query;
|
|
|
|
|
2023-02-08 13:21:03 +00:00
|
|
|
const { data: stateGroups } = useSWR(
|
2023-01-26 18:12:20 +00:00
|
|
|
workspaceSlug && projectId ? STATE_LIST(projectId) : null,
|
|
|
|
workspaceSlug && projectId
|
|
|
|
? () => stateService.getStates(workspaceSlug as string, projectId)
|
|
|
|
: null
|
|
|
|
);
|
2023-02-08 13:21:03 +00:00
|
|
|
const states = getStatesList(stateGroups ?? {});
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
const options = states?.map((state) => ({
|
|
|
|
value: state.id,
|
|
|
|
display: state.name,
|
|
|
|
color: state.color,
|
2023-03-02 06:02:18 +00:00
|
|
|
group: state.group,
|
2023-01-26 18:12:20 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
const filteredOptions =
|
|
|
|
query === ""
|
|
|
|
? options
|
|
|
|
: options?.filter((option) => option.display.toLowerCase().includes(query.toLowerCase()));
|
2023-03-02 06:02:18 +00:00
|
|
|
|
|
|
|
const currentOption = options?.find((option) => option.value === value);
|
2023-01-26 18:12:20 +00:00
|
|
|
return (
|
|
|
|
<Combobox
|
|
|
|
as="div"
|
|
|
|
value={value}
|
|
|
|
onChange={(val: any) => onChange(val)}
|
|
|
|
className="relative flex-shrink-0"
|
|
|
|
>
|
|
|
|
{({ open }: any) => (
|
|
|
|
<>
|
|
|
|
<Combobox.Button
|
2023-03-02 03:31:47 +00:00
|
|
|
className={({ open }) =>
|
2023-03-04 12:17:03 +00:00
|
|
|
`flex cursor-pointer items-center rounded-md border text-xs shadow-sm duration-200
|
2023-03-02 03:31:47 +00:00
|
|
|
${
|
2023-03-04 12:17:03 +00:00
|
|
|
open ? "border-theme bg-theme/5 outline-none ring-1 ring-theme " : "hover:bg-theme/5"
|
2023-03-02 03:31:47 +00:00
|
|
|
}`
|
|
|
|
}
|
2023-01-26 18:12:20 +00:00
|
|
|
>
|
2023-03-02 04:08:13 +00:00
|
|
|
{value && value !== "" ? (
|
2023-03-04 12:17:03 +00:00
|
|
|
<span className="flex items-center justify-center gap-2 px-3 py-1.5 text-xs">
|
2023-03-02 06:02:18 +00:00
|
|
|
{currentOption && currentOption.group
|
|
|
|
? getStateGroupIcon(currentOption.group, "16", "16", currentOption.color)
|
|
|
|
: ""}
|
2023-03-02 08:53:38 +00:00
|
|
|
<span className=" text-gray-600">{currentOption?.display}</span>
|
2023-03-02 04:08:13 +00:00
|
|
|
</span>
|
|
|
|
) : (
|
2023-03-04 12:17:03 +00:00
|
|
|
<span className="flex items-center justify-center gap-2 px-3 py-1.5 text-xs">
|
2023-03-02 04:08:13 +00:00
|
|
|
<Squares2X2Icon className="h-4 w-4 text-gray-500 " />
|
2023-03-02 08:53:38 +00:00
|
|
|
<span className=" text-gray-500">{currentOption?.display || "State"}</span>
|
2023-03-02 04:08:13 +00:00
|
|
|
</span>
|
|
|
|
)}
|
2023-01-26 18:12:20 +00:00
|
|
|
</Combobox.Button>
|
|
|
|
|
|
|
|
<Transition
|
|
|
|
show={open}
|
|
|
|
as={React.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-04 12:17:03 +00:00
|
|
|
className={`absolute z-10 mt-1 max-h-52 min-w-[8rem] overflow-auto rounded-md border-none
|
|
|
|
bg-white px-2 py-2 text-xs shadow-md focus:outline-none`}
|
2023-01-26 18:12:20 +00:00
|
|
|
>
|
2023-03-04 12:17:03 +00:00
|
|
|
<div className="flex w-full items-center justify-start rounded-sm border-[0.6px] bg-gray-100 px-2">
|
2023-03-02 03:31:47 +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 03:31:47 +00:00
|
|
|
onChange={(event) => setQuery(event.target.value)}
|
2023-03-02 04:08:13 +00:00
|
|
|
placeholder="Search States"
|
2023-03-02 03:31:47 +00:00
|
|
|
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:47 +00:00
|
|
|
className={({ active }) =>
|
|
|
|
`${
|
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-600`
|
2023-01-26 18:12:20 +00:00
|
|
|
}
|
|
|
|
value={option.value}
|
|
|
|
>
|
2023-03-02 03:31:47 +00:00
|
|
|
{({ selected, active }) =>
|
|
|
|
states && (
|
2023-03-04 12:17:03 +00:00
|
|
|
<div className="flex w-full justify-between gap-2 rounded">
|
|
|
|
<div className="flex items-center justify-start gap-2">
|
2023-03-02 06:02:18 +00:00
|
|
|
{getStateGroupIcon(option.group, "16", "16", option.color)}
|
2023-03-02 03:31:47 +00:00
|
|
|
<span>{option.display}</span>
|
|
|
|
</div>
|
2023-03-04 12:17:03 +00:00
|
|
|
<div className="flex items-center justify-center rounded p-1">
|
2023-03-02 03:31:47 +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>
|
|
|
|
))
|
|
|
|
) : (
|
2023-03-04 12:17:03 +00:00
|
|
|
<p className="px-2 text-xs text-gray-500">No states found</p>
|
2023-01-26 18:12:20 +00:00
|
|
|
)
|
|
|
|
) : (
|
2023-03-04 12:17:03 +00:00
|
|
|
<p className="px-2 text-xs text-gray-500">Loading...</p>
|
2023-01-26 18:12:20 +00:00
|
|
|
)}
|
|
|
|
<button
|
|
|
|
type="button"
|
2023-03-04 12:17:03 +00:00
|
|
|
className="flex w-full select-none items-center rounded py-2 px-1 hover:bg-gray-200"
|
2023-01-26 18:12:20 +00:00
|
|
|
onClick={() => setIsOpen(true)}
|
|
|
|
>
|
2023-03-04 12:17:03 +00:00
|
|
|
<span className="flex items-center justify-start gap-1">
|
2023-03-02 08:53:38 +00:00
|
|
|
<PlusIcon className="h-4 w-4 text-gray-600" aria-hidden="true" />
|
|
|
|
<span className="text-gray-600">Create New State</span>
|
2023-03-02 03:31:47 +00:00
|
|
|
</span>
|
2023-01-26 18:12:20 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</Combobox.Options>
|
|
|
|
</Transition>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Combobox>
|
|
|
|
);
|
|
|
|
};
|