import React from 'react';
import { connect } from 'react-redux';
import { searchableFieldsSelector } from '../selectors/search';
import { canvasMarginsSelector } from '../selectors/canvas';
import { hideHelp } from '../actions/app-actions';
const GENERAL_SHORTCUTS = [
{ key: 'esc', label: 'Close active panel' },
{ key: '/', label: 'Activate search field' },
{ key: '?', label: 'Toggle shortcut menu' },
{ key: 'g', label: 'Switch to Graph view' },
{ key: 't', label: 'Switch to Table view' },
{ key: 'r', label: 'Switch to Resources view' },
];
const CANVAS_METRIC_SHORTCUTS = [
{ key: '<', label: 'Select and pin previous metric' },
{ key: '>', label: 'Select and pin next metric' },
{ key: 'q', label: 'Unpin current metric' },
];
function renderShortcuts(cuts) {
return (
{cuts.map(({ key, label }) => (
))}
);
}
function renderShortcutPanel() {
return (
Shortcuts
General
{renderShortcuts(GENERAL_SHORTCUTS)}
Canvas Metrics
{renderShortcuts(CANVAS_METRIC_SHORTCUTS)}
);
}
const BASIC_SEARCHES = [
{ label: 'All fields for foo', term: 'foo' },
{
label: (
Any field matching
pid
{' '}
for the value 12345
),
term: 'pid: 12345'
},
];
const REGEX_SEARCHES = [
{
label: 'All fields for foo or bar',
term: 'foo|bar'
},
{
label: (
command
{' '}
field for foobar or foobaz
),
term: 'command: foo(bar|baz)'
},
];
const METRIC_SEARCHES = [
{
label: (
CPU
{' '}
greater than 4%
),
term: 'cpu > 4%'
},
{
label: (
Memory
{' '}
less than 10 megabytes
),
term: 'memory < 10mb'
},
];
function renderSearches(searches) {
return (
{searches.map(({ term, label }) => (
))}
);
}
function renderSearchPanel() {
return (
Search
Basics
{renderSearches(BASIC_SEARCHES)}
Regular expressions
{renderSearches(REGEX_SEARCHES)}
Metrics
{renderSearches(METRIC_SEARCHES)}
);
}
function renderFieldsPanel(currentTopologyName, searchableFields) {
const none = (
None
);
const currentTopology = (
{currentTopologyName}
);
return (
Fields and Metrics
Searchable fields and metrics in the
{' '}
currently selected
{' '}
{currentTopology}
{' '}
topology:
Fields
{searchableFields.get('fields').map(f => (
{f}
))}
{searchableFields.get('fields').size === 0 && none}
Metrics
{searchableFields.get('metrics').map(m => (
{m}
))}
{searchableFields.get('metrics').size === 0 && none}
);
}
function HelpPanel({
currentTopologyName, searchableFields, onClickClose, canvasMargins
}) {
return (
Help
{renderShortcutPanel()}
{renderSearchPanel()}
{renderFieldsPanel(currentTopologyName, searchableFields)}
);
}
function mapStateToProps(state) {
return {
canvasMargins: canvasMarginsSelector(state),
currentTopologyName: state.getIn(['currentTopology', 'fullName']),
searchableFields: searchableFieldsSelector(state)
};
}
export default connect(mapStateToProps, {
onClickClose: hideHelp
})(HelpPanel);