Skip to content

Commit

Permalink
Routing changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vivek-harness committed Dec 18, 2024
1 parent 6feb552 commit 7f25e9d
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions apps/gitness/src/AppMFE.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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) => {
Expand All @@ -47,7 +91,6 @@ export default function AppMFE({ scope, renderUrl, on401, useMFEThemeContext }:
})

const { theme } = useMFEThemeContext()
console.log('MFE Theme', theme)

const { setTheme } = useThemeStore()
useEffect(() => {
Expand All @@ -68,6 +111,11 @@ export default function AppMFE({ scope, renderUrl, on401, useMFEThemeContext }:
<NuqsAdapter>
<MFEContext.Provider value={{ scope, renderUrl }}>
<BrowserRouter basename={`/ng${renderUrl}`}>
<LocationChangeHandler
renderUrl={renderUrl}
locationPathname={locationPathname}
onRouteChange={onRouteChange}
/>
<Routes>
<Route path="repos/:repoId/commits" element={<RepoCommitsPage />} />
<Route path="repos/:repoId/branches" element={<RepoBranchesListPage />} />
Expand Down

0 comments on commit 7f25e9d

Please sign in to comment.