Skip to content

Commit

Permalink
Add a react context that makes namespace available anywhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobgy committed Nov 26, 2019
1 parent 54006aa commit b195f6d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
23 changes: 17 additions & 6 deletions frontend/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { init as initKfClient } from './lib/KubeflowClient';
import { init as initKfClient, NamespaceContextProvider } from './lib/KubeflowClient';
import './CSSReset';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
Expand All @@ -23,6 +23,7 @@ import Router from './components/Router';
import { cssRule } from 'typestyle';
import { theme, fonts } from './Css';
import { HashRouter } from 'react-router-dom';
import { KFP_FLAGS, Deployments } from './lib/Flags';

// TODO: license headers

Expand All @@ -39,10 +40,20 @@ cssRule('html, body, #root', {
});

ReactDOM.render(
<MuiThemeProvider theme={theme}>
<HashRouter>
<Router />
</HashRouter>
</MuiThemeProvider>,
KFP_FLAGS.DEPLOYMENT === Deployments.KUBEFLOW ? (
<MuiThemeProvider theme={theme}>
<NamespaceContextProvider>
<HashRouter>
<Router />
</HashRouter>
</NamespaceContextProvider>
</MuiThemeProvider>
) : (
<MuiThemeProvider theme={theme}>
<HashRouter>
<Router />
</HashRouter>
</MuiThemeProvider>
),
document.getElementById('root'),
);
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
import { KFP_FLAGS, Deployments } from './Flags';
import { logger } from './Utils';

Expand All @@ -10,6 +11,12 @@ declare global {
}
}

let namespace: string | undefined;
let registeredHandler: undefined | ((namespace: string) => void);
function onNamespaceChanged(handler: (namespace: string) => void) {
registeredHandler = handler;
}

export function init(): void {
if (KFP_FLAGS.DEPLOYMENT !== Deployments.KUBEFLOW) {
return;
Expand All @@ -21,12 +28,28 @@ export function init(): void {
window.centraldashboard.CentralDashboardEventHandler.init((cdeh: any) => {
// Binds a callback that gets invoked anytime the Dashboard's
// namespace is changed
cdeh.onNamespaceSelected = (namespace: string) => {
// tslint:disable-next-line:no-console
console.log('Namespace changed to', namespace);
cdeh.onNamespaceSelected = (newNamespace: string) => {
namespace = newNamespace;
if (registeredHandler) {
registeredHandler(namespace);
}
};
});
} catch (err) {
logger.error('Failed to initialize central dashboard client', err);
}
}

const NamespaceContext = React.createContext<string | undefined>(undefined);
export const NamespaceContextConsumer = NamespaceContext.Consumer;
export class NamespaceContextProvider extends React.Component {
state = {
namespace,
};
componentDidMount() {
onNamespaceChanged(ns => this.setState({ namespace: ns }));
}
render() {
return <NamespaceContext.Provider value={this.state.namespace} {...this.props} />;
}
}
2 changes: 1 addition & 1 deletion frontend/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
"jsx-no-bind": false,
"jsx-no-lambda": false,
"max-classes-per-file": false,
"member-access": false,
"ordered-imports": false,
"semicolon": [true, "always", "ignore-bound-class-methods"],
"typedef": [true, "call-signature"],
"no-unused-variable": true,
"variable-name": [
true,
Expand Down

0 comments on commit b195f6d

Please sign in to comment.