import React from "react"; import { useRouter } from "next/router"; import useSWR, { mutate } from "swr"; // services import issuesService from "services/issues.service"; import cyclesService from "services/cycles.service"; // ui import { Spinner, CustomSelect, Tooltip } from "components/ui"; // helper import { truncateText } from "helpers/string.helper"; // icons import { ContrastIcon } from "components/icons"; // types import { ICycle, IIssue, UserAuth } from "types"; // fetch-keys import { CYCLE_ISSUES, INCOMPLETE_CYCLES_LIST, ISSUE_DETAILS } from "constants/fetch-keys"; type Props = { issueDetail: IIssue | undefined; handleCycleChange: (cycle: ICycle) => void; userAuth: UserAuth; disabled?: boolean; }; export const SidebarCycleSelect: React.FC = ({ issueDetail, handleCycleChange, userAuth, disabled = false, }) => { const router = useRouter(); const { workspaceSlug, projectId, issueId } = router.query; const { data: incompleteCycles } = useSWR( workspaceSlug && projectId ? INCOMPLETE_CYCLES_LIST(projectId as string) : null, workspaceSlug && projectId ? () => cyclesService.getCyclesWithParams( workspaceSlug as string, projectId as string, "incomplete" ) : null ); const removeIssueFromCycle = (bridgeId: string, cycleId: string) => { if (!workspaceSlug || !projectId) return; issuesService .removeIssueFromCycle(workspaceSlug as string, projectId as string, cycleId, bridgeId) .then((res) => { mutate(ISSUE_DETAILS(issueId as string)); mutate(CYCLE_ISSUES(cycleId)); }) .catch((e) => { console.log(e); }); }; const issueCycle = issueDetail?.issue_cycle; const isNotAllowed = userAuth.isGuest || userAuth.isViewer || disabled; return (

Cycle

{issueCycle ? truncateText(issueCycle.cycle_detail.name, 15) : "No cycle"} } value={issueCycle ? issueCycle.cycle_detail.id : null} onChange={(value: any) => { !value ? removeIssueFromCycle(issueCycle?.id ?? "", issueCycle?.cycle ?? "") : handleCycleChange(incompleteCycles?.find((c) => c.id === value) as ICycle); }} width="auto" position="right" maxHeight="rg" disabled={isNotAllowed} > {incompleteCycles ? ( incompleteCycles.length > 0 ? ( <> {incompleteCycles.map((option) => ( {truncateText(option.name, 25)} ))} None ) : (
No cycles found
) ) : ( )}
); };