Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

1.0 KB
Raw Blame
import type { ReactNode } from "react";

import { useGetCollectionQuery } from "metabase/api";
import { Unauthorized } from "metabase/common/components/ErrorPages";
import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
import { getUserIsAdmin } from "metabase/current-user";
import { useSelector } from "metabase/redux";
import { Outlet } from "metabase/router";

type Props = {
  children?: ReactNode;
};

export const CanAccessTenantSpecificRoute = ({
  children = <Outlet />,
}: Props) => {
  const isAdmin = useSelector(getUserIsAdmin);

  // Admins always have access, skip the API call
  const { data, isLoading } = useGetCollectionQuery(
    { id: "root", namespace: "tenant-specific" },
    { skip: isAdmin },
  );

  if (isAdmin) {
    return <>{children}</>;
  }

  if (isLoading) {
    return <LoadingAndErrorWrapper loading />;
  }

  // If we got data back, user has access
  if (data) {
    return <>{children}</>;
  }

  // Error or no data means no access
  return <Unauthorized />;
};