Skip to content

Commit 75dacd6

Browse files
committed
Add getModule method to core.
1 parent 138eb84 commit 75dacd6

File tree

6 files changed

+61
-18
lines changed

6 files changed

+61
-18
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
describe('UseModule loading callback', () => {
2+
it('should display SDK inbox text', () => {
3+
cy.visit('http://localhost:8123/use-module');
4+
cy.contains('SDK Inbox').should('exist');
5+
});
6+
});

packages/core/src/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,30 @@ export const preloadModule = async (scope: string, module: string, processor?: (
176176
return setPendingLoading(scope, module, Promise.resolve(modulePromise));
177177
};
178178

179+
export const getModule = async <T = any, P = any>(scope: string, module: string, importName = 'default'): Promise<T> => {
180+
const scalprum = getScalprum();
181+
const { cachedModule } = getCachedModule(scope, module);
182+
let Module: ExposedScalprumModule<T, P>;
183+
const manifestLocation = getAppData(scope)?.manifestLocation;
184+
if (!manifestLocation) {
185+
throw new Error(`Could not get module. Manifest location not found for scope ${scope}.`);
186+
}
187+
if (!cachedModule) {
188+
try {
189+
await processManifest(manifestLocation, scope, module);
190+
Module = await scalprum.pluginStore.getExposedModule(scope, module);
191+
} catch {
192+
throw new Error(
193+
`Module not initialized! Module "${module}" was not found in "${scope}" webpack scope. Make sure the remote container is loaded?`
194+
);
195+
}
196+
} else {
197+
Module = cachedModule;
198+
}
199+
200+
return Module[importName];
201+
};
202+
179203
export const initialize = <T extends Record<string, any> = Record<string, any>>({
180204
appsConfig,
181205
api,
Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
import { useEffect, useState, useCallback } from 'react';
2-
import { getCachedModule, ExposedScalprumModule, getScalprum } from '@scalprum/core';
2+
import { getModule } from '@scalprum/core';
33

4-
export function useModule(scope: string, module: string, defaultState: any): ExposedScalprumModule | undefined {
5-
const [data, setData] = useState<ExposedScalprumModule>(defaultState);
6-
const { pluginStore } = getScalprum();
4+
export function useModule<T = any, P = any>(scope: string, module: string, defaultState?: any, importName = 'default'): T {
5+
const [data, setData] = useState<T>(defaultState);
76
const fetchModule = useCallback(async () => {
8-
const { cachedModule } = getCachedModule(scope, module);
9-
let Module: ExposedScalprumModule;
10-
if (!cachedModule) {
11-
try {
12-
Module = await pluginStore.getExposedModule(scope, module);
13-
} catch {
14-
console.error(
15-
`Module not initialized! Module "${module}" was not found in "${scope}" webpack scope. Make sure the remote container is loaded?`
16-
);
17-
}
18-
} else {
19-
Module = cachedModule;
7+
try {
8+
const Module = await getModule<T, P>(scope, module, importName);
9+
setData(() => Module);
10+
} catch (error) {
11+
console.error(error);
2012
}
21-
setData(() => Module);
2213
}, [scope, module]);
2314

2415
useEffect(() => {
2516
fetchModule();
26-
}, [scope, module]);
17+
}, [scope, module, importName]);
2718

2819
return data;
2920
}

packages/test-app/src/entry.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import RootRoute from './routes/RootRoute';
88
import SDKModules from './routes/SDKModules';
99
import NotFoundError from './routes/NotFoundError';
1010
import { AppsConfig } from '@scalprum/core';
11+
import UseModuleLoading from './routes/UseModuleLoading';
1112

1213
const config: AppsConfig<{ assetsHost?: string }> = {
1314
notFound: {
@@ -62,6 +63,7 @@ const Entry = () => {
6263
<Route path="/not-found-error" element={<NotFoundError />} />
6364
<Route path="/legacy" element={<LegacyModules />} />
6465
<Route path="/sdk" element={<SDKModules />} />
66+
<Route path="/use-module" element={<UseModuleLoading />} />
6567
</Route>
6668
</Routes>
6769
</BrowserRouter>

packages/test-app/src/layouts/RootLayout.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ function RootLayout() {
3333
<NavLink variant="button" color="text.primary" to="/not-found-error" sx={{ my: 1, mx: 1.5 }}>
3434
Manifest not found
3535
</NavLink>
36+
<NavLink variant="button" color="text.primary" to="/use-module" sx={{ my: 1, mx: 1.5 }}>
37+
Use module hook
38+
</NavLink>
3639
</nav>
3740
</Toolbar>
3841
</AppBar>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
import { Grid } from '@mui/material';
3+
import { useModule } from '@scalprum/react-core';
4+
5+
const UseModuleLoading = () => {
6+
const Component = useModule<React.ComponentType>('sdk-plugin', './SDKComponent');
7+
8+
return (
9+
<Grid container spacing={4}>
10+
<Grid xs={12} md={6} item>
11+
{Component ? <Component /> : 'Loading...'}
12+
</Grid>
13+
</Grid>
14+
);
15+
};
16+
17+
export default UseModuleLoading;

0 commit comments

Comments
 (0)