import React, { useCallback, useEffect, useRef, useState } from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; // react-beautiful-dnd import { DraggableProvided, DraggableStateSnapshot, DraggingStyle, NotDraggingStyle, } from "react-beautiful-dnd"; // services import issuesService from "services/issues.service"; import trackEventServices from "services/track-event.service"; // hooks import useToast from "hooks/use-toast"; import useOutsideClickDetector from "hooks/use-outside-click-detector"; // components import { ViewDueDateSelect, ViewEstimateSelect, ViewStartDateSelect } from "components/issues"; import { MembersSelect, LabelSelect, PrioritySelect } from "components/project"; import { StateSelect } from "components/states"; // ui import { ContextMenu, CustomMenu, Tooltip } from "components/ui"; // icons import { ClipboardDocumentCheckIcon, LinkIcon, PencilIcon, TrashIcon, XMarkIcon, ArrowTopRightOnSquareIcon, PaperClipIcon, EllipsisHorizontalIcon, } from "@heroicons/react/24/outline"; import { LayerDiagonalIcon } from "components/icons"; // helpers import { handleIssuesMutation } from "constants/issue"; import { copyTextToClipboard } from "helpers/string.helper"; // types import { ICurrentUserResponse, IIssue, IIssueViewProps, IState, ISubIssueResponse, TIssuePriorities, UserAuth, } from "types"; // fetch-keys import { CYCLE_DETAILS, MODULE_DETAILS, SUB_ISSUES } from "constants/fetch-keys"; type Props = { type?: string; provided: DraggableProvided; snapshot: DraggableStateSnapshot; issue: IIssue; projectId: string; groupTitle?: string; index: number; editIssue: () => void; makeIssueCopy: () => void; handleMyIssueOpen?: (issue: IIssue) => void; removeIssue?: (() => void) | null; handleDeleteIssue: (issue: IIssue) => void; handleDraftIssueEdit?: () => void; handleDraftIssueDelete?: () => void; handleTrashBox: (isDragging: boolean) => void; disableUserActions: boolean; user: ICurrentUserResponse | undefined; userAuth: UserAuth; viewProps: IIssueViewProps; }; export const SingleBoardIssue: React.FC = ({ type, provided, snapshot, issue, projectId, index, editIssue, makeIssueCopy, handleMyIssueOpen, removeIssue, groupTitle, handleDeleteIssue, handleDraftIssueEdit, handleDraftIssueDelete, handleTrashBox, disableUserActions, user, userAuth, viewProps, }) => { // context menu const [contextMenu, setContextMenu] = useState(false); const [contextMenuPosition, setContextMenuPosition] = useState(null); const [isMenuActive, setIsMenuActive] = useState(false); const [isDropdownActive, setIsDropdownActive] = useState(false); const actionSectionRef = useRef(null); const { displayFilters, properties, mutateIssues } = viewProps; const router = useRouter(); const { workspaceSlug, cycleId, moduleId } = router.query; const isDraftIssue = router.pathname.includes("draft-issues"); const { setToastAlert } = useToast(); const partialUpdateIssue = useCallback( (formData: Partial, issue: IIssue) => { if (!workspaceSlug || !issue) return; 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 { mutateIssues( (prevData: any) => handleIssuesMutation( formData, groupTitle ?? "", displayFilters?.group_by ?? null, index, displayFilters?.order_by ?? "-created_at", prevData ), false ); } issuesService .patchIssue(workspaceSlug as string, issue.project, issue.id, formData, user) .then(() => { mutateIssues(); if (cycleId) mutate(CYCLE_DETAILS(cycleId as string)); if (moduleId) mutate(MODULE_DETAILS(moduleId as string)); }); }, [displayFilters, workspaceSlug, cycleId, moduleId, groupTitle, index, mutateIssues, user] ); const getStyle = ( style: DraggingStyle | NotDraggingStyle | undefined, snapshot: DraggableStateSnapshot ) => { if (displayFilters?.order_by === "sort_order") return style; if (!snapshot.isDragging) return {}; if (!snapshot.isDropAnimating) return style; return { ...style, transitionDuration: `0.001s`, }; }; 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.", }); setIsMenuActive(false); }); }; 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 ); }; useEffect(() => { if (snapshot.isDragging) handleTrashBox(snapshot.isDragging); }, [snapshot, handleTrashBox]); useOutsideClickDetector(actionSectionRef, () => setIsMenuActive(false)); const openPeekOverview = () => { const { query } = router; if (handleMyIssueOpen) handleMyIssueOpen(issue); router.push({ pathname: router.pathname, query: { ...query, peekIssue: issue.id }, }); }; const isNotAllowed = userAuth.isGuest || userAuth.isViewer || disableUserActions; return ( <> {!isNotAllowed && ( <> { if (isDraftIssue && handleDraftIssueEdit) handleDraftIssueEdit(); else editIssue(); }} > Edit issue {!isDraftIssue && ( Make a copy... )} { if (isDraftIssue && handleDraftIssueDelete) handleDraftIssueDelete(); else handleDeleteIssue(issue); }} > Delete issue )} {!isDraftIssue && ( Copy issue link )} {!isDraftIssue && ( Open issue in new tab )}
{ e.preventDefault(); setContextMenu(true); setContextMenuPosition(e); }} >
{!isNotAllowed && ( )}
{properties.key && (
{issue.project_detail.identifier}-{issue.sequence_id}
)}
{properties.priority && ( )} {properties.state && ( )} {properties.start_date && issue.start_date && ( setIsDropdownActive(true)} handleOnClose={() => setIsDropdownActive(false)} user={user} isNotAllowed={isNotAllowed} /> )} {properties.due_date && issue.target_date && ( setIsDropdownActive(true)} handleOnClose={() => setIsDropdownActive(false)} user={user} isNotAllowed={isNotAllowed} /> )} {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}
)}
); };