-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathApp.js
95 lines (85 loc) · 2.32 KB
/
App.js
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
import React from 'react';
import ReactDOM from 'react-dom';
import lodash from 'lodash';
import LocalButton from './Button';
const RemoteButtonLazy = React.lazy(() => import('app2/Button'));
// A function to generate a color from a string
const getColorFromString = str => {
// Prime numbers used for generating a hash
let primes = [1, 2, 3, 5, 7, 11, 13, 17, 19, 23];
let hash = 0;
// Generate a hash from the string
for (let i = 0; i < str.length; i++) {
hash += str.charCodeAt(i) * primes[i % primes.length];
}
// Convert the hash to a color
let color = '#';
for (let i = 0; i < 3; i++) {
const value = (hash >> (i * 8)) & 0xff;
color += ('00' + value.toString(16)).substr(-2);
}
return color;
};
// The main App component
const App = () => (
<div>
<h1>Offline Remote</h1>
<h2>Remotes currently in use</h2>
{/* Display the names of the remotes loaded by the CustomPlugin */}
{__FEDERATION__.__INSTANCES__.map(inst => (
<span
style={{
padding: 10,
color: '#fff',
background: getColorFromString(inst.name.split().reverse().join('')),
}}
key={inst.name}
>
{inst.name}
</span>
))}
<p>
Click The second button. This will cause the <i>pick-remote.ts</i> to load remoteEntry urls
from a mock api call.
</p>
{/* LocalButton is a button component from the local app */}
<LocalButton />
{/* RemoteButton is a button component loaded from a remote app */}
<ErrorBoundary>
<React.Suspense fallback="Loading Button">
<RemoteButtonLazy />
</React.Suspense>
</ErrorBoundary>
{/* The Reset button clears the 'button' item from localStorage */}
<button
onClick={() => {
localStorage.clear('button');
window.location.reload();
}}
>
Reset{' '}
</button>
</div>
);
export default App;
class ErrorBoundary extends React.Component {
state = {
hasError: false,
};
componentDidCatch(error, errorInfo) {
console.error(error, errorInfo);
this.setState({ hasError: true });
}
render() {
const { children } = this.props;
if (this.state.hasError) {
return (
<div>
<h1>Something went wrong</h1>
<p>Try refreshing the page.</p>
</div>
);
}
return children;
}
}