2023-03-04 13:40:35 +00:00
|
|
|
import React from "react";
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
// services
|
|
|
|
import stateService from "services/state.service";
|
2023-03-04 13:40:35 +00:00
|
|
|
// ui
|
|
|
|
import { CustomSearchSelect } from "components/ui";
|
2023-01-26 18:12:20 +00:00
|
|
|
// icons
|
2023-03-04 13:40:35 +00:00
|
|
|
import { PlusIcon, Squares2X2Icon } from "@heroicons/react/24/outline";
|
2023-09-11 06:15:28 +00:00
|
|
|
import { StateGroupIcon } from "components/icons";
|
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
|
2023-04-22 12:49:35 +00:00
|
|
|
import { STATES_LIST } from "constants/fetch-keys";
|
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 router = useRouter();
|
|
|
|
const { workspaceSlug } = router.query;
|
|
|
|
|
2023-02-08 13:21:03 +00:00
|
|
|
const { data: stateGroups } = useSWR(
|
2023-04-22 12:49:35 +00:00
|
|
|
workspaceSlug && projectId ? STATES_LIST(projectId) : null,
|
2023-01-26 18:12:20 +00:00
|
|
|
workspaceSlug && projectId
|
|
|
|
? () => stateService.getStates(workspaceSlug as string, projectId)
|
|
|
|
: null
|
|
|
|
);
|
2023-07-26 17:50:44 +00:00
|
|
|
const states = getStatesList(stateGroups);
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
const options = states?.map((state) => ({
|
|
|
|
value: state.id,
|
2023-03-04 13:40:35 +00:00
|
|
|
query: state.name,
|
|
|
|
content: (
|
|
|
|
<div className="flex items-center gap-2">
|
2023-09-11 06:15:28 +00:00
|
|
|
<StateGroupIcon stateGroup={state.group} color={state.color} />
|
2023-03-04 13:40:35 +00:00
|
|
|
{state.name}
|
|
|
|
</div>
|
|
|
|
),
|
2023-01-26 18:12:20 +00:00
|
|
|
}));
|
|
|
|
|
2023-03-04 13:40:35 +00:00
|
|
|
const selectedOption = states?.find((s) => s.id === value);
|
2023-07-26 17:50:44 +00:00
|
|
|
const currentDefaultState = states?.find((s) => s.default);
|
2023-03-02 06:02:18 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
return (
|
2023-03-04 13:40:35 +00:00
|
|
|
<CustomSearchSelect
|
2023-01-26 18:12:20 +00:00
|
|
|
value={value}
|
2023-03-04 13:40:35 +00:00
|
|
|
onChange={onChange}
|
|
|
|
options={options}
|
|
|
|
label={
|
2023-04-24 05:49:53 +00:00
|
|
|
<div className="flex items-center gap-2">
|
2023-03-13 18:08:43 +00:00
|
|
|
{selectedOption ? (
|
2023-09-11 06:15:28 +00:00
|
|
|
<StateGroupIcon stateGroup={selectedOption.group} color={selectedOption.color} />
|
2023-04-17 06:24:22 +00:00
|
|
|
) : currentDefaultState ? (
|
2023-09-11 06:15:28 +00:00
|
|
|
<StateGroupIcon
|
|
|
|
stateGroup={currentDefaultState.group}
|
|
|
|
color={currentDefaultState.color}
|
|
|
|
/>
|
2023-03-13 18:08:43 +00:00
|
|
|
) : (
|
2023-07-10 07:17:00 +00:00
|
|
|
<Squares2X2Icon className="h-3.5 w-3.5 text-custom-text-200" />
|
2023-03-13 18:08:43 +00:00
|
|
|
)}
|
2023-04-24 05:49:53 +00:00
|
|
|
{selectedOption?.name
|
|
|
|
? selectedOption.name
|
2023-07-10 07:17:00 +00:00
|
|
|
: currentDefaultState?.name ?? <span className="text-custom-text-200">State</span>}
|
2023-03-04 13:40:35 +00:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
footerOption={
|
|
|
|
<button
|
|
|
|
type="button"
|
2023-07-10 07:17:00 +00:00
|
|
|
className="flex w-full select-none items-center gap-2 rounded px-1 py-1.5 text-xs text-custom-text-200 hover:bg-custom-background-80"
|
2023-03-04 13:40:35 +00:00
|
|
|
onClick={() => setIsOpen(true)}
|
|
|
|
>
|
|
|
|
<PlusIcon className="h-4 w-4" aria-hidden="true" />
|
|
|
|
Create New State
|
|
|
|
</button>
|
|
|
|
}
|
2023-03-05 14:52:01 +00:00
|
|
|
noChevron
|
2023-03-04 13:40:35 +00:00
|
|
|
/>
|
2023-01-26 18:12:20 +00:00
|
|
|
);
|
|
|
|
};
|