import { useCallback, useState } from "react"; import { useRouter } from "next/router"; import useSWR, { mutate } from "swr"; // react-beautiful-dnd import { DragDropContext, DropResult } from "react-beautiful-dnd"; // services import issuesService from "services/issues.service"; import stateService from "services/state.service"; import projectService from "services/project.service"; import modulesService from "services/modules.service"; // hooks import useToast from "hooks/use-toast"; import useIssuesView from "hooks/use-issues-view"; // components import { AllLists, AllBoards } from "components/core"; import { CreateUpdateIssueModal, DeleteIssueModal } from "components/issues"; import StrictModeDroppable from "components/dnd/StrictModeDroppable"; import { CreateUpdateViewModal } from "components/views"; // ui import { Avatar, EmptySpace, EmptySpaceItem, PrimaryButton, Spinner } from "components/ui"; import { CalendarView } from "./calendar-view"; // icons import { ListBulletIcon, PlusIcon, RectangleStackIcon, TrashIcon, XMarkIcon, } from "@heroicons/react/24/outline"; import { ExclamationIcon, getStateGroupIcon } from "components/icons"; // helpers import { getStatesList } from "helpers/state.helper"; import { replaceUnderscoreIfSnakeCase } from "helpers/string.helper"; // types import { CycleIssueResponse, IIssue, IIssueFilterOptions, ModuleIssueResponse, UserAuth, } from "types"; // fetch-keys import { CYCLE_ISSUES, CYCLE_ISSUES_WITH_PARAMS, MODULE_ISSUES, MODULE_ISSUES_WITH_PARAMS, PROJECT_ISSUES_LIST_WITH_PARAMS, PROJECT_ISSUE_LABELS, PROJECT_MEMBERS, STATE_LIST, } from "constants/fetch-keys"; import { getPriorityIcon } from "components/icons/priority-icon"; type Props = { type?: "issue" | "cycle" | "module"; openIssuesListModal?: () => void; isCompleted?: boolean; userAuth: UserAuth; }; export const IssuesView: React.FC = ({ type = "issue", openIssuesListModal, isCompleted = false, userAuth, }) => { // create issue modal const [createIssueModal, setCreateIssueModal] = useState(false); const [createViewModal, setCreateViewModal] = useState(null); const [preloadedData, setPreloadedData] = useState< (Partial & { actionType: "createIssue" | "edit" | "delete" }) | undefined >(undefined); // update issue modal const [editIssueModal, setEditIssueModal] = useState(false); const [issueToEdit, setIssueToEdit] = useState< (IIssue & { actionType: "edit" | "delete" }) | undefined >(undefined); // delete issue modal const [deleteIssueModal, setDeleteIssueModal] = useState(false); const [issueToDelete, setIssueToDelete] = useState(null); // trash box const [trashBox, setTrashBox] = useState(false); const router = useRouter(); const { workspaceSlug, projectId, cycleId, moduleId, viewId } = router.query; const { setToastAlert } = useToast(); const { groupedByIssues, issueView, groupByProperty: selectedGroup, orderBy, filters, isNotEmpty, setFilters, params, } = useIssuesView(); const { data: stateGroups } = useSWR( workspaceSlug && projectId ? STATE_LIST(projectId as string) : null, workspaceSlug ? () => stateService.getStates(workspaceSlug as string, projectId as string) : null ); const states = getStatesList(stateGroups ?? {}); const { data: members } = useSWR( projectId ? PROJECT_MEMBERS(projectId as string) : null, workspaceSlug && projectId ? () => projectService.projectMembers(workspaceSlug as string, projectId as string) : null ); const { data: issueLabels } = useSWR( projectId ? PROJECT_ISSUE_LABELS(projectId.toString()) : null, workspaceSlug && projectId ? () => issuesService.getIssueLabels(workspaceSlug as string, projectId.toString()) : null ); const handleDeleteIssue = useCallback( (issue: IIssue) => { setDeleteIssueModal(true); setIssueToDelete(issue); }, [setDeleteIssueModal, setIssueToDelete] ); const handleOnDragEnd = useCallback( (result: DropResult) => { setTrashBox(false); if (!result.destination || !workspaceSlug || !projectId || !groupedByIssues) return; const { source, destination } = result; const draggedItem = groupedByIssues[source.droppableId][source.index]; if (destination.droppableId === "trashBox") { handleDeleteIssue(draggedItem); } else { if (orderBy === "sort_order") { let newSortOrder = draggedItem.sort_order; const destinationGroupArray = groupedByIssues[destination.droppableId]; if (destinationGroupArray.length !== 0) { // check if dropping in the same group if (source.droppableId === destination.droppableId) { // check if dropping at beginning if (destination.index === 0) newSortOrder = destinationGroupArray[0].sort_order - 10000; // check if dropping at last else if (destination.index === destinationGroupArray.length - 1) newSortOrder = destinationGroupArray[destinationGroupArray.length - 1].sort_order + 10000; else { if (destination.index > source.index) newSortOrder = (destinationGroupArray[source.index + 1].sort_order + destinationGroupArray[source.index + 2].sort_order) / 2; else if (destination.index < source.index) newSortOrder = (destinationGroupArray[source.index - 1].sort_order + destinationGroupArray[source.index - 2].sort_order) / 2; } } else { // check if dropping at beginning if (destination.index === 0) newSortOrder = destinationGroupArray[0].sort_order - 10000; // check if dropping at last else if (destination.index === destinationGroupArray.length) newSortOrder = destinationGroupArray[destinationGroupArray.length - 1].sort_order + 10000; else newSortOrder = (destinationGroupArray[destination.index - 1].sort_order + destinationGroupArray[destination.index].sort_order) / 2; } } draggedItem.sort_order = newSortOrder; } const destinationGroup = destination.droppableId; // destination group id if (orderBy === "sort_order" || source.droppableId !== destination.droppableId) { // different group/column; // source.droppableId !== destination.droppableId -> even if order by is not sort_order, // if the issue is moved to a different group, then we will change the group of the // dragged item(or issue) if (selectedGroup === "priority") draggedItem.priority = destinationGroup; else if (selectedGroup === "state") draggedItem.state = destinationGroup; } const sourceGroup = source.droppableId; // source group id // TODO: move this mutation logic to a separate function if (cycleId) mutate<{ [key: string]: IIssue[]; }>( CYCLE_ISSUES_WITH_PARAMS(cycleId as string, params), (prevData) => { if (!prevData) return prevData; const sourceGroupArray = prevData[sourceGroup]; const destinationGroupArray = groupedByIssues[destinationGroup]; sourceGroupArray.splice(source.index, 1); destinationGroupArray.splice(destination.index, 0, draggedItem); return { ...prevData, [sourceGroup]: sourceGroupArray, [destinationGroup]: destinationGroupArray, }; }, false ); else if (moduleId) mutate<{ [key: string]: IIssue[]; }>( MODULE_ISSUES_WITH_PARAMS(moduleId as string, params), (prevData) => { if (!prevData) return prevData; const sourceGroupArray = prevData[sourceGroup]; const destinationGroupArray = groupedByIssues[destinationGroup]; sourceGroupArray.splice(source.index, 1); destinationGroupArray.splice(destination.index, 0, draggedItem); return { ...prevData, [sourceGroup]: sourceGroupArray, [destinationGroup]: destinationGroupArray, }; }, false ); else mutate<{ [key: string]: IIssue[] }>( PROJECT_ISSUES_LIST_WITH_PARAMS(projectId as string, params), (prevData) => { if (!prevData) return prevData; const sourceGroupArray = prevData[sourceGroup]; const destinationGroupArray = groupedByIssues[destinationGroup]; sourceGroupArray.splice(source.index, 1); destinationGroupArray.splice(destination.index, 0, draggedItem); return { ...prevData, [sourceGroup]: sourceGroupArray, [destinationGroup]: destinationGroupArray, }; }, false ); // patch request issuesService .patchIssue(workspaceSlug as string, projectId as string, draggedItem.id, { priority: draggedItem.priority, state: draggedItem.state, sort_order: draggedItem.sort_order, }) .then(() => { if (cycleId) mutate(CYCLE_ISSUES(cycleId as string)); if (moduleId) mutate(MODULE_ISSUES(moduleId as string)); mutate(PROJECT_ISSUES_LIST_WITH_PARAMS(projectId as string, params)); }); } }, [ workspaceSlug, cycleId, moduleId, groupedByIssues, projectId, selectedGroup, orderBy, handleDeleteIssue, params, ] ); const addIssueToState = useCallback( (groupTitle: string) => { setCreateIssueModal(true); if (selectedGroup) setPreloadedData({ [selectedGroup]: groupTitle, actionType: "createIssue", }); else setPreloadedData({ actionType: "createIssue" }); }, [setCreateIssueModal, setPreloadedData, selectedGroup] ); const makeIssueCopy = useCallback( (issue: IIssue) => { setCreateIssueModal(true); setPreloadedData({ ...issue, name: `${issue.name} (Copy)`, actionType: "createIssue" }); }, [setCreateIssueModal, setPreloadedData] ); const handleEditIssue = useCallback( (issue: IIssue) => { setEditIssueModal(true); setIssueToEdit({ ...issue, actionType: "edit", cycle: issue.issue_cycle ? issue.issue_cycle.cycle : null, module: issue.issue_module ? issue.issue_module.module : null, }); }, [setEditIssueModal, setIssueToEdit] ); const removeIssueFromCycle = useCallback( (bridgeId: string) => { if (!workspaceSlug || !projectId) return; mutate( CYCLE_ISSUES(cycleId as string), (prevData) => prevData?.filter((p) => p.id !== bridgeId), false ); issuesService .removeIssueFromCycle( workspaceSlug as string, projectId as string, cycleId as string, bridgeId ) .then((res) => { console.log(res); }) .catch((e) => { console.log(e); }); }, [workspaceSlug, projectId, cycleId] ); const removeIssueFromModule = useCallback( (bridgeId: string) => { if (!workspaceSlug || !projectId) return; mutate( MODULE_ISSUES(moduleId as string), (prevData) => prevData?.filter((p) => p.id !== bridgeId), false ); modulesService .removeIssueFromModule( workspaceSlug as string, projectId as string, moduleId as string, bridgeId ) .then((res) => { console.log(res); }) .catch((e) => { console.log(e); }); }, [workspaceSlug, projectId, moduleId] ); const handleTrashBox = useCallback( (isDragging: boolean) => { if (isDragging && !trashBox) setTrashBox(true); }, [trashBox, setTrashBox] ); const nullFilters = Object.keys(filters).filter( (key) => filters[key as keyof IIssueFilterOptions] === null ); return ( <> setCreateViewModal(null)} preLoadedData={createViewModal} /> setCreateIssueModal(false)} prePopulateData={{ ...preloadedData, }} /> setEditIssueModal(false)} data={issueToEdit} /> setDeleteIssueModal(false)} isOpen={deleteIssueModal} data={issueToDelete} />
{Object.keys(filters).map((key) => { if (filters[key as keyof typeof filters] !== null) return (
{replaceUnderscoreIfSnakeCase(key)}: {filters[key as keyof IIssueFilterOptions] === null || (filters[key as keyof IIssueFilterOptions]?.length ?? 0) <= 0 ? ( None ) : Array.isArray(filters[key as keyof IIssueFilterOptions]) ? (
{key === "state" ? (
{filters.state?.map((stateId: any) => { const state = states?.find((s) => s.id === stateId); return (

{getStateGroupIcon( state?.group ?? "backlog", "12", "12", state?.color )} {state?.name ?? ""} setFilters( { state: filters.state?.filter((s: any) => s !== stateId), }, !Boolean(viewId) ) } >

); })}
) : key === "priority" ? (
{filters.priority?.map((priority: any) => (

{getPriorityIcon(priority)} {priority} setFilters( { priority: filters.priority?.filter( (p: any) => p !== priority ), }, !Boolean(viewId) ) } >

))}
) : key === "assignees" ? (
{filters.assignees?.map((memberId: string) => { const member = members?.find((m) => m.member.id === memberId)?.member; return (

{member?.first_name} setFilters( { assignees: filters.assignees?.filter( (p: any) => p !== memberId ), }, !Boolean(viewId) ) } >

); })}
) : (key as keyof IIssueFilterOptions) === "created_by" ? (
{filters.created_by?.map((memberId: string) => { const member = members?.find((m) => m.member.id === memberId)?.member; return (

{member?.first_name} setFilters( { assignees: filters.created_by?.filter( (p: any) => p !== memberId ), }, !Boolean(viewId) ) } >

); })}
) : key === "labels" ? (
{filters.labels?.map((labelId: string) => { const label = issueLabels?.find((l) => l.id === labelId); if (!label) return null; return (
{label.name}
); })}
) : ( (filters[key as keyof IIssueFilterOptions] as any)?.join(", ") )}
) : ( {filters[key as keyof typeof filters]} )}
); })}
{Object.keys(filters).length > 0 && nullFilters.length !== Object.keys(filters).length && ( { if (viewId) { setFilters({}, true); setToastAlert({ title: "View updated", message: "Your view has been updated", type: "success", }); } else setCreateViewModal({ query: filters, }); }} className="flex items-center gap-2 text-sm" > {!viewId && } {viewId ? "Update" : "Save"} view )}
{Object.keys(filters).length > 0 && nullFilters.length !== Object.keys(filters).length && ( )}
{(provided, snapshot) => (
Drop issue here to delete {provided.placeholder}
)}
{groupedByIssues ? ( isNotEmpty ? ( <> {isCompleted && (
Completed cycles are not editable.
)} {issueView === "list" ? ( ) : issueView === "kanban" ? ( ) : ( )} ) : (
Use
C
shortcut to create a new issue } Icon={PlusIcon} action={() => { const e = new KeyboardEvent("keydown", { key: "c", }); document.dispatchEvent(e); }} /> {openIssuesListModal && ( )}
) ) : (
)}
); };