import { useRef, useState } from "react"; import Link from "next/link"; import { useRouter } from "next/router"; import { DraggableProvided, DraggableStateSnapshot } from "@hello-pangea/dnd"; import { Disclosure, Transition } from "@headlessui/react"; import { observer } from "mobx-react-lite"; // icons import { MoreVertical, PenSquare, LinkIcon, Star, FileText, Settings, Share2, LogOut, ChevronDown, MoreHorizontal, Inbox, } from "lucide-react"; // hooks import { useApplication, useEventTracker, useInbox, useProject } from "hooks/store"; import useOutsideClickDetector from "hooks/use-outside-click-detector"; import useToast from "hooks/use-toast"; // helpers import { cn } from "helpers/common.helper"; import { getNumberCount } from "helpers/string.helper"; import { renderEmoji } from "helpers/emoji.helper"; // components import { CustomMenu, Tooltip, ArchiveIcon, PhotoFilterIcon, DiceIcon, ContrastIcon, LayersIcon } from "@plane/ui"; import { LeaveProjectModal, PublishProjectModal } from "components/project"; import { EUserProjectRoles } from "constants/project"; type Props = { projectId: string; provided?: DraggableProvided; snapshot?: DraggableStateSnapshot; handleCopyText: () => void; shortContextMenu?: boolean; disableDrag?: boolean; }; const navigation = (workspaceSlug: string, projectId: string) => [ { name: "Issues", href: `/${workspaceSlug}/projects/${projectId}/issues`, Icon: LayersIcon, }, { name: "Cycles", href: `/${workspaceSlug}/projects/${projectId}/cycles`, Icon: ContrastIcon, }, { name: "Modules", href: `/${workspaceSlug}/projects/${projectId}/modules`, Icon: DiceIcon, }, { name: "Views", href: `/${workspaceSlug}/projects/${projectId}/views`, Icon: PhotoFilterIcon, }, { name: "Pages", href: `/${workspaceSlug}/projects/${projectId}/pages`, Icon: FileText, }, { name: "Inbox", href: `/${workspaceSlug}/projects/${projectId}/inbox`, Icon: Inbox, }, { name: "Settings", href: `/${workspaceSlug}/projects/${projectId}/settings`, Icon: Settings, }, ]; export const ProjectSidebarListItem: React.FC = observer((props) => { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { projectId, provided, snapshot, handleCopyText, shortContextMenu = false, disableDrag } = props; // store hooks const { theme: themeStore } = useApplication(); const { setTrackElement } = useEventTracker(); const { addProjectToFavorites, removeProjectFromFavorites, getProjectById } = useProject(); const { getInboxesByProjectId, getInboxById } = useInbox(); // states const [leaveProjectModalOpen, setLeaveProjectModal] = useState(false); const [publishModalOpen, setPublishModal] = useState(false); const [isMenuActive, setIsMenuActive] = useState(false); // router const router = useRouter(); const { workspaceSlug, projectId: URLProjectId } = router.query; // toast alert const { setToastAlert } = useToast(); // derived values const project = getProjectById(projectId); const isAdmin = project?.member_role === EUserProjectRoles.ADMIN; const isViewerOrGuest = project?.member_role && [EUserProjectRoles.VIEWER, EUserProjectRoles.GUEST].includes(project.member_role); const isCollapsed = themeStore.sidebarCollapsed; const actionSectionRef = useRef(null); const inboxesMap = project?.inbox_view ? getInboxesByProjectId(projectId) : undefined; const inboxDetails = inboxesMap && inboxesMap.length > 0 ? getInboxById(inboxesMap[0]) : undefined; const handleAddToFavorites = () => { if (!workspaceSlug || !project) return; addProjectToFavorites(workspaceSlug.toString(), project.id).catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Couldn't remove the project from favorites. Please try again.", }); }); }; const handleRemoveFromFavorites = () => { if (!workspaceSlug || !project) return; removeProjectFromFavorites(workspaceSlug.toString(), project.id).catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Couldn't remove the project from favorites. Please try again.", }); }); }; const handleLeaveProject = () => { setTrackElement("APP_SIDEBAR_PROJECT_DROPDOWN"); setLeaveProjectModal(true); }; const handleLeaveProjectModalClose = () => { setLeaveProjectModal(false); }; const handleProjectClick = () => { if (window.innerWidth < 768) { themeStore.toggleSidebar(); } }; useOutsideClickDetector(actionSectionRef, () => setIsMenuActive(false)); if (!project) return null; return ( <> setPublishModal(false)} /> {({ open }) => ( <>
{provided && !disableDrag && ( )}
{project.emoji ? ( {renderEmoji(project.emoji)} ) : project.icon_prop ? (
{renderEmoji(project.icon_prop)}
) : ( {project?.name.charAt(0)} )} {!isCollapsed &&

{project.name}

}
{!isCollapsed && ( )}
{!isCollapsed && ( setIsMenuActive(!isMenuActive)} >
} className={`hidden flex-shrink-0 group-hover:block ${isMenuActive ? "!block" : ""}`} buttonClassName="!text-custom-sidebar-text-400" ellipsis placement="bottom-start" > {!project.is_favorite && ( Add to favorites )} {project.is_favorite && ( Remove from favorites )} Copy project link {/* publish project settings */} {isAdmin && ( setPublishModal(true)}>
{project.is_deployed ? "Publish settings" : "Publish"}
)} {!isViewerOrGuest && (
Archived issues
)}
Draft issues
Settings
{/* leave project */} {isViewerOrGuest && (
Leave project
)} )} {navigation(workspaceSlug as string, project?.id).map((item) => { if ( (item.name === "Cycles" && !project.cycle_view) || (item.name === "Modules" && !project.module_view) || (item.name === "Views" && !project.issue_views_view) || (item.name === "Pages" && !project.page_view) || (item.name === "Inbox" && !project.inbox_view) ) return; return (
{item.name === "Inbox" && inboxDetails ? ( <>
{inboxDetails?.pending_issue_count > 0 && ( = 100, }, { "border-none bg-custom-primary-300 text-white": router.asPath.includes( item.href ), } )} > {getNumberCount(inboxDetails?.pending_issue_count)} )}
{!isCollapsed && item.name} ) : ( <> {!isCollapsed && item.name} )}
); })}
)}
); });