From ac41f959313f7fd754d4f8fc401be2843c94d110 Mon Sep 17 00:00:00 2001 From: Christopher Berge Hove Date: Mon, 30 Sep 2024 11:06:22 +0200 Subject: [PATCH] refactor: Update portal-config module to handle optional ref parameter Remove unused import and unnecessary code in portal-framework package Remove unused import and unnecessary code in portal-framework-config.tsx Add PortalSelector component for portal selection Update FusionFramework component to import ModulesInstance from fusion-framework-module Update PortalRouter component to include PortalSelector component Update portal-framework-config.tsx to remove console.log statement Update main.tsx to use localStorage for portalId if available Add PortalSelector component for portal selection --- .../core/src/modules/portal-config/module.ts | 10 +- .../portal-client/src/FusionFramework.tsx | 2 +- .../components/portal-router/PortalRouter.tsx | 2 + .../portal-client/src/lib/PortalSelector.tsx | 97 +++++++++++++++++++ .../src/lib/portal-framework-config.tsx | 3 +- client/packages/portal-client/src/main.tsx | 2 +- 6 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 client/packages/portal-client/src/lib/PortalSelector.tsx diff --git a/client/packages/core/src/modules/portal-config/module.ts b/client/packages/core/src/modules/portal-config/module.ts index a16f5885..00191404 100644 --- a/client/packages/core/src/modules/portal-config/module.ts +++ b/client/packages/core/src/modules/portal-config/module.ts @@ -9,7 +9,9 @@ export const module: PortalConfig = { configure: () => new PortalConfigConfigurator(), initialize: async (args): Promise => { const config = await (args.config as PortalConfigConfigurator).createConfigAsync(args, args.ref); - return args.ref?.portalConfig || new PortalConfigProvider(config); + const portalConfig = args.ref?.portalConfig || new PortalConfigProvider(config); + window['portalConfig'] = portalConfig; + return portalConfig; }, }; @@ -20,3 +22,9 @@ declare module '@equinor/fusion-framework-module' { portalServices: PortalConfig; } } + +declare global { + interface Window { + portalConfig: PortalConfigProvider; + } +} diff --git a/client/packages/portal-client/src/FusionFramework.tsx b/client/packages/portal-client/src/FusionFramework.tsx index 95102e48..cd899193 100644 --- a/client/packages/portal-client/src/FusionFramework.tsx +++ b/client/packages/portal-client/src/FusionFramework.tsx @@ -7,7 +7,7 @@ import { PortalProgressLoader } from '@equinor/portal-ui'; import { PortalProvider } from './components/portal-router/PortalRouter'; import { createPortalFramework } from './lib'; -import { ModulesInstance, AnyModule, ModulesInstanceType } from '@equinor/fusion-framework-module'; +import { ModulesInstance, AnyModule } from '@equinor/fusion-framework-module'; export const FusionFramework = (props: { portalConfig: PortalConfig; modules: ModulesInstance }) => { const Framework = createFrameworkProvider(createPortalFramework(props.portalConfig), props.modules); diff --git a/client/packages/portal-client/src/components/portal-router/PortalRouter.tsx b/client/packages/portal-client/src/components/portal-router/PortalRouter.tsx index 4a5c45af..960df347 100644 --- a/client/packages/portal-client/src/components/portal-router/PortalRouter.tsx +++ b/client/packages/portal-client/src/components/portal-router/PortalRouter.tsx @@ -10,6 +10,7 @@ import { AppPage } from '../../pages/AppPage/AppPage'; import { PortalRoutes, usePortalConfig } from '@portal/core'; import { PortalPage } from './PortalPage'; import PeopleResolverProvider from '@equinor/fusion-framework-react-components-people-provider'; +import { PortalSelector } from '../../lib/PortalSelector'; const getRoutes = (portalRoutes: PortalRoutes | undefined): RouteObject[] => { const pages = @@ -85,6 +86,7 @@ export function PortalProvider() { } return ( + {portalRoutes ? ( ) : ( diff --git a/client/packages/portal-client/src/lib/PortalSelector.tsx b/client/packages/portal-client/src/lib/PortalSelector.tsx new file mode 100644 index 00000000..676f322f --- /dev/null +++ b/client/packages/portal-client/src/lib/PortalSelector.tsx @@ -0,0 +1,97 @@ +import { Button, Card, Icon, List } from '@equinor/eds-core-react'; +import { settings } from '@equinor/eds-icons'; +import { useState } from 'react'; +import { styled } from 'styled-components'; + +import { useQuery } from 'react-query'; +import { useHttpClient } from '@equinor/fusion-framework-react-app/http'; + +const Styles = { + Selector: styled.div` + position: fixed; + bottom: 1rem; + left: 1rem; + width: 100%; + z-index: 2000; + `, + List: styled.div` + position: absolute; + left: 50px; + bottom: 0; + display: flex; + flex-direction: row; + gap: 1rem; + `, +}; + +export type Portal = { + name: string; + shortName: string; + id: string; + key: string; +}; + +const usePortals = () => { + const client = useHttpClient('portal-client'); + + return useQuery({ + queryKey: ['portals'], + queryFn: async () => await client.fetch('api/portals').then((res) => res.json()), + }); +}; + +export const PortalSelector = () => { + const [showSettings, setShowSettings] = useState(false); + const { data } = usePortals(); + + if (process.env.NODE_ENV === 'production') return null; + + window.setPortal = (portalId: string) => { + localStorage.setItem('portalId', portalId); + window.location.replace('/'); + window.location.reload(); + }; + + window.clearPortal = () => { + localStorage.removeItem('portalId'); + window.location.replace('/'); + window.location.reload(); + }; + window.togglePortalSelector = () => { + const showSettings = localStorage.getItem('showPortalSettings'); + if (!showSettings || showSettings === 'false') { + localStorage.setItem('showPortalSettings', 'true'); + } else { + localStorage.setItem('showPortalSettings', 'false'); + } + }; + + if (localStorage.getItem('showPortalSettings') === 'true') { + return ( + + {showSettings && ( + + {data?.map((portal) => ( + + ))} + + + )} + + + ); + } + return null; +}; + +declare global { + interface Window { + setPortal: (portalId: string) => void; + clearPortal: () => void; + togglePortalSelector: () => void; + } +} diff --git a/client/packages/portal-client/src/lib/portal-framework-config.tsx b/client/packages/portal-client/src/lib/portal-framework-config.tsx index fe5a0f8b..78edfdc5 100644 --- a/client/packages/portal-client/src/lib/portal-framework-config.tsx +++ b/client/packages/portal-client/src/lib/portal-framework-config.tsx @@ -174,10 +174,11 @@ export function createPortalFramework(portalConfig: PortalConfig) { }); } config.onInitialized<[NavigationModule, TelemetryModule, AppModule, PortalConfigModule]>(async (fusion) => { - console.log('hooooo', fusion); new FeatureLogger(fusion as any); + // Todo: should be moved to context module configurePortalContext(fusion.context); + // Todo: should be moved to context module fusion.context.currentContext$.pipe(skip(1)).subscribe((context) => { const { navigator } = fusion.navigation; diff --git a/client/packages/portal-client/src/main.tsx b/client/packages/portal-client/src/main.tsx index 0d2e1007..e74e6894 100644 --- a/client/packages/portal-client/src/main.tsx +++ b/client/packages/portal-client/src/main.tsx @@ -42,7 +42,7 @@ configurator.addConfig(configureHttpClient('portal-client', portalConfig.portalC enablePortalConfig(configurator, (builder) => { builder.setConfig({ - portalId: portalConfig.portalId, + portalId: localStorage.getItem('portalId') || portalConfig.portalId, portalEnv: portalConfig.fusionLegacyEnvIdentifier, }); });