import React, { useCallback } from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; // react-beautiful-dnd import { DraggableProvided, DraggableStateSnapshot } from "react-beautiful-dnd"; // services import issuesService from "services/issues.service"; import trackEventServices from "services/track-event.service"; // hooks import useCalendarIssuesView from "hooks/use-calendar-issues-view"; import useIssuesProperties from "hooks/use-issue-properties"; import useToast from "hooks/use-toast"; // components import { CustomMenu, Tooltip } from "components/ui"; import { ViewDueDateSelect, ViewEstimateSelect, ViewStartDateSelect } from "components/issues"; import { LabelSelect, MembersSelect, PrioritySelect } from "components/project"; import { StateSelect } from "components/states"; // icons import { LinkIcon, PaperClipIcon, PencilIcon, TrashIcon } from "@heroicons/react/24/outline"; import { LayerDiagonalIcon } from "components/icons"; // helper import { copyTextToClipboard, truncateText } from "helpers/string.helper"; // type import { ICurrentUserResponse, IIssue, IState, ISubIssueResponse, TIssuePriorities } from "types"; // fetch-keys import { CYCLE_ISSUES_WITH_PARAMS, MODULE_ISSUES_WITH_PARAMS, PROJECT_ISSUES_LIST_WITH_PARAMS, SUB_ISSUES, VIEW_ISSUES, } from "constants/fetch-keys"; type Props = { handleEditIssue: (issue: IIssue) => void; handleDeleteIssue: (issue: IIssue) => void; index: number; provided: DraggableProvided; snapshot: DraggableStateSnapshot; issue: IIssue; projectId: string; user: ICurrentUserResponse | undefined; isNotAllowed: boolean; }; export const SingleCalendarIssue: React.FC = ({ handleEditIssue, handleDeleteIssue, index, provided, snapshot, issue, projectId, user, isNotAllowed, }) => { const router = useRouter(); const { workspaceSlug, cycleId, moduleId, viewId } = router.query; const { setToastAlert } = useToast(); const { params } = useCalendarIssuesView(); const [properties] = useIssuesProperties(workspaceSlug as string, projectId as string); const partialUpdateIssue = useCallback( (formData: Partial, issue: IIssue) => { if (!workspaceSlug || !projectId) return; const fetchKey = cycleId ? CYCLE_ISSUES_WITH_PARAMS(cycleId.toString(), params) : moduleId ? MODULE_ISSUES_WITH_PARAMS(moduleId.toString(), params) : viewId ? VIEW_ISSUES(viewId.toString(), params) : PROJECT_ISSUES_LIST_WITH_PARAMS(projectId.toString(), params); if (issue.parent) { mutate( SUB_ISSUES(issue.parent.toString()), (prevData) => { if (!prevData) return prevData; return { ...prevData, sub_issues: (prevData.sub_issues ?? []).map((i) => { if (i.id === issue.id) { return { ...i, ...formData, }; } return i; }), }; }, false ); } else { mutate( fetchKey, (prevData) => (prevData ?? []).map((p) => { if (p.id === issue.id) { return { ...p, ...formData, assignees: formData?.assignees_list ?? p.assignees, }; } return p; }), false ); } issuesService .patchIssue( workspaceSlug as string, projectId as string, issue.id as string, formData, user ) .then(() => { mutate(fetchKey); }) .catch((error) => { console.log(error); }); }, [workspaceSlug, projectId, cycleId, moduleId, viewId, params, user] ); const handleCopyText = () => { const originURL = typeof window !== "undefined" && window.location.origin ? window.location.origin : ""; copyTextToClipboard( `${originURL}/${workspaceSlug}/projects/${projectId}/issues/${issue.id}` ).then(() => { setToastAlert({ type: "success", title: "Link Copied!", message: "Issue link copied to clipboard.", }); }); }; const handleStateChange = (data: string, states: IState[] | undefined) => { const oldState = states?.find((s) => s.id === issue.state); const newState = states?.find((s) => s.id === data); partialUpdateIssue( { state: data, state_detail: newState, }, issue ); trackEventServices.trackIssuePartialPropertyUpdateEvent( { workspaceSlug, workspaceId: issue.workspace, projectId: issue.project_detail.id, projectIdentifier: issue.project_detail.identifier, projectName: issue.project_detail.name, issueId: issue.id, }, "ISSUE_PROPERTY_UPDATE_STATE", user ); if (oldState?.group !== "completed" && newState?.group !== "completed") { trackEventServices.trackIssueMarkedAsDoneEvent( { workspaceSlug: issue.workspace_detail.slug, workspaceId: issue.workspace_detail.id, projectId: issue.project_detail.id, projectIdentifier: issue.project_detail.identifier, projectName: issue.project_detail.name, issueId: issue.id, }, user ); } }; const handleAssigneeChange = (data: any) => { const newData = issue.assignees ?? []; if (newData.includes(data)) newData.splice(newData.indexOf(data), 1); else newData.push(data); partialUpdateIssue({ assignees_list: data }, issue); trackEventServices.trackIssuePartialPropertyUpdateEvent( { workspaceSlug, workspaceId: issue.workspace, projectId: issue.project_detail.id, projectIdentifier: issue.project_detail.identifier, projectName: issue.project_detail.name, issueId: issue.id, }, "ISSUE_PROPERTY_UPDATE_ASSIGNEE", user ); }; const handleLabelChange = (data: any) => { partialUpdateIssue({ labels_list: data }, issue); }; const handlePriorityChange = (data: TIssuePriorities) => { partialUpdateIssue({ priority: data }, issue); trackEventServices.trackIssuePartialPropertyUpdateEvent( { workspaceSlug, workspaceId: issue.workspace, projectId: issue.project_detail.id, projectIdentifier: issue.project_detail.identifier, projectName: issue.project_detail.name, issueId: issue.id, }, "ISSUE_PROPERTY_UPDATE_PRIORITY", user ); }; const displayProperties = properties ? Object.values(properties).some((value) => value === true) : false; const openPeekOverview = () => { const { query } = router; router.push({ pathname: router.pathname, query: { ...query, peekIssue: issue.id }, }); }; return (
{!isNotAllowed && (
handleEditIssue(issue)}>
Edit issue
handleDeleteIssue(issue)}>
Delete issue
Copy issue Link
)} {displayProperties && (
{properties.priority && ( )} {properties.state && ( )} {properties.start_date && issue.start_date && ( )} {properties.due_date && issue.target_date && ( )} {properties.labels && issue.labels.length > 0 && ( )} {properties.assignee && ( )} {properties.estimate && issue.estimate_point !== null && ( )} {properties.sub_issue_count && issue.sub_issues_count > 0 && (
{issue.sub_issues_count}
)} {properties.link && issue.link_count > 0 && (
{issue.link_count}
)} {properties.attachment_count && issue.attachment_count > 0 && (
{issue.attachment_count}
)}
)}
); };