2022-01-27 22:10:12 +00:00
|
|
|
import React, { useContext } from "react";
|
|
|
|
import { useErrorHandler } from "react-error-boundary";
|
|
|
|
import { AppContext } from "context/app";
|
2021-06-04 13:00:14 +00:00
|
|
|
|
2021-10-26 14:24:16 +00:00
|
|
|
interface IAuthAnyAdminRoutesProps {
|
2021-06-04 13:00:14 +00:00
|
|
|
children: JSX.Element;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-27 22:10:12 +00:00
|
|
|
* Checks if a user is any maintainer or any admin when routing
|
2021-06-04 13:00:14 +00:00
|
|
|
*/
|
2021-10-26 14:24:16 +00:00
|
|
|
const AuthAnyAdminRoutes = ({
|
2021-10-22 15:34:45 +00:00
|
|
|
children,
|
2021-10-26 14:24:16 +00:00
|
|
|
}: IAuthAnyAdminRoutesProps): JSX.Element | null => {
|
2022-01-27 22:10:12 +00:00
|
|
|
const handlePageError = useErrorHandler();
|
|
|
|
const { currentUser, isGlobalAdmin, isAnyTeamAdmin } = useContext(AppContext);
|
2021-06-04 13:00:14 +00:00
|
|
|
|
2022-01-27 22:10:12 +00:00
|
|
|
if (!currentUser) {
|
2021-06-04 13:00:14 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-01-27 22:10:12 +00:00
|
|
|
if (!isGlobalAdmin && !isAnyTeamAdmin) {
|
|
|
|
handlePageError({ status: 403 });
|
2021-06-04 13:00:14 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-01-27 22:10:12 +00:00
|
|
|
|
2021-06-04 13:00:14 +00:00
|
|
|
return <>{children}</>;
|
|
|
|
};
|
|
|
|
|
2021-10-26 14:24:16 +00:00
|
|
|
export default AuthAnyAdminRoutes;
|