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";
|
|
|
|
|
2023-12-29 07:06:37 +00:00
|
|
|
import { TenantStatus } from "@docspace/shared/enums";
|
2024-02-18 17:38:24 +00:00
|
|
|
import { combineUrl } from "@docspace/shared/utils/combineUrl";
|
2024-03-14 12:54:21 +00:00
|
|
|
import { isPublicPreview, 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;
|
2023-04-07 11:00:36 +00:00
|
|
|
|
|
|
|
const location = useLocation();
|
|
|
|
|
2023-04-06 12:28:02 +00:00
|
|
|
const renderComponent = () => {
|
2023-04-07 11:00:36 +00:00
|
|
|
const isPreparationPortalUrl = location.pathname === "/preparation-portal";
|
2023-11-09 09:20:31 +00:00
|
|
|
const isDeactivationPortalUrl = location.pathname === "/unavailable";
|
2023-11-10 10:58:27 +00:00
|
|
|
const isPortalRestriction = location.pathname === "/access-restricted";
|
2023-01-24 13:19:06 +00:00
|
|
|
const isPortalRestoring = tenantStatus === TenantStatus.PortalRestore;
|
|
|
|
|
2023-04-06 12:28:02 +00:00
|
|
|
// 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-14 12:54:21 +00:00
|
|
|
if (isPublicRoom() || isPublicPreview()) {
|
2023-05-16 10:30:47 +00:00
|
|
|
return children;
|
|
|
|
}
|
|
|
|
|
2023-11-10 10:58:27 +00:00
|
|
|
if (
|
|
|
|
(isAuthenticated && !isPortalRestoring && !isPortalDeactivate) ||
|
|
|
|
(!location?.state?.isRestrictionError && isPortalRestriction)
|
|
|
|
) {
|
2024-02-18 17:38:24 +00:00
|
|
|
return <Navigate replace to="/" />;
|
2023-01-24 13:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isAuthenticated && isPortalRestoring && !isPreparationPortalUrl) {
|
|
|
|
return (
|
2023-04-06 14:02:10 +00:00
|
|
|
<Navigate
|
|
|
|
replace
|
2023-04-06 12:28:02 +00:00
|
|
|
to={combineUrl(
|
|
|
|
window.DocSpaceConfig?.proxy?.url,
|
2024-02-18 17:38:24 +00:00
|
|
|
"/preparation-portal",
|
2023-04-06 12:28:02 +00:00
|
|
|
)}
|
2023-01-24 13:19:06 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2023-11-09 09:20:31 +00:00
|
|
|
if (isAuthenticated && isPortalDeactivate && !isDeactivationPortalUrl) {
|
|
|
|
return (
|
|
|
|
<Navigate
|
|
|
|
replace
|
|
|
|
to={combineUrl(window.DocSpaceConfig?.proxy?.url, "/unavailable")}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2023-04-07 11:00:36 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-24 13:19:06 +00:00
|
|
|
if (
|
|
|
|
!isAuthenticated &&
|
|
|
|
isPortalRestoring &&
|
|
|
|
wizardCompleted &&
|
|
|
|
!isPreparationPortalUrl
|
|
|
|
) {
|
|
|
|
return (
|
2023-04-06 14:02:10 +00:00
|
|
|
<Navigate
|
|
|
|
replace
|
2023-04-06 12:28:02 +00:00
|
|
|
to={combineUrl(
|
|
|
|
window.DocSpaceConfig?.proxy?.url,
|
2024-02-18 17:38:24 +00:00
|
|
|
"/preparation-portal",
|
2023-04-06 12:28:02 +00:00
|
|
|
)}
|
2023-01-24 13:19:06 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2023-11-09 09:20:31 +00:00
|
|
|
if (
|
|
|
|
!isAuthenticated &&
|
|
|
|
isPortalDeactivate &&
|
|
|
|
wizardCompleted &&
|
|
|
|
!isDeactivationPortalUrl
|
|
|
|
) {
|
|
|
|
return (
|
|
|
|
<Navigate
|
|
|
|
replace
|
|
|
|
to={combineUrl(window.DocSpaceConfig?.proxy?.url, "/unavailable")}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2023-01-24 13:19:06 +00:00
|
|
|
|
2023-11-09 09:20:31 +00:00
|
|
|
if (
|
|
|
|
wizardCompleted &&
|
|
|
|
!isAuthenticated &&
|
|
|
|
!isPortalRestoring &&
|
|
|
|
!isPortalDeactivate
|
|
|
|
) {
|
2023-04-24 10:25:14 +00:00
|
|
|
window.location.replace(
|
2024-02-18 17:38:24 +00:00
|
|
|
combineUrl(window.DocSpaceConfig?.proxy?.url, "/login"),
|
2023-04-24 10:25:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2023-01-24 13:19:06 +00:00
|
|
|
|
2023-04-06 12:28:02 +00:00
|
|
|
return children;
|
2021-03-18 18:54:03 +00:00
|
|
|
};
|
2023-04-06 12:28:02 +00:00
|
|
|
|
|
|
|
const component = renderComponent();
|
|
|
|
|
|
|
|
return component;
|
2021-03-18 18:54:03 +00:00
|
|
|
};
|