Skip to content

Commit 39530aa

Browse files
authored
Merge pull request #3349 from weaveworks/3348-on-route-change-hook
Add onRouteChange hook to Scope app
2 parents 69d1c6e + eb767ed commit 39530aa

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

  • client/app/scripts/components

client/app/scripts/components/app.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React from 'react';
33
import PropTypes from 'prop-types';
44
import classNames from 'classnames';
55
import { connect } from 'react-redux';
6-
import { debounce } from 'lodash';
6+
import { debounce, isEqual } from 'lodash';
77

88
import { ThemeProvider } from 'styled-components';
99
import theme from 'weaveworks-ui-components/lib/theme';
@@ -67,6 +67,7 @@ class App extends React.Component {
6767

6868
this.setViewportDimensions = this.setViewportDimensions.bind(this);
6969
this.handleResize = debounce(this.setViewportDimensions, VIEWPORT_RESIZE_DEBOUNCE_INTERVAL);
70+
this.handleRouteChange = debounce(props.onRouteChange, 50);
7071

7172
this.saveAppRef = this.saveAppRef.bind(this);
7273
this.onKeyPress = this.onKeyPress.bind(this);
@@ -105,6 +106,10 @@ class App extends React.Component {
105106
if (nextProps.disableStoreViewState !== this.props.disableStoreViewState) {
106107
this.props.dispatch(setStoreViewState(!nextProps.disableStoreViewState));
107108
}
109+
// Debounce-notify about the route change if the URL state changes its content.
110+
if (!isEqual(nextProps.urlState, this.props.urlState)) {
111+
this.handleRouteChange(nextProps.urlState);
112+
}
108113
}
109114

110115
onKeyUp(ev) {
@@ -241,7 +246,6 @@ class App extends React.Component {
241246
}
242247
}
243248

244-
245249
function mapStateToProps(state) {
246250
return {
247251
currentTopology: state.get('currentTopology'),
@@ -267,13 +271,15 @@ function mapStateToProps(state) {
267271
App.propTypes = {
268272
renderTimeTravel: PropTypes.func,
269273
renderNodeDetailsExtras: PropTypes.func,
274+
onRouteChange: PropTypes.func,
270275
monitor: PropTypes.bool,
271276
disableStoreViewState: PropTypes.bool,
272277
};
273278

274279
App.defaultProps = {
275280
renderTimeTravel: () => <TimeTravelWrapper />,
276281
renderNodeDetailsExtras: () => null,
282+
onRouteChange: () => null,
277283
monitor: false,
278284
disableStoreViewState: false,
279285
};

0 commit comments

Comments
 (0)