-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathindex.tsx
More file actions
169 lines (143 loc) · 6.4 KB
/
index.tsx
File metadata and controls
169 lines (143 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* eslint-disable @typescript-eslint/no-use-before-define */
/* eslint-disable no-console */
import { useEffect, useMemo, useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import CssBaseline from '@mui/material/CssBaseline';
import { ThemeProvider, StyledEngineProvider } from '@mui/material/styles';
import { SdkConfigAccess, loginIfNecessary, getAvailablePortals } from '@pega/auth/lib/sdk-auth-manager';
import StoreContext from '@pega/react-sdk-components/lib/bridge/Context/StoreContext';
import createPConnectComponent from '@pega/react-sdk-components/lib/bridge/react_pconnect';
import { compareSdkPCoreVersions } from '@pega/react-sdk-components/lib/components/helpers/versionHelpers';
import { getSdkComponentMap } from '@pega/react-sdk-components/lib/bridge/helpers/sdk_component_map';
import localSdkComponentMap from '../../../sdk-local-component-map';
import { theme } from '../../theme';
import InvalidPortal from './InvalidPortal';
declare const myLoadPortal: any;
declare const myLoadDefaultPortal: any;
function useQuery() {
const { search } = useLocation();
return useMemo(() => new URLSearchParams(search), [search]);
}
function RootComponent(props) {
const PegaConnectObj = createPConnectComponent();
const thePConnObj = <PegaConnectObj {...props} />;
const contextValue = useMemo(() => {
return { store: PCore.getStore() };
}, []);
return <StoreContext.Provider value={contextValue}>{thePConnObj}</StoreContext.Provider>;
}
export default function FullPortal() {
const [portalSelectionScreen, setPortalSelectionScreen] = useState(false);
const [defaultPortalName, setDefaultPortalName] = useState('');
const [availablePortals, setAvailablePortals] = useState<string[]>([]);
const [rootComponentProps, setRootComponentProps] = useState<object | null>(null);
const navigate = useNavigate();
const query = useQuery();
if (query.get('portal')) {
const portalValue: any = query.get('portal');
sessionStorage.setItem('rsdk_portalName', portalValue);
}
if (query.get('locale')) {
const localeOverride: any = query.get('locale');
sessionStorage.setItem('rsdk_locale', localeOverride);
}
/**
* Callback from onPCoreReady that's called once the top-level render object
* is ready to be rendered
* @param inRenderObj the initial, top-level PConnect object to render
*/
function initialRender(inRenderObj) {
const { props, portalTarget, styleSheetTarget } = inRenderObj;
// set root components props
setRootComponentProps({ ...props, portalTarget, styleSheetTarget });
}
/**
* kick off the application's portal that we're trying to serve up
*/
function startPortal() {
// NOTE: When loadMashup is complete, this will be called.
PCore.onPCoreReady(renderObj => {
// Check that we're seeing the PCore version we expect
compareSdkPCoreVersions();
// Initialize the SdkComponentMap (local and pega-provided)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getSdkComponentMap(localSdkComponentMap).then((theComponentMap: any) => {
console.log(`SdkComponentMap initialized`);
// Don't call initialRender until SdkComponentMap is fully initialized
initialRender(renderObj);
});
});
// load the Portal and handle the onPCoreEntry response that establishes the
// top level Pega root element (likely a RootContainer)
const { appPortal: thePortal, excludePortals } = SdkConfigAccess.getSdkConfigServer();
const defaultPortal = PCore?.getEnvironmentInfo?.().getDefaultPortal?.();
const queryPortal = sessionStorage.getItem('rsdk_portalName');
// Note: myLoadPortal and myLoadDefaultPortal are set when bootstrapWithAuthHeader is invoked
if (queryPortal) {
myLoadPortal('pega-root', queryPortal, []);
} else if (thePortal) {
console.log(`Loading specified appPortal: ${thePortal}`);
myLoadPortal('pega-root', thePortal, []);
} else if (myLoadDefaultPortal && defaultPortal && !excludePortals.includes(defaultPortal)) {
console.log(`Loading default portal`);
myLoadDefaultPortal('pega-root', []);
} else {
console.log('Loading portal selection screen');
setPortalSelectionScreen(true);
setDefaultPortalName(defaultPortal);
// Getting current user's access group's available portals list other than excluded portals (relies on Traditional DX APIs)
getAvailablePortals().then(portals => {
setAvailablePortals(portals as string[]);
});
}
}
function loadSelectedPortal(portal) {
setPortalSelectionScreen(false);
myLoadPortal('app-root', portal, []); // this is defined in bootstrap shell that's been loaded already
}
function doRedirectDone() {
const redirectUrl: any = sessionStorage.getItem('url');
navigate(redirectUrl);
sessionStorage.removeItem('url');
const locale: any = sessionStorage.getItem('rsdk_locale') || undefined;
// appName and mainRedirect params have to be same as earlier invocation
loginIfNecessary({ appName: 'portal', mainRedirect: true, locale });
}
// One time (initialization)
useEffect(() => {
document.addEventListener('SdkConstellationReady', handleSdkConstellationReady);
const locale: any = sessionStorage.getItem('rsdk_locale') || undefined;
const isLoggedIn = sessionStorage.getItem('isLoggedIn');
const redirected = sessionStorage.getItem('redirected');
if (isLoggedIn !== 'true' && redirected !== 'true') {
sessionStorage.setItem('url', window.location.pathname);
navigate('/portal');
}
sessionStorage.setItem('redirected', 'true');
// Login if needed, doing an initial main window redirect
loginIfNecessary({
appName: 'portal',
mainRedirect: true,
redirectDoneCB: doRedirectDone,
locale
// semanticUrls: true //. enable this line for semantic urls
});
}, []);
const handleSdkConstellationReady = () => {
sessionStorage.setItem('isLoggedIn', 'true');
// start the portal
startPortal();
};
return portalSelectionScreen ? (
<InvalidPortal defaultPortal={defaultPortalName} portals={availablePortals} onSelect={loadSelectedPortal} />
) : (
<div id='pega-root'>
<StyledEngineProvider injectFirst>
<ThemeProvider theme={theme}>
<CssBaseline />
{rootComponentProps && <RootComponent {...rootComponentProps} />}
</ThemeProvider>
</StyledEngineProvider>
</div>
);
}