import React, { useState } from "react"; import { useRouter } from "next/router"; import useSWR, { mutate } from "swr"; // react-hook-form import { useForm, Controller, UseFormWatch, Control } from "react-hook-form"; // react-color import { TwitterPicker } from "react-color"; // headless ui import { Popover, Listbox, Transition } from "@headlessui/react"; // hooks import useToast from "hooks/use-toast"; // services import issuesServices from "services/issues.service"; // components import { SidebarAssigneeSelect, SidebarBlockedSelect, SidebarBlockerSelect, SidebarCycleSelect, SidebarParentSelect, SidebarPrioritySelect, SidebarStateSelect, } from "components/issues/sidebar-select"; import { DeleteIssueModal } from "components/issues"; // ui import { Input, Button, Spinner, CustomDatePicker } from "components/ui"; // icons import { TagIcon, ChevronDownIcon, LinkIcon, CalendarDaysIcon, TrashIcon, PlusIcon, XMarkIcon, } from "@heroicons/react/24/outline"; // helpers import { copyTextToClipboard } from "helpers/string.helper"; // types import type { ICycle, IIssue, IIssueLabels, UserAuth } from "types"; // fetch-keys import { PROJECT_ISSUE_LABELS, PROJECT_ISSUES_LIST, ISSUE_DETAILS } from "constants/fetch-keys"; type Props = { control: Control; submitChanges: (formData: Partial) => void; issueDetail: IIssue | undefined; watch: UseFormWatch; userAuth: UserAuth; }; const defaultValues: Partial = { name: "", colour: "#ff0000", }; export const IssueDetailsSidebar: React.FC = ({ control, submitChanges, issueDetail, watch: watchIssue, userAuth, }) => { const [createLabelForm, setCreateLabelForm] = useState(false); const [deleteIssueModal, setDeleteIssueModal] = useState(false); const router = useRouter(); const { workspaceSlug, projectId, issueId } = router.query; const { setToastAlert } = useToast(); const { data: issues } = useSWR( workspaceSlug && projectId ? PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string) : null, workspaceSlug && projectId ? () => issuesServices.getIssues(workspaceSlug as string, projectId as string) : null ); const { data: issueLabels, mutate: issueLabelMutate } = useSWR( workspaceSlug && projectId ? PROJECT_ISSUE_LABELS(projectId as string) : null, workspaceSlug && projectId ? () => issuesServices.getIssueLabels(workspaceSlug as string, projectId as string) : null ); const { register, handleSubmit, formState: { isSubmitting }, reset, watch, control: controlLabel, } = useForm({ defaultValues, }); const handleNewLabel = (formData: any) => { if (!workspaceSlug || !projectId || isSubmitting) return; issuesServices .createIssueLabel(workspaceSlug as string, projectId as string, formData) .then((res) => { reset(defaultValues); issueLabelMutate((prevData) => [...(prevData ?? []), res], false); submitChanges({ labels_list: [...(issueDetail?.labels ?? []), res.id] }); setCreateLabelForm(false); }); }; const handleCycleChange = (cycleDetail: ICycle) => { if (!workspaceSlug || !projectId || !issueDetail) return; issuesServices .addIssueToCycle(workspaceSlug as string, projectId as string, cycleDetail.id, { issues: [issueDetail.id], }) .then((res) => { mutate(ISSUE_DETAILS(issueId as string)); }); }; const isNotAllowed = userAuth.isGuest || userAuth.isViewer; return ( <> setDeleteIssueModal(false)} isOpen={deleteIssueModal} data={issueDetail} />

{issueDetail?.project_detail?.identifier}-{issueDetail?.sequence_id}

{!isNotAllowed && ( )}
i.id !== issueDetail?.id && i.id !== issueDetail?.parent && i.parent !== issueDetail?.id ) ?? [] } customDisplay={ issueDetail?.parent_detail ? ( ) : (
No parent selected
) } watch={watchIssue} userAuth={userAuth} /> i.id !== issueDetail?.id) ?? []} watch={watchIssue} userAuth={userAuth} /> i.id !== issueDetail?.id) ?? []} watch={watchIssue} userAuth={userAuth} />

Due date

( submitChanges({ target_date: val, }) } disabled={isNotAllowed} /> )} />

Label

{watchIssue("labels_list")?.map((label) => { const singleLabel = issueLabels?.find((l) => l.id === label); if (!singleLabel) return null; return ( { const updatedLabels = watchIssue("labels_list")?.filter((l) => l !== label); submitChanges({ labels_list: updatedLabels, }); }} > {singleLabel.name} ); })} ( submitChanges({ labels_list: val })} className="flex-shrink-0" multiple disabled={isNotAllowed} > {({ open }) => ( <> Label
Select Label
{issueLabels ? ( issueLabels.length > 0 ? ( issueLabels.map((label: IIssueLabels) => ( `${ active || selected ? "bg-indigo-50" : "" } relative flex cursor-pointer select-none items-center gap-2 truncate p-2 text-gray-900` } value={label.id} > {label.name} )) ) : (
No labels found
) ) : ( )}
)}
)} />
{createLabelForm && (
{({ open }) => ( <> {watch("colour") && watch("colour") !== "" && ( )} ( onChange(value.hex)} /> )} /> )}
)}
); };