From e716e04ea8f8864024abcecfafe0e6dd1312fb0b Mon Sep 17 00:00:00 2001 From: Jessie Wei Date: Tue, 18 Jun 2024 14:55:50 +1000 Subject: [PATCH] chore: Fix the github page redirect issue --- .../GithubPagesNavigationHelper/index.tsx | 47 ------------------- .../src/containers/App/index.tsx | 8 +--- .../src/routes/initialWorkspaceLoader.tsx | 23 +++++++++ 3 files changed, 24 insertions(+), 54 deletions(-) delete mode 100644 packages/threat-composer-app/src/components/GithubPagesNavigationHelper/index.tsx diff --git a/packages/threat-composer-app/src/components/GithubPagesNavigationHelper/index.tsx b/packages/threat-composer-app/src/components/GithubPagesNavigationHelper/index.tsx deleted file mode 100644 index 4a51a514..00000000 --- a/packages/threat-composer-app/src/components/GithubPagesNavigationHelper/index.tsx +++ /dev/null @@ -1,47 +0,0 @@ -/** ******************************************************************************************************************* - Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - - Licensed under the Apache License, Version 2.0 (the "License"). - You may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - ******************************************************************************************************************** */ -import { useEffect, FC, PropsWithChildren } from 'react'; -import { useNavigate } from 'react-router-dom'; - -const requiredRewriteUrl = (search: string) => { - return search && (search.startsWith('?/') || search.startsWith('?%2F')); -}; - -const ROUTE_BASE_PATH = process.env.REACT_APP_ROUTE_BASE_PATH || ''; - -const GithubPagesNavigationHelper: FC> = ({ - children, -}) => { - const navigate = useNavigate(); - useEffect(() => { - const l = window.location; - if (requiredRewriteUrl(l.search)) { - let search = decodeURIComponent(l.search); - if (search.indexOf('=') === search.length - 1) { - search = search.slice(0, search.length - 1); - } - var decoded = search.slice(1).split('&').map(function (s) { - return s.replace(/~and~/g, '&'); - }).join('?'); - - navigate(ROUTE_BASE_PATH + '/' + decoded + l.hash); - } - }, [window.location.search]); - - return requiredRewriteUrl(window.location.search) ? <> : <>{children}; -}; - -export default GithubPagesNavigationHelper; \ No newline at end of file diff --git a/packages/threat-composer-app/src/containers/App/index.tsx b/packages/threat-composer-app/src/containers/App/index.tsx index 9da3ca26..4a69af03 100644 --- a/packages/threat-composer-app/src/containers/App/index.tsx +++ b/packages/threat-composer-app/src/containers/App/index.tsx @@ -16,10 +16,8 @@ import { FC } from 'react'; import Full from './components/Full'; import Standalone from './components/Standalone'; -import GithubPagesNavigationHelper from '../../components/GithubPagesNavigationHelper'; import getComposerMode from '../../utils/getComposerMode'; -const isGithubPages = process.env.REACT_APP_GITHUB_PAGES === 'true'; /** * Demo app for threat-composer @@ -31,11 +29,7 @@ const App: FC = () => { return (composerMode === 'ThreatsOnly' || composerMode === 'EditorOnly') ? ( - ) : ( - isGithubPages ? - () : - - ); + ) : (); }; export default App; diff --git a/packages/threat-composer-app/src/routes/initialWorkspaceLoader.tsx b/packages/threat-composer-app/src/routes/initialWorkspaceLoader.tsx index a615b544..e6678187 100644 --- a/packages/threat-composer-app/src/routes/initialWorkspaceLoader.tsx +++ b/packages/threat-composer-app/src/routes/initialWorkspaceLoader.tsx @@ -17,7 +17,30 @@ import { LOCAL_STORAGE_KEY_CURRENT_WORKSPACE, Workspace } from '@aws/threat-comp import { generatePath, redirect } from 'react-router-dom'; import { ROUTE_WORKSPACE_DEFAULT, ROUTE_WORKSPACE_PATH } from '../config/routes'; +const isGithubPages = process.env.REACT_APP_GITHUB_PAGES === 'true'; + +const requiredRewriteUrl = (search: string) => { + return search && (search.startsWith('?/') || search.startsWith('?%2F')); +}; + const initialWorkspaceLoader = async () => { + const l = window.location; + + // For github page, the direct navigation to workspace pages (e.g., https://awslabs.github.io/threat-composer/workspaces/default/dashboard) results in 404. + // In 404.html, the url is rewrited to https://awslabs.github.io/threat-composer?/workspaces/default/dashboard so that the index.html can be loaded. + // And here is to reconstruct the original url and navigate to the right page. + if (isGithubPages && requiredRewriteUrl(l.search)) { + let search = decodeURIComponent(l.search); + if (search.indexOf('=') === search.length - 1) { + search = search.slice(0, search.length - 1); + } + var decoded = search.slice(1).split('&').map(function (s) { + return s.replace(/~and~/g, '&'); + }).join('?'); + + return redirect(decoded + l.hash); + } + const currentWorkspaceValue = window.localStorage.getItem(LOCAL_STORAGE_KEY_CURRENT_WORKSPACE); if (currentWorkspaceValue) {