summaryrefslogtreecommitdiff
path: root/app/client/common/header.component.js
blob: 8450e8abc640ee9eb110cee0c460ecef3063f015 (plain)
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
import { h, Component } from 'preact'
import { bindActionCreators } from 'redux'
import { Link } from 'react-router-dom';
import { connect } from 'react-redux'

import * as systemActions from '../system/system.actions'

import modules from '../modules'

function NavLink(props){
  if (! props.to) {
    return <span>{props.children}</span>
  }
  let children = props.to === props.location.pathname ? <b>{props.children}</b> : props.children
  return (
    <span><Link to={props.to}>{children}</Link></span>
  )
}

function Header(props){
  const { site, app, fps, playing, actions, location, history } = props
  const tool_list = Object.keys(modules).map((name, i) => {
    const label = name.replace(/_/, " ")
    return <option value={name} key={i}>{label}</option>
  })
  const tool = modules[app.tool]
  const links = tool.links().map((link,i) => {
    return (
      <NavLink key={i} location={location} to={link.url}>{link.name}</NavLink>
    )
  })
  return (
    <header>
      <span><b>{site.name} cortex</b></span>
      <span>
        <select onChange={e => {
          let path = window.location.pathname.split("/")
          path.shift()
          console.log(path)
          if (path[0] in modules && path[1]) {
            history.push('/' + e.target.value + '/' + path[1] + '/')
          }
          actions.changeTool(e.target.value, true)
        }} value={app.tool}>
          {tool_list}
        </select>
      </span>
      <NavLink location={location} to="/system">system</NavLink>
      <NavLink location={location} to="/dashboard">dashboard</NavLink>
      <span>{links}</span>
      {playing && <span>{fps} fps</span>}
    </header>
  )
}

const mapStateToProps = state => ({
  site: state.system.site,
  app: state.system.app,
  fps: state.live.fps,
  playing: state.live.playing,
})

const mapDispatchToProps = (dispatch, ownProps) => ({
  actions: bindActionCreators(systemActions, dispatch),
})

export default connect(mapStateToProps, mapDispatchToProps)(Header)