|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and Juno contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { useEffect, useRef, useState } from "react" |
| 7 | +import { useRouter } from "@tanstack/react-router" |
| 8 | +import { AppProps } from "../Shell" |
| 9 | +import type { AuthStore } from "@cloudoperators/greenhouse-auth-provider" |
| 10 | +import type { PluginModule } from "@cloudoperators/juno-app-supernova" |
| 11 | + |
| 12 | +// Cache loaded modules at the module level (persists across component mounts) |
| 13 | +const moduleCache = new Map<string, PluginModule>() |
| 14 | + |
| 15 | +const getApp = async (appName: string): Promise<PluginModule | null> => { |
| 16 | + // Return cached module immediately if available |
| 17 | + if (moduleCache.has(appName)) { |
| 18 | + return moduleCache.get(appName)! |
| 19 | + } |
| 20 | + |
| 21 | + // Load the module |
| 22 | + let module: PluginModule | null = null |
| 23 | + switch (appName) { |
| 24 | + case "supernova": |
| 25 | + module = await import("@cloudoperators/juno-app-supernova") |
| 26 | + break |
| 27 | + case "doop": |
| 28 | + module = await import("@cloudoperators/juno-app-doop") |
| 29 | + break |
| 30 | + case "heureka": |
| 31 | + module = await import("@cloudoperators/juno-app-heureka") |
| 32 | + break |
| 33 | + } |
| 34 | + |
| 35 | + // Cache it for next time |
| 36 | + if (module) { |
| 37 | + moduleCache.set(appName, module) |
| 38 | + } |
| 39 | + |
| 40 | + return module |
| 41 | +} |
| 42 | + |
| 43 | +type UsePluginLoaderParams = { |
| 44 | + pluginName: string |
| 45 | + config: any |
| 46 | + appProps: AppProps |
| 47 | + pluginAuth: AuthStore |
| 48 | +} |
| 49 | + |
| 50 | +type UsePluginLoaderResult = { |
| 51 | + isLoading: boolean |
| 52 | + containerRef: React.RefObject<HTMLDivElement | null> |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Custom hook to handle plugin loading and mounting |
| 57 | + * Loads plugins dynamically with caching and handles mount/unmount lifecycle |
| 58 | + */ |
| 59 | +export function usePluginLoader({ |
| 60 | + pluginName, |
| 61 | + config, |
| 62 | + appProps, |
| 63 | + pluginAuth, |
| 64 | +}: UsePluginLoaderParams): UsePluginLoaderResult { |
| 65 | + const router = useRouter() |
| 66 | + const containerRef = useRef<HTMLDivElement>(null) |
| 67 | + |
| 68 | + // Check if module is already cached - if so, start with it loaded! |
| 69 | + const cachedModule = moduleCache.get(pluginName) |
| 70 | + const [app, setApp] = useState<PluginModule | null>(cachedModule || null) |
| 71 | + const [isLoading, setIsLoading] = useState(!cachedModule) // Only show loading if not cached |
| 72 | + |
| 73 | + // Load the plugin module dynamically (only if not already loaded) |
| 74 | + useEffect(() => { |
| 75 | + if (cachedModule) { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + let cancelled = false |
| 80 | + |
| 81 | + const loadApp = async () => { |
| 82 | + setIsLoading(true) |
| 83 | + try { |
| 84 | + const appModule = await getApp(pluginName) |
| 85 | + if (!cancelled) { |
| 86 | + setApp(appModule) |
| 87 | + setIsLoading(false) |
| 88 | + } |
| 89 | + } catch (error) { |
| 90 | + if (!cancelled) { |
| 91 | + setIsLoading(false) |
| 92 | + } |
| 93 | + throw error |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + loadApp() |
| 98 | + |
| 99 | + return () => { |
| 100 | + cancelled = true |
| 101 | + } |
| 102 | + }, [pluginName, cachedModule]) |
| 103 | + |
| 104 | + // Mount the app once it's loaded |
| 105 | + useEffect(() => { |
| 106 | + if (!app || !containerRef.current) { |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + app.mount(containerRef.current, { |
| 111 | + props: { |
| 112 | + ...config.props, |
| 113 | + embedded: true, |
| 114 | + basePath: `${router.basepath === "/" ? "" : router.basepath}/${config.id}`, |
| 115 | + enableHashedRouting: appProps?.enableHashedRouting || false, |
| 116 | + auth: pluginAuth, |
| 117 | + }, |
| 118 | + }) |
| 119 | + |
| 120 | + return () => { |
| 121 | + app.unmount() |
| 122 | + } |
| 123 | + }, [app, config, router, pluginAuth, appProps]) |
| 124 | + |
| 125 | + return { isLoading, containerRef } |
| 126 | +} |
0 commit comments