33 * SPDX-License-Identifier: Apache-2.0
44 */
55
6- import React , { StrictMode } from "react"
6+ import React , { StrictMode , useEffect , useLayoutEffect } from "react"
77import { createBrowserHistory , createHashHistory , createRouter , RouterProvider } from "@tanstack/react-router"
88import { AppShellProvider } from "@cloudoperators/juno-ui-components"
99import { MessagesProvider } from "@cloudoperators/juno-messages-provider"
1010import { decodeV2 , encodeV2 } from "@cloudoperators/juno-url-state-provider"
1111import Auth from "./components/Auth"
1212import styles from "./styles.css?inline"
1313import StoreProvider from "./components/StoreProvider"
14- import { AuthProvider } from "./components/AuthProvider"
14+ import { AuthProvider , useAuth } from "./components/AuthProvider"
1515import { routeTree } from "./routeTree.gen"
1616
1717// Create a new router instance
@@ -39,25 +39,35 @@ export type AppProps = {
3939 enableHashedRouting ?: boolean
4040}
4141
42- const StyledShell = ( props : AppProps ) => {
43- props = { ...props , currentHost : props . currentHost === "origin" ? window . location . origin : props . currentHost }
42+ const getBasePath = ( auth : any ) => {
43+ // Determine if org is part of the domain
44+ const currentUrl = new URL ( window . location . href )
45+ const organizationIsPartOfDomain = currentUrl . host . match ( / ^ ( .+ ) \. d a s h b o a r d \. .+ / )
46+ if ( organizationIsPartOfDomain ) {
47+ return "/"
48+ }
49+ // If the organization is not part of the domain, extract it from the auth token
50+ const orgString = auth ?. data ?. raw . groups ?. find ( ( g : any ) => g . indexOf ( "organization:" ) === 0 )
51+ return orgString ? orgString . split ( ":" ) [ 1 ] : undefined
52+ }
4453
54+ function App ( props : AppProps ) {
55+ const auth = useAuth ( )
4556 /*
4657 * Dynamically change the type of history on the router
4758 * based on the enableHashedRouting prop. This ensures that
4859 * the correct history type is used when A Shell app does not
4960 * want the app to use browser history.
5061 */
5162 router . update ( {
52- routeTree ,
63+ basepath : getBasePath ( auth ) ,
5364 context : { appProps : props } ,
5465 stringifySearch : encodeV2 ,
5566 history : props . enableHashedRouting ? createHashHistory ( ) : createBrowserHistory ( ) ,
5667 parseSearch : ( searchString ) => {
5768 if ( ! props . enableHashedRouting ) {
5869 return decodeV2 ( searchString )
5970 }
60-
6171 /*
6272 * In case of hashed routing Tanstack router returns URL search params of the entire URL rather than just from the hashed part.
6373 * We'll have to extract the query part from the hash because otherwise in embedded mode the app will be taking search params from the shell app as well.
@@ -75,6 +85,11 @@ const StyledShell = (props: AppProps) => {
7585 return decodeV2 ( searchStringFromHash )
7686 } ,
7787 } )
88+ return < RouterProvider router = { router } />
89+ }
90+
91+ const StyledShell = ( props : AppProps ) => {
92+ props = { ...props , currentHost : props . currentHost === "origin" ? window . location . origin : props . currentHost }
7893
7994 return (
8095 < AppShellProvider >
@@ -91,7 +106,7 @@ const StyledShell = (props: AppProps) => {
91106 < StoreProvider options = { props } >
92107 < MessagesProvider >
93108 < StrictMode >
94- < RouterProvider basepath = "/" router = { router } />
109+ < App { ... props } />
95110 </ StrictMode >
96111 </ MessagesProvider >
97112 </ StoreProvider >
0 commit comments