import React, { useState } from "react"; import { useRouter } from "next/router"; import useSWR, { mutate } from "swr"; // hooks import { useCycle, useIssues } from "hooks/store"; // services import { CycleService } from "services/cycle.service"; // ui import { ContrastIcon, CustomSearchSelect, Spinner, Tooltip } from "@plane/ui"; // types import { TIssue } from "@plane/types"; // fetch-keys import { CYCLE_ISSUES, INCOMPLETE_CYCLES_LIST, ISSUE_DETAILS } from "constants/fetch-keys"; import { EIssuesStoreType } from "constants/issue"; type Props = { issueDetail: TIssue | undefined; handleCycleChange?: (cycleId: string) => void; disabled?: boolean; handleIssueUpdate?: () => void; }; // services const cycleService = new CycleService(); export const SidebarCycleSelect: React.FC = (props) => { const { issueDetail, disabled = false, handleIssueUpdate, handleCycleChange } = props; // router const router = useRouter(); const { workspaceSlug, projectId } = router.query; // mobx store const { issues: { removeIssueFromCycle, addIssueToCycle }, } = useIssues(EIssuesStoreType.CYCLE); const { getCycleById } = useCycle(); const [isUpdating, setIsUpdating] = useState(false); const { data: incompleteCycles } = useSWR( workspaceSlug && projectId ? INCOMPLETE_CYCLES_LIST(projectId as string) : null, workspaceSlug && projectId ? () => cycleService.getCyclesWithParams(workspaceSlug as string, projectId as string) // FIXME, "incomplete") : null ); const handleCycleStoreChange = async (cycleId: string) => { if (!workspaceSlug || !issueDetail || !cycleId || !projectId) return; setIsUpdating(true); await addIssueToCycle(workspaceSlug.toString(), projectId?.toString(), cycleId, [issueDetail.id]) .then(async () => { handleIssueUpdate && (await handleIssueUpdate()); }) .finally(() => { setIsUpdating(false); }); }; const handleRemoveIssueFromCycle = (cycleId: string) => { if (!workspaceSlug || !projectId || !issueDetail) return; setIsUpdating(true); removeIssueFromCycle(workspaceSlug.toString(), projectId.toString(), cycleId, issueDetail.id) .then(async () => { handleIssueUpdate && (await handleIssueUpdate()); mutate(ISSUE_DETAILS(issueDetail.id)); mutate(CYCLE_ISSUES(cycleId)); }) .catch((e) => { console.error(e); }) .finally(() => { setIsUpdating(false); }); }; const options = incompleteCycles?.map((cycle) => ({ value: cycle.id, query: cycle.name, content: (
{cycle.name}
), })); const issueCycle = (issueDetail && issueDetail.cycle_id && getCycleById(issueDetail.cycle_id)) || undefined; const disableSelect = disabled || isUpdating; return (
{ value === issueDetail?.cycle_id ? handleRemoveIssueFromCycle(issueDetail?.cycle_id ?? "") : handleCycleChange ? handleCycleChange(value) : handleCycleStoreChange(value); }} options={options} customButton={
} width="max-w-[10rem]" noChevron disabled={disableSelect} /> {isUpdating && }
); };