import { map, sortBy } from 'common/collections'; import { flow } from 'common/fp'; import { toFixed } from 'common/math'; import { pureComponentHooks } from 'common/react'; import { Component, Fragment } from 'inferno'; import { Box, Button, Chart, ColorBox, Flex, Icon, LabeledList, ProgressBar, Section, Table } from '../components'; const PEAK_DRAW = 500000; const powerRank = str => { const unit = String(str.split(' ')[1]).toLowerCase(); return ['w', 'kw', 'mw', 'gw'].indexOf(unit); }; export class PowerMonitor extends Component { constructor() { super(); this.state = { sortByField: null, }; } render() { const { state } = this.props; const { data } = state; const { history } = data; const { sortByField } = this.state; const supply = history.supply[history.supply.length - 1] || 0; const demand = history.demand[history.demand.length - 1] || 0; const supplyData = history.supply.map((value, i) => [i, value]); const demandData = history.demand.map((value, i) => [i, value]); const maxValue = Math.max( PEAK_DRAW, ...history.supply, ...history.demand); // Process area data const areas = flow([ map((area, i) => ({ ...area, // Generate a unique id id: area.name + i, })), sortByField === 'name' && sortBy(area => area.name), sortByField === 'charge' && sortBy(area => -area.charge), sortByField === 'draw' && sortBy( area => -powerRank(area.load), area => -parseFloat(area.load)), ])(data.areas); return (
Sort by: this.setState({ sortByField: sortByField !== 'name' && 'name', })} /> this.setState({ sortByField: sortByField !== 'charge' && 'charge', })} /> this.setState({ sortByField: sortByField !== 'draw' && 'draw', })} /> Area Charge Draw Eqp Lgt Env {areas.map((area, i) => ( ))}
{area.name} {area.load}
); } } const AreaCharge = props => { const { charging, charge } = props; return ( 50 ? 'battery-half' : 'battery-quarter' ) || charging === 1 && 'bolt' || charging === 2 && 'battery-full' )} color={( charging === 0 && ( charge > 50 ? 'yellow' : 'red' ) || charging === 1 && 'yellow' || charging === 2 && 'green' )} /> {toFixed(charge) + '%'} ); }; AreaCharge.defaultHooks = pureComponentHooks; const AreaStatusColorBox = props => { const { status } = props; const power = Boolean(status & 2); const mode = Boolean(status & 1); const tooltipText = (power ? 'On' : 'Off') + ` [${mode ? 'auto' : 'manual'}]`; return ( ); }; AreaStatusColorBox.defaultHooks = pureComponentHooks;