|
3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
4 | 4 | */ |
5 | 5 |
|
6 | | -import React, { useLayoutEffect } from "react" |
| 6 | +import React, { StrictMode, useLayoutEffect } from "react" |
7 | 7 |
|
8 | 8 | import { AppShellProvider, ContentHeading } from "@cloudoperators/juno-ui-components" |
9 | | -import AppContent from "./components/AppContent" |
| 9 | +import { RouterProvider, createBrowserHistory, createHashHistory, createRouter } from "@tanstack/react-router" |
| 10 | +import { decodeV2, encodeV2 } from "@cloudoperators/juno-url-state-provider" |
10 | 11 | import styles from "./styles.css?inline" |
11 | 12 | import { MessagesProvider } from "@cloudoperators/juno-messages-provider" |
12 | 13 | import StoreProvider from "./components/StoreProvider" |
13 | 14 | import AsyncWorker from "./components/AsyncWorker" |
14 | 15 | import { AppShell } from "@cloudoperators/juno-ui-components" |
15 | 16 | import { QueryClientProvider, QueryClient } from "@tanstack/react-query" |
16 | 17 | import { useGlobalsActions } from "./components/StoreProvider" |
| 18 | +import { routeTree } from "./routeTree.gen" |
17 | 19 |
|
18 | | -const App = (props = {}) => { |
| 20 | +// Create a new router instance |
| 21 | +const router = createRouter({ |
| 22 | + routeTree, |
| 23 | + context: { |
| 24 | + appProps: undefined!, |
| 25 | + queryClient: undefined!, |
| 26 | + }, |
| 27 | +}) |
| 28 | + |
| 29 | +// Register the router instance for type safety |
| 30 | +declare module "@tanstack/react-router" { |
| 31 | + interface Register { |
| 32 | + router: typeof router |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +export type AppProps = { |
| 37 | + id?: string |
| 38 | + theme?: "theme-dark" | "theme-light" |
| 39 | + apiEndpoint?: string |
| 40 | + embedded?: boolean |
| 41 | + basePath?: string |
| 42 | + enableHashedRouting?: boolean |
| 43 | + showDebugSeverities?: boolean |
| 44 | +} |
| 45 | + |
| 46 | +const App = (props: AppProps = {}) => { |
19 | 47 | // @ts-expect-error TS(2339) FIXME: Property 'setEndpoint' does not exist on type '{}'. |
20 | 48 | const { setEndpoint } = useGlobalsActions() |
21 | 49 |
|
22 | 50 | const queryClient = new QueryClient({ |
23 | 51 | defaultOptions: { |
24 | 52 | queries: { |
25 | 53 | meta: { |
26 | | - // @ts-expect-error TS(2339) FIXME: Property 'apiEndpoint' does not exist on type '{}'... Remove this comment to see the full error message |
27 | 54 | endpoint: props.apiEndpoint, |
28 | 55 | }, |
29 | 56 | }, |
30 | 57 | }, |
31 | 58 | }) |
32 | 59 |
|
| 60 | + /* |
| 61 | + * Dynamically change the type of history on the router |
| 62 | + * based on the enableHashedRouting prop. This ensures that |
| 63 | + * the correct history type is used when A Shell app does not |
| 64 | + * want the app to use browser history. |
| 65 | + */ |
| 66 | + router.update({ |
| 67 | + routeTree, |
| 68 | + context: { appProps: props, queryClient }, |
| 69 | + history: props.enableHashedRouting ? createHashHistory() : createBrowserHistory(), |
| 70 | + stringifySearch: encodeV2, |
| 71 | + parseSearch: (searchString) => { |
| 72 | + if (!props.enableHashedRouting) { |
| 73 | + return decodeV2(searchString) |
| 74 | + } |
| 75 | + |
| 76 | + /* |
| 77 | + * In case of hashed routing Tanstack router returns URL search params of the entire URL rather than just from the hashed part. |
| 78 | + * 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. |
| 79 | + * Sanitize the search string by extracting the substring between the first '?' and the next '?' (if any), keeping the first '?'. |
| 80 | + * https://github.com/TanStack/router/issues/4370 |
| 81 | + * http://localhost:3000/?preHashParam=prehashtest#/services?postHashParam1=test1?preHashParam=prehashtest |
| 82 | + * searchString = "?postHashParam1=test1?preHashParam=prehashtest" |
| 83 | + * searchStringFromHash = "?postHashParam1=test1" |
| 84 | + */ |
| 85 | + const postHashParams = searchString.indexOf("?") |
| 86 | + if (postHashParams === -1) return {} // If no query part is found, return an empty object |
| 87 | + const preHashParams = searchString.indexOf("?", postHashParams + 1) |
| 88 | + const searchStringFromHash = searchString.slice(postHashParams, preHashParams === -1 ? undefined : preHashParams) |
| 89 | + |
| 90 | + return decodeV2(searchStringFromHash) |
| 91 | + }, |
| 92 | + }) |
| 93 | + |
33 | 94 | // on load application save the props to be used in oder components |
34 | 95 | useLayoutEffect(() => { |
35 | 96 | setEndpoint(window.location.origin) |
36 | 97 | }, []) |
37 | 98 |
|
38 | 99 | return ( |
39 | 100 | <MessagesProvider> |
40 | | - {/* @ts-expect-error TS(2339): Property 'embedded' does not exist on type '{}'. // @ts-expect-error TS(2339) FIXME: */} |
41 | 101 | <AppShell pageHeader={`Doop`} embedded={props.embedded === true}> |
42 | 102 | <ContentHeading |
43 | 103 | // @ts-expect-error TS(2339) FIXME: Property 'displayName' does not exist on type '{}'... Remove this comment to see the full error message |
44 | 104 | heading={`Decentralized Observer of Policies ${props.displayName ? ` - ${props.displayName}` : ""}`} |
45 | 105 | /> |
46 | | - {/* @ts-expect-error TS(2339): Property 'id' does not exist on type '{}'. // @ts-expect-error TS(2339) FIXME: */} |
47 | 106 | <AsyncWorker consumerId={props.id || "doop"} /> |
48 | 107 | <QueryClientProvider client={queryClient}> |
49 | | - {/* @ts-expect-error TS(2339): Property 'id' does not exist on type '{}'. // @ts-expect-error TS(2339) FIXME: */} |
50 | | - <AppContent id={props?.id} showDebugSeverities={props.showDebugSeverities} /> |
| 108 | + <StrictMode> |
| 109 | + <RouterProvider basepath={props.basePath || "/"} router={router} /> |
| 110 | + </StrictMode> |
51 | 111 | </QueryClientProvider> |
52 | 112 | </AppShell> |
53 | 113 | </MessagesProvider> |
|
0 commit comments