|
1 | | -import { isBrowserWindow } from '@atomic-reactor/reactium-sdk-core'; |
| 1 | +import { |
| 2 | + isBrowserWindow, |
| 3 | + useHookComponent, |
| 4 | +} from '@atomic-reactor/reactium-sdk-core'; |
2 | 5 | import React, { Suspense, lazy } from 'react'; |
3 | 6 | import manifestLoader from 'manifest'; |
4 | 7 |
|
5 | | -export default (elms = []) => { |
6 | | - let cmps = {}; |
7 | | - if (isBrowserWindow()) { |
8 | | - const contexts = manifestLoader.contexts; |
9 | | - |
10 | | - // Traverse the Array of bindable elements and require the components for them |
11 | | - elms.forEach(({ type, path }) => { |
12 | | - let req; |
13 | | - |
14 | | - // The path to the component |
15 | | - path = !path ? type : path; |
16 | | - Object.entries(contexts).forEach(([name, context]) => { |
17 | | - [ |
18 | | - `./${path}/index.js`, |
19 | | - `./${path}/index.jsx`, |
20 | | - `./${path}.js`, |
21 | | - `./${path}.jsx`, |
22 | | - ].forEach(attempt => { |
23 | | - // Exit if the component has already been defined |
24 | | - if (cmps[type]) { |
25 | | - return; |
26 | | - } |
27 | | - |
28 | | - const found = context.keys().find(key => key === attempt); |
29 | | - if (found) { |
30 | | - req = context(attempt); |
31 | | - } |
32 | | - |
33 | | - if (req) { |
34 | | - let Component; |
35 | | - if ('default' in req) { |
36 | | - // sync context |
37 | | - Component = req.default; |
38 | | - } else { |
39 | | - let Fallback = () => ( |
40 | | - <div className='get-components-loading' /> |
41 | | - ); |
42 | | - |
43 | | - try { |
44 | | - Fallback = require('components/Fallback') |
45 | | - .default; |
46 | | - } catch (err) { |
47 | | - console.log(err); |
48 | | - // left intentionally blank |
49 | | - } |
50 | | - |
51 | | - // async context |
52 | | - const Found = lazy(() => req); |
53 | | - Component = () => ( |
54 | | - <Suspense fallback={<Fallback />}> |
55 | | - <Found /> |
56 | | - </Suspense> |
57 | | - ); |
58 | | - } |
59 | | - |
60 | | - cmps[type] = Component; |
61 | | - } |
62 | | - }); |
63 | | - }); |
64 | | - }); |
65 | | - } |
66 | | - |
67 | | - // SSR and not found component cases |
68 | | - elms.forEach(({ type, path }) => { |
69 | | - if (!cmps[type]) { |
70 | | - cmps[type] = () => null; |
71 | | - } |
72 | | - }); |
73 | | - |
74 | | - // Output the Components Object |
75 | | - return cmps; |
| 8 | +const hookableComponent = name => props => { |
| 9 | + const Component = useHookComponent(name); |
| 10 | + return <Component {...props} />; |
76 | 11 | }; |
| 12 | + |
| 13 | +export default (elms = []) => |
| 14 | + elms.reduce((cmps, { type, path }) => { |
| 15 | + if (path) console.warn('path no longer supported in getComponents'); |
| 16 | + cmps[type] = hookableComponent(type); |
| 17 | + return cmps; |
| 18 | + }, {}); |
0 commit comments