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

146 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-03-21 14:09:55 +00:00
// (c) Copyright Ascensio System SIA 2009-2024
2024-03-18 09:54:09 +00:00
//
2024-03-17 23:13:02 +00:00
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
2024-03-18 09:54:09 +00:00
//
2024-03-17 23:13:02 +00:00
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
2024-03-18 09:54:09 +00:00
//
2024-03-17 23:13:02 +00:00
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
2024-03-18 09:54:09 +00:00
//
2024-03-17 23:13:02 +00:00
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
2024-03-18 09:54:09 +00:00
//
2024-03-17 23:13:02 +00:00
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
2024-03-18 09:54:09 +00:00
//
2024-03-17 23:13:02 +00:00
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
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-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;
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-14 12:54:21 +00:00
if (isPublicRoom() || isPublicPreview()) {
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(
2024-06-11 08:23:31 +00:00
window.ClientConfig?.proxy?.url,
2024-02-18 17:38:24 +00:00
"/preparation-portal",
)}
/>
);
}
if (isAuthenticated && isPortalDeactivate && !isDeactivationPortalUrl) {
return (
<Navigate
replace
2024-06-11 08:23:31 +00:00
to={combineUrl(window.ClientConfig?.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(
2024-06-11 08:23:31 +00:00
window.ClientConfig?.proxy?.url,
2024-02-18 17:38:24 +00:00
"/preparation-portal",
)}
/>
);
}
if (
!isAuthenticated &&
isPortalDeactivate &&
wizardCompleted &&
!isDeactivationPortalUrl
) {
return (
<Navigate
replace
2024-06-11 08:23:31 +00:00
to={combineUrl(window.ClientConfig?.proxy?.url, "/unavailable")}
/>
);
}
if (
wizardCompleted &&
!isAuthenticated &&
!isPortalRestoring &&
!isPortalDeactivate
) {
window.location.replace(
2024-06-11 08:23:31 +00:00
combineUrl(window.ClientConfig?.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
};