|
| 1 | +import React from 'react'; |
| 2 | +import ReactDOM from 'react-dom'; |
| 3 | +import lodash from 'lodash'; |
| 4 | + |
| 5 | +import LocalButton from './Button'; |
| 6 | +const RemoteButtonLazy = React.lazy(() => import('app2/Button')); |
| 7 | + |
| 8 | +// A function to generate a color from a string |
| 9 | +const getColorFromString = str => { |
| 10 | + // Prime numbers used for generating a hash |
| 11 | + let primes = [1, 2, 3, 5, 7, 11, 13, 17, 19, 23]; |
| 12 | + let hash = 0; |
| 13 | + |
| 14 | + // Generate a hash from the string |
| 15 | + for (let i = 0; i < str.length; i++) { |
| 16 | + hash += str.charCodeAt(i) * primes[i % primes.length]; |
| 17 | + } |
| 18 | + |
| 19 | + // Convert the hash to a color |
| 20 | + let color = '#'; |
| 21 | + for (let i = 0; i < 3; i++) { |
| 22 | + const value = (hash >> (i * 8)) & 0xff; |
| 23 | + color += ('00' + value.toString(16)).substr(-2); |
| 24 | + } |
| 25 | + |
| 26 | + return color; |
| 27 | +}; |
| 28 | + |
| 29 | +// The main App component |
| 30 | +const App = () => ( |
| 31 | + <div> |
| 32 | + <h1>Offline Remote</h1> |
| 33 | + <h2>Remotes currently in use</h2> |
| 34 | + {/* Display the names of the remotes loaded by the CustomPlugin */} |
| 35 | + {__FEDERATION__.__INSTANCES__.map(inst => ( |
| 36 | + <span |
| 37 | + style={{ |
| 38 | + padding: 10, |
| 39 | + color: '#fff', |
| 40 | + background: getColorFromString(inst.name.split().reverse().join('')), |
| 41 | + }} |
| 42 | + key={inst.name} |
| 43 | + > |
| 44 | + {inst.name} |
| 45 | + </span> |
| 46 | + ))} |
| 47 | + <p> |
| 48 | + Click The second button. This will cause the <i>pick-remote.ts</i> to load remoteEntry urls |
| 49 | + from a mock api call. |
| 50 | + </p> |
| 51 | + {/* LocalButton is a button component from the local app */} |
| 52 | + <LocalButton /> |
| 53 | + {/* RemoteButton is a button component loaded from a remote app */} |
| 54 | + <ErrorBoundary> |
| 55 | + <React.Suspense fallback="Loading Button"> |
| 56 | + <RemoteButtonLazy /> |
| 57 | + </React.Suspense> |
| 58 | + </ErrorBoundary> |
| 59 | + {/* The Reset button clears the 'button' item from localStorage */} |
| 60 | + <button |
| 61 | + onClick={() => { |
| 62 | + localStorage.clear('button'); |
| 63 | + window.location.reload(); |
| 64 | + }} |
| 65 | + > |
| 66 | + Reset{' '} |
| 67 | + </button> |
| 68 | + </div> |
| 69 | +); |
| 70 | + |
| 71 | +export default App; |
| 72 | + |
| 73 | +class ErrorBoundary extends React.Component { |
| 74 | + state = { |
| 75 | + hasError: false, |
| 76 | + }; |
| 77 | + |
| 78 | + componentDidCatch(error, errorInfo) { |
| 79 | + console.error(error, errorInfo); |
| 80 | + this.setState({ hasError: true }); |
| 81 | + } |
| 82 | + |
| 83 | + render() { |
| 84 | + const { children } = this.props; |
| 85 | + if (this.state.hasError) { |
| 86 | + return ( |
| 87 | + <div> |
| 88 | + <h1>Something went wrong</h1> |
| 89 | + <p>Try refreshing the page.</p> |
| 90 | + </div> |
| 91 | + ); |
| 92 | + } |
| 93 | + return children; |
| 94 | + } |
| 95 | +} |
0 commit comments