forked from openshift/console
/
app.jsx
215 lines (188 loc) · 6.37 KB
/
app.jsx
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import * as _ from 'lodash-es';
import * as React from 'react';
import { render } from 'react-dom';
import { Helmet } from 'react-helmet';
import { Provider } from 'react-redux';
import { Route, Router } from 'react-router-dom';
// AbortController is not supported in some older browser versions
import 'abort-controller/polyfill';
import store from '../redux';
import { detectFeatures } from '../actions/features';
import AppContents from './app-contents';
import { getBrandingDetails, Masthead } from './masthead';
import { ConsoleNotifier } from './console-notifier';
import { ConnectedNotificationDrawer } from './notification-drawer';
import { Navigation } from './nav';
import { history, Firehose } from './utils';
import * as UIActions from '../actions/ui';
import { fetchSwagger, getCachedResources, referenceForModel } from '../module/k8s';
import { receivedResources, watchAPIServices } from '../actions/k8s';
import { ClusterVersionModel } from '../models';
import '../vendor.scss';
import '../style.scss';
//PF4 Imports
import { Page } from '@patternfly/react-core';
const breakpointMD = 768;
const breakpointLG = 1600;
const cvResource = [
{
kind: referenceForModel(ClusterVersionModel),
namespaced: false,
name: 'version',
isList: false,
prop: 'cv',
optional: true,
},
];
// Edge lacks URLSearchParams
import 'url-search-params-polyfill';
class App extends React.PureComponent {
constructor(props) {
super(props);
this._onNavToggle = this._onNavToggle.bind(this);
this._onNavSelect = this._onNavSelect.bind(this);
this._isDesktop = this._isDesktop.bind(this);
this._onResize = this._onResize.bind(this);
this.previousDesktopState = this._isDesktop();
this.previousDrawerInlineState = this._isLargeLayout();
this.state = {
isNavOpen: this._isDesktop(),
isDrawerInline: this._isLargeLayout(),
};
}
UNSAFE_componentWillMount() {
window.addEventListener('resize', this._onResize);
}
componentWillUnmount() {
window.removeEventListener('resize', this._onResize);
}
componentDidUpdate(prevProps) {
const props = this.props;
// Prevent infinite loop in case React Router decides to destroy & recreate the component (changing key)
const oldLocation = _.omit(prevProps.location, ['key']);
const newLocation = _.omit(props.location, ['key']);
if (_.isEqual(newLocation, oldLocation) && _.isEqual(props.match, prevProps.match)) {
return;
}
// two way data binding :-/
const { pathname } = props.location;
store.dispatch(UIActions.setCurrentLocation(pathname));
}
_isLargeLayout() {
return window.innerWidth >= breakpointLG;
}
_isDesktop() {
return window.innerWidth >= breakpointMD;
}
_onNavToggle() {
// Some components, like svg charts, need to reflow when nav is toggled.
// Fire event after a short delay to allow nav animation to complete.
setTimeout(() => {
window.dispatchEvent(new Event('nav_toggle'));
}, 100);
this.setState((prevState) => {
return {
isNavOpen: !prevState.isNavOpen,
};
});
}
_onNavSelect() {
//close nav on mobile nav selects
if (!this._isDesktop()) {
this.setState({ isNavOpen: false });
}
}
_onResize() {
const isDesktop = this._isDesktop();
const isDrawerInline = this._isLargeLayout();
if (this.previousDesktopState !== isDesktop) {
this.setState({ isNavOpen: isDesktop });
this.previousDesktopState = isDesktop;
}
if (this.previousDrawerInlineState !== isDrawerInline) {
this.setState({ isDrawerInline });
this.previousDrawerInlineState = isDrawerInline;
}
}
render() {
const { isNavOpen, isDrawerInline } = this.state;
const { productName } = getBrandingDetails();
return (
<>
<Helmet titleTemplate={`%s · ${productName}`} defaultTitle={productName} />
<ConsoleNotifier location="BannerTop" />
<Page
header={<Masthead onNavToggle={this._onNavToggle} />}
sidebar={
<Navigation
isNavOpen={isNavOpen}
onNavSelect={this._onNavSelect}
onPerspectiveSelected={this._onNavSelect}
/>
}
>
<Firehose resources={cvResource}>
<ConnectedNotificationDrawer isDesktop={isDrawerInline}>
<AppContents />
</ConnectedNotificationDrawer>
</Firehose>
</Page>
<ConsoleNotifier location="BannerBottom" />
</>
);
}
}
const startDiscovery = () => store.dispatch(watchAPIServices());
// Load cached API resources from localStorage to speed up page load.
getCachedResources()
.then((resources) => {
if (resources) {
store.dispatch(receivedResources(resources));
}
// Still perform discovery to refresh the cache.
startDiscovery();
})
.catch(startDiscovery);
store.dispatch(detectFeatures());
// Global timer to ensure all <Timestamp> components update in sync
setInterval(() => store.dispatch(UIActions.updateTimestamps(Date.now())), 10000);
// Fetch swagger on load if it's stale.
fetchSwagger();
// Used by GUI tests to check for unhandled exceptions
window.windowError = false;
window.onerror = window.onunhandledrejection = () => (window.windowError = true);
if ('serviceWorker' in navigator) {
if (window.SERVER_FLAGS.loadTestFactor > 1) {
// eslint-disable-next-line import/no-unresolved
import('file-loader?name=load-test.sw.js!../load-test.sw.js')
.then(() => navigator.serviceWorker.register('/load-test.sw.js'))
.then(
() =>
new Promise((r) =>
navigator.serviceWorker.controller
? r()
: navigator.serviceWorker.addEventListener('controllerchange', () => r()),
),
)
.then(() =>
navigator.serviceWorker.controller.postMessage({
topic: 'setFactor',
value: window.SERVER_FLAGS.loadTestFactor,
}),
);
} else {
navigator.serviceWorker
.getRegistrations()
.then((registrations) => registrations.forEach((reg) => reg.unregister()))
// eslint-disable-next-line no-console
.catch((e) => console.warn('Error unregistering service workers', e));
}
}
render(
<Provider store={store}>
<Router history={history} basename={window.SERVER_FLAGS.basePath}>
<Route path="/" component={App} />
</Router>
</Provider>,
document.getElementById('app'),
);