DocSpace-client/packages/shared/routes/Route.public.tsx

120 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-03-18 18:54:03 +00:00
/* eslint-disable react/prop-types */
import React from "react";
2024-02-18 17:38:24 +00:00
import { Navigate, useLocation } from "react-router-dom";
import { TenantStatus } from "@docspace/shared/enums";
2024-02-18 17:38:24 +00:00
import { combineUrl } from "@docspace/shared/utils/combineUrl";
2024-03-04 12:22:54 +00:00
import { isPublicRoom } from "@docspace/shared/utils/common";
2024-02-18 17:38:24 +00:00
import type { PublicRouteProps } from "./Routers.types";
2021-03-18 18:54:03 +00:00
2024-02-18 17:38:24 +00:00
export const PublicRoute = (props: PublicRouteProps) => {
const {
wizardCompleted,
isAuthenticated,
tenantStatus,
isPortalDeactivate,
children,
} = props;
const location = useLocation();
const renderComponent = () => {
const isPreparationPortalUrl = location.pathname === "/preparation-portal";
const isDeactivationPortalUrl = location.pathname === "/unavailable";
const isPortalRestriction = location.pathname === "/access-restricted";
const isPortalRestoring = tenantStatus === TenantStatus.PortalRestore;
// if (!isLoaded) {
// return <AppLoader />;
// }
2021-03-18 18:54:03 +00:00
2023-11-10 09:55:42 +00:00
if (location?.state?.isRestrictionError) {
return children;
}
2024-03-04 12:22:54 +00:00
if (isPublicRoom()) {
return children;
}
if (
(isAuthenticated && !isPortalRestoring && !isPortalDeactivate) ||
(!location?.state?.isRestrictionError && isPortalRestriction)
) {
2024-02-18 17:38:24 +00:00
return <Navigate replace to="/" />;
}
if (isAuthenticated && isPortalRestoring && !isPreparationPortalUrl) {
return (
2023-04-06 14:02:10 +00:00
<Navigate
replace
to={combineUrl(
window.DocSpaceConfig?.proxy?.url,
2024-02-18 17:38:24 +00:00
"/preparation-portal",
)}
/>
);
}
if (isAuthenticated && isPortalDeactivate && !isDeactivationPortalUrl) {
return (
<Navigate
replace
to={combineUrl(window.DocSpaceConfig?.proxy?.url, "/unavailable")}
/>
);
}
if (!wizardCompleted && location.pathname !== "/wizard") {
2024-02-18 17:38:24 +00:00
return <Navigate replace to="/wizard" />;
2021-03-18 19:16:09 +00:00
}
if (
!isAuthenticated &&
isPortalRestoring &&
wizardCompleted &&
!isPreparationPortalUrl
) {
return (
2023-04-06 14:02:10 +00:00
<Navigate
replace
to={combineUrl(
window.DocSpaceConfig?.proxy?.url,
2024-02-18 17:38:24 +00:00
"/preparation-portal",
)}
/>
);
}
if (
!isAuthenticated &&
isPortalDeactivate &&
wizardCompleted &&
!isDeactivationPortalUrl
) {
return (
<Navigate
replace
to={combineUrl(window.DocSpaceConfig?.proxy?.url, "/unavailable")}
/>
);
}
if (
wizardCompleted &&
!isAuthenticated &&
!isPortalRestoring &&
!isPortalDeactivate
) {
window.location.replace(
2024-02-18 17:38:24 +00:00
combineUrl(window.DocSpaceConfig?.proxy?.url, "/login"),
);
return null;
}
return children;
2021-03-18 18:54:03 +00:00
};
const component = renderComponent();
return component;
2021-03-18 18:54:03 +00:00
};