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";
|
|
|
|
|
|
|
|
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,
|
|
|
|
}));
|
|
|
|
|
|
|
|
const filteredOptions =
|
|
|
|
query === ""
|
|
|
|
? options
|
|
|
|
: options?.filter((option) => option.display.toLowerCase().includes(query.toLowerCase()));
|
|
|
|
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 }) =>
|
|
|
|
`flex items-center text-xs cursor-pointer border rounded-md shadow-sm duration-300
|
|
|
|
${
|
|
|
|
open
|
|
|
|
? "outline-none border-[#3F76FF] bg-[rgba(63,118,255,0.05)] ring-1 ring-[#3F76FF] "
|
|
|
|
: "hover:bg-[rgba(63,118,255,0.05)] focus:bg-[rgba(63,118,255,0.05)]"
|
|
|
|
}`
|
|
|
|
}
|
2023-01-26 18:12:20 +00:00
|
|
|
>
|
2023-03-02 04:08:13 +00:00
|
|
|
{value && value !== "" ? (
|
|
|
|
<span className="flex items-center justify-center text-xs gap-2 px-3 py-1.5">
|
|
|
|
<span
|
|
|
|
className="h-3 w-3 flex-shrink-0 rounded-full"
|
|
|
|
style={{
|
|
|
|
backgroundColor: options?.find((option) => option.value === value)?.color,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<span className=" text-[#495057]">
|
|
|
|
{options?.find((option) => option.value === value)?.display}
|
2023-03-02 03:31:47 +00:00
|
|
|
</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">
|
|
|
|
<Squares2X2Icon className="h-4 w-4 text-gray-500 " />
|
|
|
|
<span className=" text-[#858E96]">
|
|
|
|
{options?.find((option) => option.value === value)?.display || "State"}
|
2023-03-02 03:31:47 +00:00
|
|
|
</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}
|
|
|
|
leave="transition ease-in duration-100"
|
|
|
|
leaveFrom="opacity-100"
|
|
|
|
leaveTo="opacity-0"
|
|
|
|
>
|
|
|
|
<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 03:31:47 +00:00
|
|
|
<div className="flex justify-start items-center rounded-sm border-[0.6px] bg-[#FAFAFA] border-[#E2E2E2] w-full px-2">
|
|
|
|
<MagnifyingGlassIcon className="h-3 w-3 text-gray-500" />
|
|
|
|
<Combobox.Input
|
|
|
|
className="w-full bg-transparent py-1 px-2 text-xs text-[#888888] focus:outline-none"
|
|
|
|
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 }) =>
|
|
|
|
`${
|
|
|
|
active ? "bg-[#E9ECEF]" : ""
|
|
|
|
} group flex min-w-[14rem] cursor-pointer select-none items-center gap-2 truncate rounded px-1 py-1.5 text-[#495057]`
|
2023-01-26 18:12:20 +00:00
|
|
|
}
|
|
|
|
value={option.value}
|
|
|
|
>
|
2023-03-02 03:31:47 +00:00
|
|
|
{({ selected, active }) =>
|
|
|
|
states && (
|
|
|
|
<div className="flex w-full gap-2 justify-between rounded">
|
|
|
|
<div className="flex justify-start items-center gap-2">
|
|
|
|
<span
|
|
|
|
className="h-3 w-3 flex-shrink-0 rounded-full"
|
|
|
|
style={{
|
|
|
|
backgroundColor: option.color,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<span>{option.display}</span>
|
|
|
|
</div>
|
|
|
|
<div className="flex justify-center items-center p-1 rounded">
|
|
|
|
<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 states found</p>
|
|
|
|
)
|
|
|
|
) : (
|
|
|
|
<p className="text-xs text-gray-500 px-2">Loading...</p>
|
|
|
|
)}
|
|
|
|
<button
|
|
|
|
type="button"
|
2023-03-02 03:31:47 +00:00
|
|
|
className="flex select-none w-full items-center py-2 px-1 rounded hover:bg-[#E9ECEF]"
|
2023-01-26 18:12:20 +00:00
|
|
|
onClick={() => setIsOpen(true)}
|
|
|
|
>
|
2023-03-02 03:31:47 +00:00
|
|
|
<span className="flex justify-start items-center gap-1">
|
|
|
|
<PlusIcon className="h-4 w-4 text-[#495057]" aria-hidden="true" />
|
|
|
|
<span className="text-[#495057]">Create New State</span>
|
|
|
|
</span>
|
2023-01-26 18:12:20 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</Combobox.Options>
|
|
|
|
</Transition>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Combobox>
|
|
|
|
);
|
|
|
|
};
|