import { useState, FC, useRef, useEffect } from "react"; import { combine } from "@atlaskit/pragmatic-drag-and-drop/combine"; import { autoScrollForElements } from "@atlaskit/pragmatic-drag-and-drop-auto-scroll/element"; import { observer } from "mobx-react"; import { useRouter } from "next/router"; import { ChevronDown, ChevronRight, Plus } from "lucide-react"; import { Disclosure, Transition } from "@headlessui/react"; import { IProject } from "@plane/types"; // hooks import { TOAST_TYPE, setToast } from "@plane/ui"; import { CreateProjectModal, ProjectSidebarListItem } from "@/components/project"; import { EUserWorkspaceRoles } from "@/constants/workspace"; import { cn } from "@/helpers/common.helper"; import { orderJoinedProjects } from "@/helpers/project.helper"; import { copyUrlToClipboard } from "@/helpers/string.helper"; import { useAppTheme, useCommandPalette, useEventTracker, useProject, useUser } from "@/hooks/store"; // ui // components // helpers // constants export const ProjectSidebarList: FC = observer(() => { // states const [isFavoriteProjectCreate, setIsFavoriteProjectCreate] = useState(false); const [isProjectModalOpen, setIsProjectModalOpen] = useState(false); const [isScrolled, setIsScrolled] = useState(false); // scroll animation state // refs const containerRef = useRef(null); // store hooks const { toggleCreateProjectModal } = useCommandPalette(); const { sidebarCollapsed } = useAppTheme(); const { setTrackElement } = useEventTracker(); const { membership: { currentWorkspaceRole }, } = useUser(); const { getProjectById, joinedProjectIds: joinedProjects, favoriteProjectIds: favoriteProjects, updateProjectView, } = useProject(); // router const router = useRouter(); const { workspaceSlug } = router.query; const isAuthorizedUser = !!currentWorkspaceRole && currentWorkspaceRole >= EUserWorkspaceRoles.MEMBER; const handleCopyText = (projectId: string) => { copyUrlToClipboard(`${workspaceSlug}/projects/${projectId}/issues`).then(() => { setToast({ type: TOAST_TYPE.SUCCESS, title: "Link Copied!", message: "Project link copied to clipboard.", }); }); }; const handleOnProjectDrop = ( sourceId: string | undefined, destinationId: string | undefined, shouldDropAtEnd: boolean ) => { if (!sourceId || !destinationId || !workspaceSlug) return; if (sourceId === destinationId) return; const joinedProjectsList: IProject[] = []; joinedProjects.map((projectId) => { const projectDetails = getProjectById(projectId); if (projectDetails) joinedProjectsList.push(projectDetails); }); const sourceIndex = joinedProjects.indexOf(sourceId); const destinationIndex = shouldDropAtEnd ? joinedProjects.length : joinedProjects.indexOf(destinationId); if (joinedProjectsList.length <= 0) return; const updatedSortOrder = orderJoinedProjects(sourceIndex, destinationIndex, sourceId, joinedProjectsList); if (updatedSortOrder != undefined) updateProjectView(workspaceSlug.toString(), sourceId, { sort_order: updatedSortOrder }).catch(() => { setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: "Something went wrong. Please try again.", }); }); }; const isCollapsed = sidebarCollapsed || false; /** * Implementing scroll animation styles based on the scroll length of the container */ useEffect(() => { const handleScroll = () => { if (containerRef.current) { const scrollTop = containerRef.current.scrollTop; setIsScrolled(scrollTop > 0); } }; const currentContainerRef = containerRef.current; if (currentContainerRef) { currentContainerRef.addEventListener("scroll", handleScroll); } return () => { if (currentContainerRef) { currentContainerRef.removeEventListener("scroll", handleScroll); } }; }, [containerRef]); useEffect(() => { const element = containerRef.current; if (!element) return; return combine( autoScrollForElements({ element, canScroll: ({ source }) => source?.data?.dragInstanceId === "PROJECTS", getAllowedAxis: () => "vertical", }) ); }, [containerRef]); return ( <> {workspaceSlug && ( { setIsProjectModalOpen(false); }} setToFavorite={isFavoriteProjectCreate} workspaceSlug={workspaceSlug.toString()} /> )}
{favoriteProjects && favoriteProjects.length > 0 && ( {({ open }) => ( <> {!isCollapsed && (
Favorites {open ? : } {isAuthorizedUser && ( )}
)} {favoriteProjects.map((projectId, index) => ( handleCopyText(projectId)} projectListType="FAVORITES" disableDrag disableDrop isLastChild={index === favoriteProjects.length - 1} /> ))} )}
)}
{joinedProjects && joinedProjects.length > 0 && ( {({ open }) => ( <> {!isCollapsed && (
Your projects {open ? : } {isAuthorizedUser && ( )}
)} {joinedProjects.map((projectId, index) => ( handleCopyText(projectId)} isLastChild={index === joinedProjects.length - 1} handleOnProjectDrop={handleOnProjectDrop} /> ))} )}
)}
{isAuthorizedUser && joinedProjects && joinedProjects.length === 0 && ( )}
); });