Skip to content

Commit

Permalink
NCL-8833 Remove query string for legacy url rediret
Browse files Browse the repository at this point in the history
  • Loading branch information
DnsZhou committed Nov 19, 2024
1 parent 7bee175 commit 562e408
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
46 changes: 27 additions & 19 deletions src/hooks/useLegacyUrlRedirector.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
import { useEffect } from 'react';

import { URL_BASE_PATH } from 'common/constants';

export const useLegacyUrlRedirector = () => {
useEffect(() => {
const { pathname, hash } = window.location;
const { pathname, hash } = window.location;

if (pathname.startsWith(URL_BASE_PATH + '/') && hash.startsWith('#/')) {
let newPath = '/' + hash.substring(2); // Remove '#/' and concatenate
if (pathname.startsWith(URL_BASE_PATH + '/') && hash.startsWith('#/')) {
let newPath = '/' + hash.substring(2); // Remove '#/' and concatenate

// If it contains '/build-configs/', remove everything between '/pnc-web/' and '/build-configs/'
const buildConfigsIndex = newPath.indexOf('/build-configs/');
if (buildConfigsIndex !== -1) {
newPath = newPath.substring(buildConfigsIndex);
}
console.log(
`You are accessing a legacy PNC URL: ${window.location}. Please save the new URL after redirection for a better experience.`
);

// If it contains '/builds/', remove everything between '/pnc-web/' and '/builds/'
const buildsIndex = newPath.indexOf('/builds/');
if (buildsIndex !== -1) {
newPath = newPath.substring(buildsIndex);
}
// Remove the query string part
const queryIndex = newPath.indexOf('?');
if (queryIndex !== -1) {
const removedQueryString = newPath.substring(queryIndex);
console.log(`Query string removed from old URL: ${removedQueryString}`);
newPath = newPath.substring(0, queryIndex);
}

console.log(`Redirecting to new URL: ${newPath}`);
window.location.href = URL_BASE_PATH + newPath;
// If it contains '/build-configs/', remove everything between '/pnc-web/' and '/build-configs/'
const buildConfigsIndex = newPath.indexOf('/build-configs/');
if (buildConfigsIndex !== -1) {
newPath = newPath.substring(buildConfigsIndex);
}
}, []);

// If it contains '/builds/', remove everything between '/pnc-web/' and '/builds/'
const buildsIndex = newPath.indexOf('/builds/');
if (buildsIndex !== -1) {
newPath = newPath.substring(buildsIndex);
}

console.log(`Redirecting to new URL: ${newPath}`);
window.location.href = URL_BASE_PATH + newPath;
}
};
4 changes: 2 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import reportWebVitals from './reportWebVitals';

const router = createBrowserRouter(createRoutesFromElements(AppRoutes), { basename: URL_BASE_PATH });

useLegacyUrlRedirector();

const App = () => {
const [isKeycloakInitiated, setIsKeycloakInitiated] = useState<boolean>(false);
const [isKeycloakInitFail, setIsKeycloakInitFail] = useState<boolean>(false);
const [isKeycloakInitInProcess, setIsKeycloakInitInProcess] = useState<boolean>(true);

useLegacyUrlRedirector();

useEffect(() => {
keycloakService
.isInitialized()
Expand Down

0 comments on commit 562e408

Please sign in to comment.