Skip to content

Commit 5315151

Browse files
committed
fix(greenhouse): add and read organization from pathname
1 parent d345458 commit 5315151

3 files changed

Lines changed: 37 additions & 10 deletions

File tree

apps/greenhouse/src/Shell.tsx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import React, { StrictMode } from "react"
6+
import React, { StrictMode, useEffect, useLayoutEffect } from "react"
77
import { createBrowserHistory, createHashHistory, createRouter, RouterProvider } from "@tanstack/react-router"
88
import { AppShellProvider } from "@cloudoperators/juno-ui-components"
99
import { MessagesProvider } from "@cloudoperators/juno-messages-provider"
1010
import { decodeV2, encodeV2 } from "@cloudoperators/juno-url-state-provider"
1111
import Auth from "./components/Auth"
1212
import styles from "./styles.css?inline"
1313
import StoreProvider from "./components/StoreProvider"
14-
import { AuthProvider } from "./components/AuthProvider"
14+
import { AuthProvider, useAuth } from "./components/AuthProvider"
1515
import { 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(/^(.+)\.dashboard\..+/)
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>

apps/greenhouse/src/components/AuthProvider.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const setOrganizationToUrl = (groups: any) => {
1111
if (orgString) {
1212
const name = orgString.split(":")[1]
1313
let url = new URL(window.location.href)
14-
url.searchParams.set("org", name)
14+
// set the org as the first path segment
15+
url.pathname = `/${name}`
1516
// @ts-expect-error TS(2345): Argument of type 'null' is not assignable to param... Remove this comment to see the full error message
1617
window.history.replaceState(null, null, url.href)
1718
}
@@ -54,8 +55,17 @@ function resolveMockAuth(value: any) {
5455

5556
const extractOrganizationName = () => {
5657
const currentUrl = new URL(window.location.href)
58+
59+
// Try to extract from subdomain
5760
let match = currentUrl.host.match(/^(.+)\.dashboard\..+/)
58-
return match ? match[1] : currentUrl.searchParams.get("org")
61+
if (match) return match[1]
62+
63+
// Try to extract from the first path segment
64+
const pathParts = currentUrl.pathname.split("/").filter(Boolean)
65+
if (pathParts.length > 0) return pathParts[0]
66+
67+
// Return null if no organization found
68+
return undefined
5969
}
6070

6171
const initializeDemoAuth = (

apps/greenhouse/src/components/Extension.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import React, { Suspense, useEffect, useRef } from "react"
7+
import { useRouter } from "@tanstack/react-router"
78
import * as supernova from "@cloudoperators/juno-app-supernova"
89
import * as doop from "@cloudoperators/juno-app-doop"
910
import * as heureka from "@cloudoperators/juno-app-heureka"
@@ -33,6 +34,7 @@ type ExtensionProps = {
3334
}
3435

3536
function Extension({ id, config, auth, appProps }: ExtensionProps) {
37+
const router = useRouter()
3638
const appContainerRef = useRef<HTMLDivElement>(null)
3739
const app = getApp(config.name)
3840

@@ -48,7 +50,7 @@ function Extension({ id, config, auth, appProps }: ExtensionProps) {
4850
? {
4951
embedded: true,
5052
token: auth?.JWT,
51-
basePath: `/${config.id}`,
53+
basePath: `${router.basepath}/${config.id}`,
5254
enableHashedRouting: appProps?.enableHashedRouting || false,
5355
}
5456
: { auth: auth }),

0 commit comments

Comments
 (0)