import { FC, ReactNode } from "react"; import { observer } from "mobx-react-lite"; import Image from "next/image"; import Link from "next/link"; import { useParams } from "next/navigation"; import { useTheme } from "next-themes"; import useSWR from "swr"; import { LogOut } from "lucide-react"; // hooks import { Button, TOAST_TYPE, setToast, Tooltip } from "@plane/ui"; import { LogoSpinner } from "@/components/common"; import { useMember, useProject, useUser, useWorkspace } from "@/hooks/store"; import { usePlatformOS } from "@/hooks/use-platform-os"; // images import PlaneBlackLogo from "public/plane-logos/black-horizontal-with-blue-logo.png"; import PlaneWhiteLogo from "public/plane-logos/white-horizontal-with-blue-logo.png"; import WorkSpaceNotAvailable from "public/workspace/workspace-not-available.png"; export interface IWorkspaceAuthWrapper { children: ReactNode; } export const WorkspaceAuthWrapper: FC = observer((props) => { const { children } = props; // router params const { workspaceSlug } = useParams(); // next themes const { resolvedTheme } = useTheme(); // store hooks const { membership, signOut, data: currentUser } = useUser(); const { fetchProjects } = useProject(); const { workspace: { fetchWorkspaceMembers }, } = useMember(); const { workspaces } = useWorkspace(); const { isMobile } = usePlatformOS(); const planeLogo = resolvedTheme === "dark" ? PlaneWhiteLogo : PlaneBlackLogo; const allWorkspaces = workspaces ? Object.values(workspaces) : undefined; const currentWorkspace = (allWorkspaces && allWorkspaces.find((workspace) => workspace?.slug === workspaceSlug)) || undefined; // fetching user workspace information useSWR( workspaceSlug && currentWorkspace ? `WORKSPACE_MEMBERS_ME_${workspaceSlug}` : null, workspaceSlug && currentWorkspace ? () => membership.fetchUserWorkspaceInfo(workspaceSlug.toString()) : null, { revalidateIfStale: false, revalidateOnFocus: false } ); // fetching workspace projects useSWR( workspaceSlug && currentWorkspace ? `WORKSPACE_PROJECTS_${workspaceSlug}` : null, workspaceSlug && currentWorkspace ? () => fetchProjects(workspaceSlug.toString()) : null, { revalidateIfStale: false, revalidateOnFocus: false } ); // fetch workspace members useSWR( workspaceSlug && currentWorkspace ? `WORKSPACE_MEMBERS_${workspaceSlug}` : null, workspaceSlug && currentWorkspace ? () => fetchWorkspaceMembers(workspaceSlug.toString()) : null, { revalidateIfStale: false, revalidateOnFocus: false } ); // fetch workspace user projects role useSWR( workspaceSlug && currentWorkspace ? `WORKSPACE_PROJECTS_ROLE_${workspaceSlug}` : null, workspaceSlug && currentWorkspace ? () => membership.fetchUserWorkspaceProjectsRole(workspaceSlug.toString()) : null, { revalidateIfStale: false, revalidateOnFocus: false } ); const handleSignOut = async () => { await signOut().catch(() => setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: "Failed to sign out. Please try again.", }) ); }; // if list of workspaces are not there then we have to render the spinner if (allWorkspaces === undefined) { return (
); } // if workspaces are there and we are trying to access the workspace that we are not part of then show the existing workspaces if ( currentWorkspace === undefined && !membership.currentWorkspaceMemberInfo && membership.hasPermissionToCurrentWorkspace === undefined ) { return (
Plane logo
{currentUser?.email}
Plane logo

Workspace not found

No workspace found with the URL. It may not exist or you lack authorization to view it.

{allWorkspaces && allWorkspaces.length > 1 && ( )}
); } // while user does not have access to view that workspace if ( membership.hasPermissionToCurrentWorkspace !== undefined && membership.hasPermissionToCurrentWorkspace === false ) { return (

Not Authorized!

You{"'"}re not a member of this workspace. Please contact the workspace admin to get an invitation or check your pending invitations.

); } return <>{children}; });