import { FC, useCallback, useEffect, useRef, useState } from "react"; import { combine } from "@atlaskit/pragmatic-drag-and-drop/combine"; import { dropTargetForElements } from "@atlaskit/pragmatic-drag-and-drop/element/adapter"; import { autoScrollForElements } from "@atlaskit/pragmatic-drag-and-drop-auto-scroll/element"; import { observer } from "mobx-react-lite"; import { useRouter } from "next/router"; import { TIssue } from "@plane/types"; import { Spinner, TOAST_TYPE, setToast } from "@plane/ui"; import { DeleteIssueModal } from "@/components/issues"; import { ISSUE_DELETED } from "@/constants/event-tracker"; import { EIssueFilterType, EIssuesStoreType } from "@/constants/issue"; import { EUserProjectRoles } from "@/constants/project"; // hooks import { useEventTracker, useIssueDetail, useIssues, useKanbanView, useUser } from "@/hooks/store"; import { useIssuesActions } from "@/hooks/use-issues-actions"; // store import { ISSUE_FILTER_DEFAULT_DATA } from "@/store/issue/helpers/issue-helper.store"; // ui // types import { IQuickActionProps, TRenderQuickActions } from "../list/list-view-types"; //components import { GroupDropLocation, handleGroupDragDrop, getSourceFromDropPayload } from "../utils"; import { KanBan } from "./default"; import { KanBanSwimLanes } from "./swimlanes"; export type KanbanStoreType = | EIssuesStoreType.PROJECT | EIssuesStoreType.MODULE | EIssuesStoreType.CYCLE | EIssuesStoreType.PROJECT_VIEW | EIssuesStoreType.DRAFT | EIssuesStoreType.PROFILE; export interface IBaseKanBanLayout { QuickActions: FC; showLoader?: boolean; viewId?: string; storeType: KanbanStoreType; addIssuesToView?: (issueIds: string[]) => Promise; canEditPropertiesBasedOnProject?: (projectId: string) => boolean; isCompletedCycle?: boolean; } export const BaseKanBanRoot: React.FC = observer((props: IBaseKanBanLayout) => { const { QuickActions, showLoader, viewId, storeType, addIssuesToView, canEditPropertiesBasedOnProject, isCompletedCycle = false, } = props; // router const router = useRouter(); const { workspaceSlug, projectId } = router.query; // store hooks const { membership: { currentProjectRole }, } = useUser(); const { captureIssueEvent } = useEventTracker(); const { issueMap, issuesFilter, issues } = useIssues(storeType); const { issue: { getIssueById }, } = useIssueDetail(); const { updateIssue, removeIssue, removeIssueFromView, archiveIssue, restoreIssue, updateFilters } = useIssuesActions(storeType); const { issues: { addCycleToIssue, removeCycleFromIssue }, } = useIssues(EIssuesStoreType.CYCLE); const { issues: { changeModulesInIssue }, } = useIssues(EIssuesStoreType.MODULE); const deleteAreaRef = useRef(null); const [isDragOverDelete, setIsDragOverDelete] = useState(false); const { isDragging } = useKanbanView(); const issueIds = issues?.groupedIssueIds || []; const displayFilters = issuesFilter?.issueFilters?.displayFilters; const displayProperties = issuesFilter?.issueFilters?.displayProperties; const sub_group_by = displayFilters?.sub_group_by; const group_by = displayFilters?.group_by; const orderBy = displayFilters?.order_by; const userDisplayFilters = displayFilters || null; const KanBanView = sub_group_by ? KanBanSwimLanes : KanBan; const { enableInlineEditing, enableQuickAdd, enableIssueCreation } = issues?.viewFlags || {}; const scrollableContainerRef = useRef(null); // states const [draggedIssueId, setDraggedIssueId] = useState(undefined); const [deleteIssueModal, setDeleteIssueModal] = useState(false); const isEditingAllowed = !!currentProjectRole && currentProjectRole >= EUserProjectRoles.MEMBER; const canEditProperties = useCallback( (projectId: string | undefined) => { const isEditingAllowedBasedOnProject = canEditPropertiesBasedOnProject && projectId ? canEditPropertiesBasedOnProject(projectId) : isEditingAllowed; return enableInlineEditing && isEditingAllowedBasedOnProject; }, [canEditPropertiesBasedOnProject, enableInlineEditing, isEditingAllowed] ); // Enable Auto Scroll for Main Kanban useEffect(() => { const element = scrollableContainerRef.current; if (!element) return; return combine( autoScrollForElements({ element, }) ); }, [scrollableContainerRef?.current]); // Make the Issue Delete Box a Drop Target useEffect(() => { const element = deleteAreaRef.current; if (!element) return; return combine( dropTargetForElements({ element, getData: () => ({ columnId: "issue-trash-box", groupId: "issue-trash-box", type: "DELETE" }), onDragEnter: () => { setIsDragOverDelete(true); }, onDragLeave: () => { setIsDragOverDelete(false); }, onDrop: (payload) => { setIsDragOverDelete(false); const source = getSourceFromDropPayload(payload); if (!source) return; setDraggedIssueId(source.id); setDeleteIssueModal(true); }, }) ); }, [deleteAreaRef?.current, setIsDragOverDelete, setDraggedIssueId, setDeleteIssueModal]); /** * update Issue on Drop, checks if modules or cycles are changed and then calls appropriate functions * @param projectId * @param issueId * @param data * @param issueUpdates */ const updateIssueOnDrop = async ( projectId: string, issueId: string, data: Partial, issueUpdates: { [groupKey: string]: { ADD: string[]; REMOVE: string[]; }; } ) => { const errorToastProps = { type: TOAST_TYPE.ERROR, title: "Error!", message: "Error while updating issue" } const moduleKey = ISSUE_FILTER_DEFAULT_DATA["module"]; const cycleKey = ISSUE_FILTER_DEFAULT_DATA["cycle"]; const isModuleChanged = Object.keys(data).includes(moduleKey); const isCycleChanged = Object.keys(data).includes(cycleKey); if (isCycleChanged && workspaceSlug) { if(data[cycleKey]) { addCycleToIssue(workspaceSlug.toString(), projectId, data[cycleKey], issueId).catch(() => setToast(errorToastProps)); } else { removeCycleFromIssue(workspaceSlug.toString(), projectId, issueId).catch(() => setToast(errorToastProps)) } delete data[cycleKey]; } if (isModuleChanged && workspaceSlug && issueUpdates[moduleKey]) { changeModulesInIssue( workspaceSlug.toString(), projectId, issueId, issueUpdates[moduleKey].ADD, issueUpdates[moduleKey].REMOVE ).catch(() => setToast(errorToastProps)); delete data[moduleKey]; } updateIssue && updateIssue(projectId, issueId, data).catch(() => setToast(errorToastProps)); }; const handleOnDrop = async (source: GroupDropLocation, destination: GroupDropLocation) => { if ( source.columnId && destination.columnId && destination.columnId === source.columnId && destination.id === source.id ) return; await handleGroupDragDrop( source, destination, getIssueById, issues.getIssueIds, updateIssueOnDrop, group_by, sub_group_by, orderBy !== "sort_order" ).catch((err) => { setToast({ title: "Error!", type: TOAST_TYPE.ERROR, message: err?.detail ?? "Failed to perform this action", }); }); }; const renderQuickActions: TRenderQuickActions = useCallback( ({ issue, parentRef, customActionButton }) => ( removeIssue(issue.project_id, issue.id)} handleUpdate={async (data) => updateIssue && updateIssue(issue.project_id, issue.id, data)} handleRemoveFromView={async () => removeIssueFromView && removeIssueFromView(issue.project_id, issue.id)} handleArchive={async () => archiveIssue && archiveIssue(issue.project_id, issue.id)} handleRestore={async () => restoreIssue && restoreIssue(issue.project_id, issue.id)} readOnly={!isEditingAllowed || isCompletedCycle} /> ), // eslint-disable-next-line react-hooks/exhaustive-deps [isEditingAllowed, isCompletedCycle, removeIssue, updateIssue, removeIssueFromView, archiveIssue, restoreIssue] ); const handleDeleteIssue = async () => { const draggedIssue = getIssueById(draggedIssueId ?? ""); if (!draggedIssueId || !draggedIssue) return; await removeIssue(draggedIssue.project_id, draggedIssueId).finally(() => { setDeleteIssueModal(false); setDraggedIssueId(undefined); captureIssueEvent({ eventName: ISSUE_DELETED, payload: { id: draggedIssueId, state: "FAILED", element: "Kanban layout drag & drop" }, path: router.asPath, }); }); }; const handleKanbanFilters = (toggle: "group_by" | "sub_group_by", value: string) => { if (workspaceSlug && projectId) { let kanbanFilters = issuesFilter?.issueFilters?.kanbanFilters?.[toggle] || []; if (kanbanFilters.includes(value)) { kanbanFilters = kanbanFilters.filter((_value) => _value != value); } else { kanbanFilters.push(value); } updateFilters(projectId.toString(), EIssueFilterType.KANBAN_FILTERS, { [toggle]: kanbanFilters, }); } }; const kanbanFilters = issuesFilter?.issueFilters?.kanbanFilters || { group_by: [], sub_group_by: [] }; return ( <> setDeleteIssueModal(false)} onSubmit={handleDeleteIssue} /> {showLoader && issues?.loader === "init-loader" && (
)}
{/* drag and delete component */}
Drop here to delete the issue.
); });