import React from "react"; // swr import useSWR from "swr"; // headless ui import { Listbox, Transition } from "@headlessui/react"; // react hook form import { useForm, Controller } from "react-hook-form"; // services import stateServices from "lib/services/state.services"; import issuesServices from "lib/services/issues.services"; import workspaceService from "lib/services/workspace.service"; // hooks import useUser from "lib/hooks/useUser"; // fetching keys import { PROJECT_ISSUES_LIST, STATE_LIST, WORKSPACE_MEMBERS, PROJECT_ISSUE_LABELS, } from "constants/fetch-keys"; // commons import { classNames, copyTextToClipboard } from "constants/common"; // ui import { Input, Button } from "ui"; // icons import { UserIcon, TagIcon, UserGroupIcon, ChevronDownIcon, Squares2X2Icon, ChartBarIcon, ClipboardDocumentIcon, LinkIcon, } from "@heroicons/react/24/outline"; // types import type { Control } from "react-hook-form"; import type { IIssue, IIssueLabels, IssueResponse, IState, WorkspaceMember } from "types"; type Props = { control: Control; submitChanges: (formData: Partial) => void; issueDetail: IIssue | undefined; }; const PRIORITIES = ["high", "medium", "low"]; const defaultValues: Partial = { name: "", }; const IssueDetailSidebar: React.FC = ({ control, submitChanges, issueDetail }) => { const { activeWorkspace, activeProject } = useUser(); const { data: states } = useSWR( activeWorkspace && activeProject ? STATE_LIST(activeProject.id) : null, activeWorkspace && activeProject ? () => stateServices.getStates(activeWorkspace.slug, activeProject.id) : null ); const { data: people } = useSWR( activeWorkspace ? WORKSPACE_MEMBERS : null, activeWorkspace ? () => workspaceService.workspaceMembers(activeWorkspace.slug) : null ); const { data: projectIssues } = useSWR( activeProject && activeWorkspace ? PROJECT_ISSUES_LIST(activeWorkspace.slug, activeProject.id) : null, activeProject && activeWorkspace ? () => issuesServices.getIssues(activeWorkspace.slug, activeProject.id) : null ); const { data: issueLabels, mutate: issueLabelMutate } = useSWR( activeProject && activeWorkspace ? PROJECT_ISSUE_LABELS(activeProject.id) : null, activeProject && activeWorkspace ? () => issuesServices.getIssueLabels(activeWorkspace.slug, activeProject.id) : null ); const { register, handleSubmit, formState: { isSubmitting }, reset, } = useForm({ defaultValues, }); const onSubmit = (formData: any) => { if (!activeWorkspace || !activeProject || isSubmitting) return; issuesServices .createIssueLabel(activeWorkspace.slug, activeProject.id, formData) .then((res) => { console.log(res); reset(defaultValues); issueLabelMutate((prevData) => [...(prevData ?? []), res], false); }); }; const sidebarOptions = [ { label: "Priority", name: "priority", canSelectMultipleOptions: false, icon: ChartBarIcon, options: PRIORITIES.map((property) => ({ label: property, value: property, })), }, { label: "Status", name: "state", canSelectMultipleOptions: false, icon: Squares2X2Icon, options: states?.map((state) => ({ label: state.name, value: state.id, })), }, { label: "Assignees", name: "assignees_list", canSelectMultipleOptions: true, icon: UserGroupIcon, options: people?.map((person) => ({ label: person.member.first_name, value: person.member.id, })), }, { label: "Blocker", name: "blockers_list", canSelectMultipleOptions: true, icon: UserIcon, options: projectIssues?.results?.map((issue) => ({ label: issue.name, value: issue.id, })), }, { label: "Blocked", name: "blocked_list", canSelectMultipleOptions: true, icon: UserIcon, options: projectIssues?.results?.map((issue) => ({ label: issue.name, value: issue.id, })), }, ]; return (

Quick Actions

{sidebarOptions.map((item) => (

{item.label}

( submitChanges({ [item.name]: value })} className="flex-shrink-0" > {({ open }) => (
{value ? Array.isArray(value) ? value .map( (i: any) => item.options?.find((option) => option.value === i)?.label ) .join(", ") || item.label : item.options?.find((option) => option.value === value)?.label : "None"}
{item.options?.map((option) => ( `${ active || selected ? "text-white bg-theme" : "text-gray-900" } ${ item.label === "Priority" && "capitalize" } cursor-pointer select-none relative p-2 rounded-md truncate` } value={option.value} > {option.label} ))}
)}
)} />
))}

Label

( submitChanges({ labels_list: value })} className="flex-shrink-0" > {({ open }) => ( <> Label
{value && value.length > 0 ? value .map( (i: string) => issueLabels?.find((option) => option.id === i)?.name ) .join(", ") : "None"}
{issueLabels?.map((label: any) => ( `${ active || selected ? "text-white bg-theme" : "text-gray-900" } cursor-pointer select-none relative p-2 rounded-md truncate` } value={label.id} > {label.name} ))}
)}
)} />
); }; export default IssueDetailSidebar;