import { Fragment, useState } from "react"; import { observer } from "mobx-react-lite"; import Link from "next/link"; import { useRouter } from "next/router"; import { useTheme } from "next-themes"; import { usePopper } from "react-popper"; import { mutate } from "swr"; // ui import { Menu, Transition } from "@headlessui/react"; // icons import { Check, ChevronDown, CircleUserRound, LogOut, Mails, PlusSquare, Settings, UserCircle2 } from "lucide-react"; // plane ui import { Avatar, Loader, TOAST_TYPE, setToast } from "@plane/ui"; // hooks import { useApplication, useUser, useWorkspace } from "hooks/store"; // types import { IWorkspace } from "@plane/types"; // Static Data const userLinks = (workspaceSlug: string, userId: string) => [ { key: "workspace_invites", name: "Workspace invites", href: "/invitations", icon: Mails, }, { key: "view_profile", name: "View profile", href: `/${workspaceSlug}/profile/${userId}`, icon: CircleUserRound, }, { key: "settings", name: "Settings", href: `/${workspaceSlug}/settings`, icon: Settings, }, ]; const profileLinks = (workspaceSlug: string, userId: string) => [ { name: "View profile", icon: UserCircle2, link: `/${workspaceSlug}/profile/${userId}`, }, { name: "Settings", icon: Settings, link: "/profile", }, ]; export const WorkspaceSidebarDropdown = observer(() => { // router const router = useRouter(); const { workspaceSlug } = router.query; // store hooks const { theme: { sidebarCollapsed, toggleSidebar }, } = useApplication(); const { currentUser, updateCurrentUser, isUserInstanceAdmin, signOut } = useUser(); const { currentWorkspace: activeWorkspace, workspaces } = useWorkspace(); const { setTheme } = useTheme(); // popper-js refs const [referenceElement, setReferenceElement] = useState(null); const [popperElement, setPopperElement] = useState(null); // popper-js init const { styles, attributes } = usePopper(referenceElement, popperElement, { placement: "right", modifiers: [ { name: "preventOverflow", options: { padding: 12, }, }, ], }); const handleWorkspaceNavigation = (workspace: IWorkspace) => updateCurrentUser({ last_workspace_id: workspace?.id, }); const handleSignOut = async () => { await signOut() .then(() => { mutate("CURRENT_USER_DETAILS", null); setTheme("system"); router.push("/"); }) .catch(() => setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: "Failed to sign out. Please try again.", }) ); }; const handleItemClick = () => { if (window.innerWidth < 768) { toggleSidebar(); } }; const workspacesList = Object.values(workspaces ?? {}); // TODO: fix workspaces list scroll return (
{({ open }) => ( <>
{activeWorkspace?.logo && activeWorkspace.logo !== "" ? ( Workspace Logo ) : ( activeWorkspace?.name?.charAt(0) ?? "..." )}
{!sidebarCollapsed && (

{activeWorkspace?.name ? activeWorkspace.name : "Loading..."}

)}
{!sidebarCollapsed && (
{currentUser?.email}
{workspacesList ? (
{workspacesList.length > 0 && workspacesList.map((workspace) => ( { handleWorkspaceNavigation(workspace); handleItemClick(); }} className="w-full" >
{workspace?.logo && workspace.logo !== "" ? ( Workspace Logo ) : ( workspace?.name?.charAt(0) ?? "..." )}
{workspace.name}
{workspace.id === activeWorkspace?.id && ( )}
))}
) : (
)}
Create workspace {userLinks(workspaceSlug?.toString() ?? "", currentUser?.id ?? "").map((link, index) => ( { if (index > 0) handleItemClick(); }} > {link.name} ))}
Sign out
)}
{!sidebarCollapsed && (
{currentUser?.email} {profileLinks(workspaceSlug?.toString() ?? "", currentUser?.id ?? "").map((link, index) => ( { if (index == 0) handleItemClick(); }} > {link.name} ))}
Sign out
{isUserInstanceAdmin && (
Enter God Mode
)}
)}
); });