import React from "react"; import { useRouter } from "next/router"; import Link from "next/link"; import Image from "next/image"; import useSWR from "swr"; import { ChevronDownIcon, ClipboardDocumentListIcon, Cog6ToothIcon, HomeIcon, PlusIcon, RectangleStackIcon, } from "@heroicons/react/24/outline"; import { Menu, Transition } from "@headlessui/react"; // services import userService from "lib/services/user.service"; import workspaceService from "lib/services/workspace.service"; import authenticationService from "lib/services/authentication.service"; // hooks import useUser from "lib/hooks/useUser"; // constants import { USER_WORKSPACES } from "constants/fetch-keys"; // types import { IUser } from "types"; // ui import { Loader } from "ui"; type Props = { sidebarCollapse: boolean; }; const workspaceLinks = (workspaceSlug: string) => [ { icon: HomeIcon, name: "Home", href: `/${workspaceSlug}`, }, { icon: ClipboardDocumentListIcon, name: "Projects", href: `/${workspaceSlug}/projects`, }, { icon: RectangleStackIcon, name: "My Issues", href: `/${workspaceSlug}/me/my-issues`, }, { icon: Cog6ToothIcon, name: "Settings", href: `/${workspaceSlug}/settings`, }, ]; const userLinks = (workspaceSlug: string) => [ { name: "My Profile", href: `/${workspaceSlug}/me/profile`, }, { name: "Workspace Invites", href: "/invitations", }, ]; const WorkspaceOptions: React.FC = ({ sidebarCollapse }) => { const router = useRouter(); const { user, mutateUser } = useUser(); const { data: workspaces } = useSWR(USER_WORKSPACES, () => workspaceService.userWorkspaces(), { shouldRetryOnError: false, }); const { query: { workspaceSlug }, } = router; const activeWorkspace = workspaces?.find((w) => w.slug === workspaceSlug); return (
{activeWorkspace?.logo && activeWorkspace.logo !== "" ? ( Workspace Logo ) : ( activeWorkspace?.name?.charAt(0) ?? "..." )}
{!sidebarCollapse && (

{activeWorkspace?.name ? activeWorkspace.name.length > 17 ? `${activeWorkspace.name.substring(0, 17)}...` : activeWorkspace.name : "Loading..."}

)}
{!sidebarCollapse && (
)}
{user?.email}
{workspaces ? ( <> {workspaces.length > 0 ? ( workspaces.map((workspace) => ( {({ active }) => ( )} )) ) : (

No workspace found!

)} { router.push("/create-workspace"); }} className="flex w-full items-center gap-2 rounded px-2 py-1 text-left text-xs hover:bg-gray-100" > Create Workspace ) : (
)}
{userLinks(workspaceSlug as string).map((link, index) => ( {link.name} ))} { await authenticationService .signOut({ refresh_token: authenticationService.getRefreshToken(), }) .then((response) => { console.log("user signed out", response); }) .catch((error) => { console.log("Failed to sign out", error); }) .finally(() => { mutateUser(); router.push("/signin"); }); }} > Sign out
{workspaceLinks(workspaceSlug as string).map((link, index) => ( ))}
); }; export default WorkspaceOptions;