Skip to content

Commit

Permalink
refactor: Update portal-config module to handle optional ref parameter
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Noggling committed Sep 30, 2024
1 parent c2b6ae6 commit ac41f95
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 4 deletions.
10 changes: 9 additions & 1 deletion client/packages/core/src/modules/portal-config/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export const module: PortalConfig = {
configure: () => new PortalConfigConfigurator(),
initialize: async (args): Promise<IPortalConfigProvider> => {
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;
},
};

Expand All @@ -20,3 +22,9 @@ declare module '@equinor/fusion-framework-module' {
portalServices: PortalConfig;
}
}

declare global {
interface Window {
portalConfig: PortalConfigProvider;
}
}
2 changes: 1 addition & 1 deletion client/packages/portal-client/src/FusionFramework.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnyModule[]> }) => {
const Framework = createFrameworkProvider(createPortalFramework(props.portalConfig), props.modules);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -85,6 +86,7 @@ export function PortalProvider() {
}
return (
<PeopleResolverProvider>
<PortalSelector />
{portalRoutes ? (
<PortalRouter routes={getRoutes(portalRoutes)} />
) : (
Expand Down
97 changes: 97 additions & 0 deletions client/packages/portal-client/src/lib/PortalSelector.tsx
Original file line number Diff line number Diff line change
@@ -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<Portal[]>({
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 (
<Styles.Selector>
{showSettings && (
<Styles.List>
{data?.map((portal) => (
<Button key={portal.id} onClick={() => window.setPortal(portal.id)}>
{portal.name}
</Button>
))}
<Button onClick={() => window.clearPortal()}>Clear Portal</Button>
</Styles.List>
)}
<Button variant="ghost_icon" onClick={() => setShowSettings((s) => !s)}>
<Icon data={settings}></Icon>
</Button>
</Styles.Selector>
);
}
return null;
};

declare global {
interface Window {
setPortal: (portalId: string) => void;
clearPortal: () => void;
togglePortalSelector: () => void;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion client/packages/portal-client/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
Expand Down

0 comments on commit ac41f95

Please sign in to comment.