import React, { useState } from "react"; import { useRouter } from "next/router"; import { observer } from "mobx-react-lite"; import { mutate } from "swr"; // mobx store import { useMobxStore } from "lib/mobx/store-provider"; // ui import { CustomSearchSelect, DiceIcon, Spinner, Tooltip } from "@plane/ui"; // types import { IIssue } from "types"; // fetch-keys import { ISSUE_DETAILS, MODULE_ISSUES } from "constants/fetch-keys"; type Props = { issueDetail: IIssue | undefined; handleModuleChange?: (moduleId: string) => void; disabled?: boolean; handleIssueUpdate?: () => void; }; export const SidebarModuleSelect: React.FC = observer((props) => { const { issueDetail, disabled = false, handleIssueUpdate, handleModuleChange } = props; // router const router = useRouter(); const { workspaceSlug, projectId } = router.query; // mobx store const { module: { projectModules }, moduleIssues: { removeIssueFromModule, addIssueToModule }, } = useMobxStore(); const [isUpdating, setIsUpdating] = useState(false); const handleModuleStoreChange = async (moduleId: string) => { if (!workspaceSlug || !issueDetail || !moduleId) return; setIsUpdating(true); await addIssueToModule(workspaceSlug.toString(), moduleId, [issueDetail.id], false, projectId?.toString()) .then(async () => { handleIssueUpdate && (await handleIssueUpdate()); }) .finally(() => { setIsUpdating(false); }); }; const handleRemoveIssueFromModule = (bridgeId: string, moduleId: string) => { if (!workspaceSlug || !projectId || !issueDetail) return; setIsUpdating(true); removeIssueFromModule(workspaceSlug.toString(), projectId.toString(), moduleId, issueDetail.id, bridgeId) .then(async () => { handleIssueUpdate && (await handleIssueUpdate()); mutate(ISSUE_DETAILS(issueDetail.id)); mutate(MODULE_ISSUES(moduleId)); }) .catch((e) => { console.log(e); }) .finally(() => { setIsUpdating(false); }); }; const options = projectModules?.map((module) => ({ value: module.id, query: module.name, content: (
{module.name}
), })); const issueModule = issueDetail?.issue_module; const disableSelect = disabled || isUpdating; return (
{ value === issueModule?.module_detail.id ? handleRemoveIssueFromModule(issueModule?.id ?? "", issueModule?.module ?? "") : handleModuleChange ? handleModuleChange(value) : handleModuleStoreChange(value); }} options={options} customButton={
m.id === issueModule?.module)?.name ?? "No module"}`} >
} width="max-w-[10rem]" noChevron disabled={disableSelect} /> {isUpdating && }
); });