Skip to content

Commit

Permalink
Custom menu via /api/config with project links as defaults (jaegertra…
Browse files Browse the repository at this point in the history
…cing#78)

* Custom menu via /api/config with friendly default

* Move custom menu to right, remove new ticket links

Signed-off-by: vvvprabhakar <vvvprabhakar@gmail.com>
  • Loading branch information
tiffon committed Sep 26, 2017
1 parent ae6e1e6 commit b42a4e0
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 41 deletions.
2 changes: 2 additions & 0 deletions src/actions/jaeger-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import JaegerAPI from '../api/jaeger';
* async wrapper to get the api object in case we're in demo mode.
*/

export const fetchConfig = createAction('@JAEGER_API/FETCH_CONFIG', () => JaegerAPI.fetchConfig());

export const fetchTrace = createAction(
'@JAEGER_API/FETCH_TRACE',
id => JaegerAPI.fetchTrace(id),
Expand Down
3 changes: 3 additions & 0 deletions src/api/jaeger.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export const DEFAULT_DEPENDENCY_LOOKBACK = moment.duration(1, 'weeks').asMillise

const JaegerAPI = {
apiRoot: DEFAULT_API_ROOT,
fetchConfig() {
return getJSON(`${this.apiRoot}config`).catch(err => ({ error: err }));
},
fetchTrace(id) {
return getJSON(`${this.apiRoot}traces/${id}`);
},
Expand Down
10 changes: 7 additions & 3 deletions src/components/App/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ import { connect } from 'react-redux';
import type { Location } from 'react-router-dom';

import TopNav from './TopNav';
import type { Config } from '../../types/config';
import { trackPageView } from '../../utils/metrics';

import './Page.css';

type PageProps = {
location: Location,
children: React.Node,
config: { data: Config },
};

class Page extends React.Component<PageProps> {
Expand All @@ -52,11 +54,12 @@ class Page extends React.Component<PageProps> {
}

render() {
const { children } = this.props;
const { children, config } = this.props;
const menu = config && config.data && config.data.menu;
return (
<section className="jaeger-ui-page" id="jaeger-ui">
<Helmet title="Jaeger UI" />
<TopNav />
<TopNav menuConfig={menu} />
<div className="jaeger-ui--content">
{children}
</div>
Expand All @@ -66,8 +69,9 @@ class Page extends React.Component<PageProps> {
}

function mapStateToProps(state, ownProps) {
const { config } = state;
const { location } = state.routing;
return { ...ownProps, location };
return { ...ownProps, config, location };
}

export default connect(mapStateToProps)(Page);
12 changes: 10 additions & 2 deletions src/components/App/TopNav.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

.ui.menu.jaeger-ui--topnav {
border-radius: 0;
.TopNav {
/* !important is to override styles from semantic UI */
border-radius: 0 !important;
margin: 0 !important;
padding: 5px;
position: fixed;
width: 100%;
z-index: 1000;
}

.TopNav--DropdownItem {
display: block;
margin: -1em;
padding: 1em;
}
76 changes: 63 additions & 13 deletions src/components/App/TopNav.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @flow

// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
Expand All @@ -20,12 +22,45 @@

import React from 'react';
import { Link } from 'react-router-dom';
import { Dropdown, Menu } from 'semantic-ui-react';

import TraceIDSearchInput from './TraceIDSearchInput';
import type { ConfigMenuItem, ConfigMenuGroup } from '../../types/config';
import prefixUrl from '../../utils/prefix-url';

import './TopNav.css';

type TopNavProps = {
menuConfig: (ConfigMenuItem | ConfigMenuGroup)[],
};

function CustomNavItem({ label, url }: ConfigMenuItem) {
return (
<a href={url} className="header item" target="_blank">
{label}
</a>
);
}

function CustomNavDropdown({ label, items }: ConfigMenuGroup) {
return (
<Dropdown text={label} pointing className="link item">
<Dropdown.Menu>
{items.map(item => {
const { label: itemLabel, url } = item;
return (
<Dropdown.Item key={itemLabel}>
<a href={url} className="ui TopNav--DropdownItem" target="_blank">
{itemLabel}
</a>
</Dropdown.Item>
);
})}
</Dropdown.Menu>
</Dropdown>
);
}

const NAV_LINKS = [
{
key: 'dependencies',
Expand All @@ -39,23 +74,38 @@ const NAV_LINKS = [
},
];

export default function TopNav() {
export default function TopNav(props: TopNavProps) {
const { menuConfig } = props;
const menuItems = Array.isArray(menuConfig) ? menuConfig : [];
return (
<nav className="ui top inverted menu jaeger-ui--topnav">
<Menu inverted className="TopNav">
<Link to={prefixUrl('/')} className="header item">
{'Jaeger UI'}
Jaeger UI
</Link>

<div className="ui input">
<TraceIDSearchInput />
</div>
{NAV_LINKS.map(({ key, to, text }) =>
<Link key={key} to={to} className="item">
{text}
</Link>
)}
<div className="right menu">
<div className="ui input">
<TraceIDSearchInput />
</div>
{NAV_LINKS.map(({ key, to, text }) =>
<Link key={key} to={to} className="item">
{text}
</Link>
)}
{menuItems.map(item => {
if (item.items) {
return <CustomNavDropdown key={item.label} {...item} />;
}
return <CustomNavItem key={item.label} {...item} />;
})}
</div>
</nav>
</Menu>
);
}

TopNav.defaultProps = {
menuConfig: [],
};

// exported for tests
TopNav.CustomNavItem = CustomNavItem;
TopNav.CustomNavDropdown = CustomNavDropdown;
102 changes: 102 additions & 0 deletions src/components/App/TopNav.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import React from 'react';
import { shallow } from 'enzyme';
import { Link } from 'react-router-dom';

import TopNav from './TopNav';

describe('<TopNav>', () => {
const labelGitHub = 'GitHub';
const labelAbout = 'About Jaeger';
const dropdownItems = [
{
label: 'Docs',
url: 'http://jaeger.readthedocs.io/en/latest/',
},
{
label: 'Twitter',
url: 'https://twitter.com/JaegerTracing',
},
];

const defaultProps = {
menuConfig: [
{
label: labelGitHub,
url: 'https://github.com/uber/jaeger',
},
{
label: labelAbout,
items: dropdownItems,
},
],
};

let wrapper;

beforeEach(() => {
wrapper = shallow(<TopNav {...defaultProps} />);
});

describe('renders the default menu options', () => {
it('renders the "Jaeger UI" button', () => {
const items = wrapper.find(Link).findWhere(link => /Jaeger UI/.test(link.text()));
expect(items.length).toBe(1);
});

it('renders the "Search" button', () => {
const items = wrapper.find(Link).findWhere(link => /Search/.test(link.text()));
expect(items.length).toBe(1);
});

it('renders the "Dependencies" button', () => {
const items = wrapper.find(Link).findWhere(link => /Dependencies/.test(link.text()));
expect(items.length).toBe(1);
});
});

describe('renders the custom menu', () => {
it('renders the top-level item', () => {
const item = wrapper.find(TopNav.CustomNavItem);
expect(item.length).toBe(1);
expect(item.prop('label')).toBe(labelGitHub);
});

describe('renders the nested menu items', () => {
it('renders the <CustomNavDropdown> component', () => {
const item = wrapper.find(TopNav.CustomNavDropdown);
expect(item.length).toBe(1);
expect(item.prop('label')).toBe(labelAbout);
expect(item.prop('items')).toBe(dropdownItems);
});

it('the <CustomNavDropdown> renders the links', () => {
const dropdown = shallow(<TopNav.CustomNavDropdown label={labelAbout} items={dropdownItems} />);
const links = dropdown.find('a');
expect(links.length).toBe(2);
const linkTexts = links.map(node => node.text()).sort();
const expectTexts = dropdownItems.map(item => item.label).sort();
expect(expectTexts).toEqual(linkTexts);
});
});
});
});
30 changes: 8 additions & 22 deletions src/components/App/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import React, { Component } from 'react';
import createHistory from 'history/createBrowserHistory';
import PropTypes from 'prop-types';
import { Provider } from 'react-redux';
import { Route, Redirect, Switch } from 'react-router-dom';
import { ConnectedRouter } from 'react-router-redux';
Expand All @@ -32,39 +31,26 @@ import NotFound from './NotFound';
import { ConnectedDependencyGraphPage } from '../DependencyGraph';
import { ConnectedSearchTracePage } from '../SearchTracePage';
import { ConnectedTracePage } from '../TracePage';
import { fetchConfig } from '../../actions/jaeger-api';
import JaegerAPI, { DEFAULT_API_ROOT } from '../../api/jaeger';
import configureStore from '../../utils/configure-store';
import prefixUrl from '../../utils/prefix-url';

import './App.css';

const defaultHistory = createHistory();
const history = createHistory();

export default class JaegerUIApp extends Component {
static get propTypes() {
return {
history: PropTypes.object,
apiRoot: PropTypes.string,
};
}

static get defaultProps() {
return {
history: defaultHistory,
apiRoot: DEFAULT_API_ROOT,
};
}

componentDidMount() {
const { apiRoot } = this.props;
JaegerAPI.apiRoot = apiRoot;
constructor(props) {
super(props);
this.store = configureStore(history);
JaegerAPI.apiRoot = DEFAULT_API_ROOT;
this.store.dispatch(fetchConfig());
}

render() {
const { history } = this.props;
const store = configureStore(history);
return (
<Provider store={store}>
<Provider store={this.store}>
<ConnectedRouter history={history}>
<Page>
<Switch>
Expand Down
2 changes: 1 addition & 1 deletion src/components/TracePage/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ THE SOFTWARE.
padding: 0.5em 0.5em 0 0.5em;
position: fixed;
width: 100%;
z-index: 1000001;
z-index: 3;
}

.trace-timeline-section {
Expand Down
Loading

0 comments on commit b42a4e0

Please sign in to comment.