import React, { useState, FC, useRef, useEffect } from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; // react-beautiful-dnd import { DragDropContext, Draggable, DropResult, Droppable } from "react-beautiful-dnd"; // headless ui import { Disclosure, Transition } from "@headlessui/react"; // hooks import useToast from "hooks/use-toast"; import useTheme from "hooks/use-theme"; import useUserAuth from "hooks/use-user-auth"; import useProjects from "hooks/use-projects"; // components import { DeleteProjectModal, SingleSidebarProject } from "components/project"; // services import projectService from "services/project.service"; // icons import { Icon } from "components/ui"; import { PlusIcon } from "@heroicons/react/24/outline"; // helpers import { copyTextToClipboard } from "helpers/string.helper"; import { orderArrayBy } from "helpers/array.helper"; // types import { IProject } from "types"; // fetch-keys import { PROJECTS_LIST } from "constants/fetch-keys"; // mobx store import { useMobxStore } from "lib/mobx/store-provider"; export const ProjectSidebarList: FC = () => { const store: any = useMobxStore(); const [deleteProjectModal, setDeleteProjectModal] = useState(false); const [projectToDelete, setProjectToDelete] = useState(null); // router const [isScrolled, setIsScrolled] = useState(false); const containerRef = useRef(null); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { user } = useUserAuth(); const { collapsed: sidebarCollapse } = useTheme(); const { setToastAlert } = useToast(); const { projects: allProjects } = useProjects(); const joinedProjects = allProjects?.filter((p) => p.sort_order); const favoriteProjects = allProjects?.filter((p) => p.is_favorite); const otherProjects = allProjects?.filter((p) => p.sort_order === null); const orderedJoinedProjects: IProject[] | undefined = joinedProjects ? orderArrayBy(joinedProjects, "sort_order", "ascending") : undefined; const orderedFavProjects: IProject[] | undefined = favoriteProjects ? orderArrayBy(favoriteProjects, "sort_order", "ascending") : undefined; const handleDeleteProject = (project: IProject) => { setProjectToDelete(project); setDeleteProjectModal(true); }; const handleCopyText = (projectId: string) => { const originURL = typeof window !== "undefined" && window.location.origin ? window.location.origin : ""; copyTextToClipboard(`${originURL}/${workspaceSlug}/projects/${projectId}/issues`).then(() => { setToastAlert({ type: "success", title: "Link Copied!", message: "Project link copied to clipboard.", }); }); }; const onDragEnd = async (result: DropResult) => { const { source, destination, draggableId } = result; if (!destination || !workspaceSlug) return; if (source.index === destination.index) return; const projectsList = (destination.droppableId === "joined-projects" ? orderedJoinedProjects : orderedFavProjects) ?? []; let updatedSortOrder = projectsList[source.index].sort_order; if (destination.index === 0) updatedSortOrder = (projectsList[0].sort_order as number) - 1000; else if (destination.index === projectsList.length - 1) updatedSortOrder = (projectsList[projectsList.length - 1].sort_order as number) + 1000; else { const destinationSortingOrder = projectsList[destination.index].sort_order as number; const relativeDestinationSortingOrder = source.index < destination.index ? (projectsList[destination.index + 1].sort_order as number) : (projectsList[destination.index - 1].sort_order as number); updatedSortOrder = Math.round( (destinationSortingOrder + relativeDestinationSortingOrder) / 2 ); } mutate( PROJECTS_LIST(workspaceSlug as string, { is_favorite: "all" }), (prevData) => { if (!prevData) return prevData; return prevData.map((p) => p.id === draggableId ? { ...p, sort_order: updatedSortOrder } : p ); }, false ); await projectService .setProjectView(workspaceSlug as string, draggableId, { sort_order: updatedSortOrder }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Something went wrong. Please try again.", }); }); }; const handleScroll = () => { if (containerRef.current) { const scrollTop = containerRef.current.scrollTop; setIsScrolled(scrollTop > 0); } }; useEffect(() => { const currentContainerRef = containerRef.current; if (currentContainerRef) { currentContainerRef.addEventListener("scroll", handleScroll); } return () => { if (currentContainerRef) { currentContainerRef.removeEventListener("scroll", handleScroll); } }; }, []); return ( <> setDeleteProjectModal(false)} data={projectToDelete} user={user} />
{(provided) => (
{orderedFavProjects && orderedFavProjects.length > 0 && ( {({ open }) => ( <> {!store?.theme?.sidebarCollapsed && ( Favorites )} {orderedFavProjects.map((project, index) => ( {(provided, snapshot) => (
handleDeleteProject(project)} handleCopyText={() => handleCopyText(project.id)} shortContextMenu />
)}
))}
{provided.placeholder} )}
)}
)}
{(provided) => (
{orderedJoinedProjects && orderedJoinedProjects.length > 0 && ( {({ open }) => ( <> {!store?.theme?.sidebarCollapsed && (
Projects
)} {orderedJoinedProjects.map((project, index) => ( {(provided, snapshot) => (
handleDeleteProject(project)} handleCopyText={() => handleCopyText(project.id)} />
)}
))}
{provided.placeholder} )}
)}
)}
{allProjects && allProjects.length === 0 && ( )}
); };