import React from "react"; import { useRouter } from "next/router"; import useSWR from "swr"; // services import stateService from "services/state.service"; // ui import { Spinner, CustomSelect } from "components/ui"; // icons import { Squares2X2Icon } from "@heroicons/react/24/outline"; import { getStateGroupIcon } from "components/icons"; // helpers import { getStatesList } from "helpers/state.helper"; import { addSpaceIfCamelCase } from "helpers/string.helper"; // types import { UserAuth } from "types"; // constants import { STATE_LIST } from "constants/fetch-keys"; type Props = { value: string; onChange: (val: string) => void; userAuth: UserAuth; }; export const SidebarStateSelect: React.FC = ({ value, onChange, userAuth }) => { const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { data: stateGroups } = useSWR( workspaceSlug && projectId ? STATE_LIST(projectId as string) : null, workspaceSlug && projectId ? () => stateService.getStates(workspaceSlug as string, projectId as string) : null ); const states = getStatesList(stateGroups ?? {}); const selectedState = states?.find((s) => s.id === value); const isNotAllowed = userAuth.isGuest || userAuth.isViewer; return (

State

{getStateGroupIcon( selectedState?.group ?? "backlog", "16", "16", selectedState?.color ?? "" )} {addSpaceIfCamelCase(selectedState?.name ?? "")}
} value={value} onChange={onChange} width="w-full" position="right" disabled={isNotAllowed} > {states ? ( states.length > 0 ? ( states.map((state) => ( <> {getStateGroupIcon(state.group, "16", "16", state.color)} {state.name} )) ) : (
No states found
) ) : ( )}
); };