|
| 1 | +import React, { useEffect, useRef, useState } from 'react'; |
| 2 | +import { useHistory } from 'react-router-dom'; |
| 3 | +import styled from 'styled-components'; |
| 4 | +import { |
| 5 | + __federation_method_getRemote as getRemote, |
| 6 | + __federation_method_setRemote as setRemote, |
| 7 | + __federation_method_unwrapDefault as unwrapModule, |
| 8 | +} from 'virtual:__federation__'; |
| 9 | + |
| 10 | +import { ErrorComponent } from '@app/mfeframework/ErrorComponent'; |
| 11 | +import { MFEConfig } from '@app/mfeframework/mfeConfigLoader'; |
| 12 | +import { useIsThemeV2 } from '@app/useIsThemeV2'; |
| 13 | +import { useShowNavBarRedesign } from '@app/useShowNavBarRedesign'; |
| 14 | + |
| 15 | +const MFEConfigurableContainer = styled.div<{ isV2: boolean; $isShowNavBarRedesign?: boolean }>` |
| 16 | + background-color: ${(props) => (props.isV2 ? '#fff' : 'inherit')}; |
| 17 | + padding: 16px; |
| 18 | + ${(props) => |
| 19 | + props.$isShowNavBarRedesign && |
| 20 | + ` |
| 21 | + height: 100%; |
| 22 | + margin: 5px; |
| 23 | + overflow: auto; |
| 24 | + box-shadow: ${props.theme.styles['box-shadow-navbar-redesign']}; |
| 25 | + `} |
| 26 | + ${(props) => |
| 27 | + !props.$isShowNavBarRedesign && |
| 28 | + ` |
| 29 | + margin-right: ${props.isV2 ? '24px' : '0'}; |
| 30 | + margin-bottom: ${props.isV2 ? '24px' : '0'}; |
| 31 | + `} |
| 32 | + border-radius: ${(props) => { |
| 33 | + if (props.isV2 && props.$isShowNavBarRedesign) return props.theme.styles['border-radius-navbar-redesign']; |
| 34 | + return props.isV2 ? '8px' : '0'; |
| 35 | + }}; |
| 36 | +`; |
| 37 | + |
| 38 | +interface MountMFEParams { |
| 39 | + config: MFEConfig; |
| 40 | + containerElement: HTMLDivElement | null; |
| 41 | + onError: () => void; |
| 42 | + aliveRef: { current: boolean }; |
| 43 | +} |
| 44 | + |
| 45 | +async function mountMFE({ |
| 46 | + config, |
| 47 | + containerElement, |
| 48 | + onError, |
| 49 | + aliveRef, |
| 50 | +}: MountMFEParams): Promise<(() => void) | undefined> { |
| 51 | + const { module, remoteEntry } = config; |
| 52 | + const mountStart = performance.now(); |
| 53 | + |
| 54 | + if (import.meta.env.DEV) { |
| 55 | + console.log('MFE id: ', config.id, ' Mounting start '); |
| 56 | + } |
| 57 | + try { |
| 58 | + if (import.meta.env.DEV) { |
| 59 | + console.log('[HOST] mount path: ', module); |
| 60 | + console.log('[HOST] attempting mount'); |
| 61 | + } |
| 62 | + |
| 63 | + // Parse module string, something like: "myapp/mount" |
| 64 | + const [remoteName, modulePath] = module.split('/'); |
| 65 | + const modulePathWithDot = `./${modulePath}`; // Convert "mount" to "./mount" |
| 66 | + |
| 67 | + if (import.meta.env.DEV) { |
| 68 | + console.log('[HOST] parsed remote name: ', remoteName); |
| 69 | + console.log('[HOST] parsed module path: ', modulePathWithDot); |
| 70 | + } |
| 71 | + |
| 72 | + // Configure the dynamic remote |
| 73 | + const remoteConfig = { |
| 74 | + url: remoteEntry, |
| 75 | + format: 'var' as const, |
| 76 | + from: 'webpack' as const, |
| 77 | + }; |
| 78 | + setRemote(remoteName, remoteConfig); |
| 79 | + |
| 80 | + // Create a timeout promise that rejects in a few seconds |
| 81 | + const timeoutPromise = new Promise((_, reject) => { |
| 82 | + setTimeout( |
| 83 | + () => reject(new Error(`Timeout loading from remote ${remoteName}, module: ${modulePathWithDot}`)), |
| 84 | + 5000, |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + // Race between getRemote and timeout |
| 89 | + const fetchStart = performance.now(); |
| 90 | + if (import.meta.env.DEV) { |
| 91 | + console.log('[HOST] Attempting to load remote module with config:', remoteConfig); |
| 92 | + } |
| 93 | + const remoteModule = await Promise.race([getRemote(remoteName, modulePathWithDot), timeoutPromise]); |
| 94 | + const fetchEnd = performance.now(); |
| 95 | + if (import.meta.env.DEV) { |
| 96 | + console.log(`latency for remote module fetch: ${config.id}`, fetchEnd - fetchStart, 'ms'); |
| 97 | + console.log('[HOST] Remote module loaded, unwrapping...'); |
| 98 | + } |
| 99 | + const unwrapStart = performance.now(); |
| 100 | + const mod = await unwrapModule(remoteModule); |
| 101 | + const unwrapEnd = performance.now(); |
| 102 | + if (import.meta.env.DEV) { |
| 103 | + console.log(`latency for module unwrap: ${config.id}`, unwrapEnd - unwrapStart, 'ms'); |
| 104 | + console.log('[HOST] imported mod: ', mod); |
| 105 | + console.log('[HOST] mod type: ', typeof mod); |
| 106 | + } |
| 107 | + |
| 108 | + const maybeFn = |
| 109 | + typeof mod === 'function' |
| 110 | + ? mod |
| 111 | + : ((mod as any)?.mount ?? |
| 112 | + (typeof (mod as any)?.default === 'function' ? (mod as any).default : (mod as any)?.default?.mount)); |
| 113 | + |
| 114 | + if (!aliveRef.current) { |
| 115 | + console.error('[HOST] import/mount has failed due to timeout.'); |
| 116 | + return undefined; |
| 117 | + } |
| 118 | + if (!config.flags.enabled) { |
| 119 | + console.warn( |
| 120 | + '[HOST] skipping remote module loading for<config.id> because planning not to show it, enabled=false', |
| 121 | + ); |
| 122 | + return undefined; |
| 123 | + } |
| 124 | + |
| 125 | + if (!containerElement) { |
| 126 | + console.warn('[HOST] ref is null (container div not in DOM'); |
| 127 | + return undefined; |
| 128 | + } |
| 129 | + |
| 130 | + if (typeof maybeFn !== 'function') { |
| 131 | + if (import.meta.env.DEV) { |
| 132 | + console.warn('MFE id: ', config.id, ' Mounting failed'); |
| 133 | + console.warn('[HOST] mount is not a function; got: ', maybeFn); |
| 134 | + } |
| 135 | + return undefined; |
| 136 | + } |
| 137 | + const mountFnStart = performance.now(); |
| 138 | + const cleanup = maybeFn(containerElement, {}); |
| 139 | + const mountFnEnd = performance.now(); |
| 140 | + if (import.meta.env.DEV) { |
| 141 | + console.log(`latency for mount function execution: ${config.id}`, mountFnEnd - mountFnStart, 'ms'); |
| 142 | + console.log('[HOST] mount called'); |
| 143 | + } |
| 144 | + const mountEnd = performance.now(); |
| 145 | + const latency = mountEnd - mountStart; |
| 146 | + if (import.meta.env.DEV) { |
| 147 | + console.log(`latency for successful MFE id: ${config.id}`, latency, 'ms'); |
| 148 | + } |
| 149 | + return cleanup; |
| 150 | + } catch (e) { |
| 151 | + if (import.meta.env.DEV) { |
| 152 | + console.log(`latency for unsuccessful MFE id: ${config.id}`, performance.now() - mountStart, 'ms'); |
| 153 | + console.error('[HOST] import/mount failed:', e); |
| 154 | + } |
| 155 | + if (aliveRef.current) { |
| 156 | + onError(); |
| 157 | + } |
| 158 | + return undefined; |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +export const MFEBaseConfigurablePage = ({ config }: { config: MFEConfig }) => { |
| 163 | + const isV2 = useIsThemeV2(); |
| 164 | + const isShowNavBarRedesign = useShowNavBarRedesign(); |
| 165 | + const box = useRef<HTMLDivElement>(null); |
| 166 | + const history = useHistory(); |
| 167 | + const [hasError, setHasError] = useState(false); |
| 168 | + const aliveRef = useRef(true); |
| 169 | + |
| 170 | + useEffect(() => { |
| 171 | + aliveRef.current = true; |
| 172 | + let cleanup: (() => void) | undefined; |
| 173 | + |
| 174 | + mountMFE({ |
| 175 | + config, |
| 176 | + containerElement: box.current, |
| 177 | + onError: () => setHasError(true), |
| 178 | + aliveRef, |
| 179 | + }).then((cleanupFn) => { |
| 180 | + cleanup = cleanupFn; |
| 181 | + }); |
| 182 | + |
| 183 | + return () => { |
| 184 | + aliveRef.current = false; |
| 185 | + if (cleanup) { |
| 186 | + if (import.meta.env.DEV) { |
| 187 | + console.log('[HOST] Executing cleanup method provided by mount'); |
| 188 | + } |
| 189 | + const cleanupStart = performance.now(); |
| 190 | + cleanup(); |
| 191 | + const cleanupEnd = performance.now(); |
| 192 | + if (import.meta.env.DEV) { |
| 193 | + console.log(`latency for cleanup execution: ${config.id}`, cleanupEnd - cleanupStart, 'ms'); |
| 194 | + } |
| 195 | + } |
| 196 | + }; |
| 197 | + }, [config, history]); |
| 198 | + |
| 199 | + if (hasError) { |
| 200 | + return <ErrorComponent isV2={isV2} message={`${config.label} is not available at this time`} />; |
| 201 | + } |
| 202 | + if (!config.flags.enabled) { |
| 203 | + return <ErrorComponent isV2={isV2} message={`${config.label} is disabled.`} />; |
| 204 | + } |
| 205 | + |
| 206 | + return ( |
| 207 | + <MFEConfigurableContainer |
| 208 | + isV2={isV2} |
| 209 | + $isShowNavBarRedesign={isShowNavBarRedesign} |
| 210 | + data-testid="mfe-configurable-container" |
| 211 | + > |
| 212 | + <div ref={box} style={{ minHeight: 480 }} /> |
| 213 | + </MFEConfigurableContainer> |
| 214 | + ); |
| 215 | +}; |
0 commit comments