From 96f32d53dd2fb96e13c3a1c09b17d69e08f69399 Mon Sep 17 00:00:00 2001 From: Vivek Date: Tue, 17 Dec 2024 16:34:10 -0800 Subject: [PATCH] Routing changes --- apps/gitness/src/AppMFE.tsx | 54 ++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/apps/gitness/src/AppMFE.tsx b/apps/gitness/src/AppMFE.tsx index 8c9090ea6..799dd9747 100644 --- a/apps/gitness/src/AppMFE.tsx +++ b/apps/gitness/src/AppMFE.tsx @@ -2,7 +2,7 @@ import './AppMFE.css' import { useEffect } from 'react' import { I18nextProvider } from 'react-i18next' -import { BrowserRouter, Route, Routes } from 'react-router-dom' +import { BrowserRouter, Route, Routes, useLocation, useNavigate } from 'react-router-dom' import { ChildComponentProps } from '@harness/microfrontends' import { QueryClientProvider } from '@tanstack/react-query' @@ -33,7 +33,51 @@ import { RepoImportContainer } from './pages/repo/repo-import-container' const BASE_URL_PREFIX = `${window.apiUrl || ''}/api/v1` -export default function AppMFE({ scope, renderUrl, on401, useMFEThemeContext }: ChildComponentProps) { +function LocationChangeHandler({ + renderUrl, + locationPathname, + onRouteChange +}: { + renderUrl: string + locationPathname: string + onRouteChange: (updatedLocationPathname: string) => void +}) { + // Handle location change detected from parent route + const navigate = useNavigate() + useEffect(() => { + console.log('locationPathname', locationPathname) + if (renderUrl) { + const pathToReplace = renderUrl + console.log('pathToReplace', pathToReplace) + + const pathToNavigate = locationPathname.replace(pathToReplace, '') + console.log('pathToNavigate', pathToNavigate) + + navigate(pathToNavigate) + } + }, [locationPathname]) + + // Notify parent about route change + const location = useLocation() + useEffect(() => { + console.log('location.pathname -> Child', location.pathname) + console.log('locationPathname -> Parent', locationPathname) + if (location.pathname !== locationPathname) { + onRouteChange?.(`${renderUrl}${location.pathname}`) + } + }, [location]) + + return <> +} + +export default function AppMFE({ + scope, + renderUrl, + on401, + useMFEThemeContext, + locationPathname, + onRouteChange +}: ChildComponentProps) { new CodeServiceAPIClient({ urlInterceptor: (url: string) => `/code${BASE_URL_PREFIX}${url}`, responseInterceptor: (response: Response) => { @@ -47,7 +91,6 @@ export default function AppMFE({ scope, renderUrl, on401, useMFEThemeContext }: }) const { theme } = useMFEThemeContext() - console.log('MFE Theme', theme) const { setTheme } = useThemeStore() useEffect(() => { @@ -68,6 +111,11 @@ export default function AppMFE({ scope, renderUrl, on401, useMFEThemeContext }: + } /> } />