|
| 1 | +import { useEffect, useId, useMemo, useRef, useState } from 'react'; |
| 2 | + |
| 3 | +import { Subscription } from 'rxjs'; |
| 4 | +import { last } from 'rxjs/operators'; |
| 5 | + |
| 6 | +import { useFramework } from '@equinor/fusion-framework-react'; |
| 7 | + |
| 8 | +import { useObservableState } from '@equinor/fusion-observable/react'; |
| 9 | + |
| 10 | +import { AppManifestError } from '@equinor/fusion-framework-module-app/errors.js'; |
| 11 | + |
| 12 | +import { ErrorViewer } from './components/ErrorViewer'; |
| 13 | +import type { AppModule } from '@equinor/fusion-framework-module-app'; |
| 14 | +import { EquinorLoader } from './components/EquinorLoader'; |
| 15 | + |
| 16 | +/** |
| 17 | + * React Functional Component for handling current application |
| 18 | + * |
| 19 | + * this component will set the current app by provided appKey. |
| 20 | + * when the appKey changes, this component will try to initialize the referred application |
| 21 | + * and render it. |
| 22 | + */ |
| 23 | +export const AppLoader = (props: { readonly appKey: string }) => { |
| 24 | + const { appKey } = props; |
| 25 | + const fusion = useFramework<[AppModule]>(); |
| 26 | + |
| 27 | + /** reference of application section/container */ |
| 28 | + const ref = useRef<HTMLElement>(null); |
| 29 | + const applicationContentId = useId(); |
| 30 | + |
| 31 | + const [loading, setLoading] = useState<boolean>(false); |
| 32 | + const [error, setError] = useState<Error | undefined>(); |
| 33 | + |
| 34 | + // TODO change to `useCurrentApp` |
| 35 | + /** observe and use the current selected application from framework */ |
| 36 | + const { value: currentApp } = useObservableState( |
| 37 | + useMemo(() => fusion.modules.app.current$, [fusion.modules.app]), |
| 38 | + ); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + /** when appKey property change, assign it to current */ |
| 42 | + fusion.modules.app.setCurrentApp(appKey); |
| 43 | + }, [appKey, fusion]); |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + /** flag that application is loading */ |
| 47 | + setLoading(true); |
| 48 | + |
| 49 | + /** clear previous errors */ |
| 50 | + setError(undefined); |
| 51 | + |
| 52 | + /** create a teardown of load */ |
| 53 | + const subscription = new Subscription(); |
| 54 | + |
| 55 | + /** make sure that initialize is canceled and disposed if current app changes */ |
| 56 | + subscription.add( |
| 57 | + currentApp |
| 58 | + ?.initialize() |
| 59 | + .pipe(last()) |
| 60 | + .subscribe({ |
| 61 | + next: ({ manifest, script, config }) => { |
| 62 | + /** generate basename for application */ |
| 63 | + const [basename] = window.location.pathname.match(/\/?apps\/[a-z|-]+(\/)?/g) ?? ['']; |
| 64 | + |
| 65 | + /** create a 'private' element for the application */ |
| 66 | + const el = document.createElement('div'); |
| 67 | + if (!ref.current) { |
| 68 | + throw Error('Missing application mounting point'); |
| 69 | + } |
| 70 | + |
| 71 | + ref.current.appendChild(el); |
| 72 | + |
| 73 | + /** extract render callback function from javascript module */ |
| 74 | + const render = script.renderApp ?? script.default; |
| 75 | + |
| 76 | + /** add application teardown to current render effect teardown */ |
| 77 | + subscription.add(render(el, { fusion, env: { basename, config, manifest } })); |
| 78 | + |
| 79 | + /** remove app element when application unmounts */ |
| 80 | + subscription.add(() => el.remove()); |
| 81 | + }, |
| 82 | + complete: () => { |
| 83 | + /** flag that application is no longer loading */ |
| 84 | + setLoading(false); |
| 85 | + }, |
| 86 | + error: (err) => { |
| 87 | + /** set error if initialization of application fails */ |
| 88 | + setError(err); |
| 89 | + }, |
| 90 | + }), |
| 91 | + ); |
| 92 | + |
| 93 | + /** teardown application when hook unmounts */ |
| 94 | + return () => subscription.unsubscribe(); |
| 95 | + }, [fusion, currentApp]); |
| 96 | + |
| 97 | + if (error) { |
| 98 | + if (error.cause instanceof AppManifestError) { |
| 99 | + return ( |
| 100 | + <div> |
| 101 | + <h2>🔥 Failed to load application manifest 🤬</h2> |
| 102 | + <h3>{error.cause.type}</h3> |
| 103 | + <ErrorViewer error={error} /> |
| 104 | + </div> |
| 105 | + ); |
| 106 | + } |
| 107 | + return ( |
| 108 | + <div> |
| 109 | + <h2>🔥 Failed to load application 🤬</h2> |
| 110 | + <ErrorViewer error={error} /> |
| 111 | + </div> |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + return ( |
| 116 | + <section id={applicationContentId} ref={ref} style={{ display: 'contents' }}> |
| 117 | + {loading && <EquinorLoader text="Loading Application" />} |
| 118 | + </section> |
| 119 | + ); |
| 120 | +}; |
| 121 | + |
| 122 | +export default AppLoader; |
0 commit comments