Farmbot-Web-App/frontend/nav/index.tsx

158 lines
5.1 KiB
TypeScript
Raw Normal View History

2017-06-29 12:54:02 -06:00
import * as React from "react";
2017-07-07 03:52:39 -06:00
import { NavBarProps, NavBarState } from "./interfaces";
2017-06-29 12:54:02 -06:00
import { EStopButton } from "../devices/components/e_stop_btn";
import { Session } from "../session";
import { Row, Col } from "../ui/index";
2017-11-22 00:00:28 -07:00
import { getPathArray } from "../history";
2017-10-24 16:28:05 -06:00
import { updatePageInfo } from "../util";
2017-07-05 08:30:41 -06:00
import { SyncButton } from "./sync_button";
2017-08-04 15:41:15 -06:00
import { NavLinks } from "./nav_links";
2017-07-07 01:10:36 -06:00
import { TickerList } from "./ticker_list";
2017-07-05 23:02:20 -06:00
import { AdditionalMenu } from "./additional_menu";
2017-07-07 03:52:39 -06:00
import { MobileMenu } from "./mobile_menu";
2018-05-15 17:05:35 -06:00
import { Popover, Position } from "@blueprintjs/core";
2018-04-02 09:23:09 -06:00
import { ErrorBoundary } from "../error_boundary";
2018-10-15 17:23:58 -06:00
import { RunTour } from "../help/tour";
2019-04-02 13:59:37 -06:00
import { t } from "../i18next_wrapper";
2019-04-09 18:45:51 -06:00
import { Connectivity } from "../devices/connectivity/connectivity";
import { connectivityData } from "../devices/connectivity/generate_data";
import { DiagnosisSaucer } from "../devices/connectivity/diagnosis";
import { maybeSetTimezone } from "../devices/timezones/guess_timezone";
2019-04-17 13:31:18 -06:00
import { BooleanSetting } from "../session_keys";
import { ReadOnlyIcon } from "../read_only_mode";
2019-07-30 06:56:04 -06:00
2017-07-07 03:52:39 -06:00
export class NavBar extends React.Component<NavBarProps, Partial<NavBarState>> {
state: NavBarState = {
2017-07-07 10:50:52 -06:00
mobileMenuOpen: false,
tickerListOpen: false,
accountMenuOpen: false
2017-07-07 03:52:39 -06:00
};
2017-06-29 12:54:02 -06:00
2019-04-09 18:45:51 -06:00
componentDidMount = () => {
const { device } = this.props;
device && maybeSetTimezone(this.props.dispatch, device);
}
logout = () => Session.clear();
2017-06-29 12:54:02 -06:00
2017-07-07 03:52:39 -06:00
toggle = (name: keyof NavBarState) => () =>
this.setState({ [name]: !this.state[name] });
2017-09-05 19:51:38 -06:00
close = (name: keyof NavBarState) => () =>
this.setState({ [name]: false });
2019-12-26 13:20:10 -07:00
ReadOnlyStatus = () =>
<ReadOnlyIcon locked={!!this.props.getConfigValue(
BooleanSetting.user_interface_read_only_mode)} />
SyncButton = () =>
<SyncButton
2017-11-14 11:56:37 -07:00
bot={this.props.bot}
2017-11-20 12:44:46 -07:00
dispatch={this.props.dispatch}
2019-04-11 21:17:18 -06:00
autoSync={this.props.autoSync}
2019-12-26 13:20:10 -07:00
consistent={this.props.consistent} />
2019-04-09 18:45:51 -06:00
2019-12-26 13:20:10 -07:00
EstopButton = () =>
<EStopButton
bot={this.props.bot}
forceUnlock={!!this.props.getConfigValue(
BooleanSetting.disable_emergency_unlock_confirmation)} />
2019-04-09 18:45:51 -06:00
2019-12-26 13:20:10 -07:00
AccountMenu = () => {
2020-01-03 13:04:45 -07:00
const hasName = this.props.user?.body.name;
2017-10-24 16:28:05 -06:00
const firstName = hasName ?
`${hasName.split(" ")[0].slice(0, 9)} â–¾` : `${t("Menu")} â–¾`;
2019-12-26 13:20:10 -07:00
return <div className="menu-popover">
<Popover
portalClassName={"nav-right"}
popoverClassName={"menu-popover"}
position={Position.BOTTOM_RIGHT}
isOpen={this.state.accountMenuOpen}
onClose={this.close("accountMenuOpen")}>
<div className="nav-name" data-title={firstName}
onClick={this.toggle("accountMenuOpen")}>
{firstName}
</div>
{AdditionalMenu({ logout: this.logout, close: this.close })}
</Popover>
</div>;
}
2017-08-11 15:48:20 -06:00
2019-12-26 13:20:10 -07:00
ConnectionStatus = () => {
const data = connectivityData({
bot: this.props.bot,
device: this.props.device
});
return <div className="connection-status-popover">
<Popover position={Position.BOTTOM_RIGHT}
portalClassName={"connectivity-popover-portal"}
popoverClassName="connectivity-popover">
<DiagnosisSaucer {...data.flags} />
<ErrorBoundary>
<Connectivity
bot={this.props.bot}
rowData={data.rowData}
flags={data.flags}
pings={this.props.pings} />
</ErrorBoundary>
</Popover>
</div>;
}
2017-07-05 23:02:20 -06:00
2019-12-26 13:20:10 -07:00
AppNavLinks = () => {
const { close } = this;
const { mobileMenuOpen } = this.state;
const { alertCount } = this.props;
return <div>
<i className={"fa fa-bars mobile-menu-icon"}
onClick={this.toggle("mobileMenuOpen")} />
<span className="mobile-menu-container">
{MobileMenu({ close, mobileMenuOpen, alertCount })}
</span>
<span className="top-menu-container">
{NavLinks({ close, alertCount })}
</span>
</div>;
}
2017-06-29 12:54:02 -06:00
2019-12-26 13:20:10 -07:00
render() {
2017-07-04 07:32:23 -06:00
/** Change document meta title on every route change. */
2019-12-26 13:20:10 -07:00
updatePageInfo(getPathArray()[2] || "");
2017-06-29 12:54:02 -06:00
2019-12-26 13:20:10 -07:00
const { toggle } = this;
const { tickerListOpen } = this.state;
const { logs, timeSettings, getConfigValue } = this.props;
2018-08-30 19:25:58 -06:00
const tickerListProps = {
2019-04-09 23:17:03 -06:00
logs, tickerListOpen, toggle, timeSettings, getConfigValue
2018-08-30 19:25:58 -06:00
};
2018-04-02 09:23:09 -06:00
return <ErrorBoundary>
<div className="nav-wrapper">
<nav role="navigation">
<Row>
<Col xs={12}>
<div>
2018-08-30 19:25:58 -06:00
<TickerList {...tickerListProps} />
2018-04-02 09:23:09 -06:00
<div className="nav-group">
<div className="nav-left">
2019-12-26 13:20:10 -07:00
<this.AppNavLinks />
2018-04-02 09:23:09 -06:00
</div>
<div className="nav-right">
2019-12-26 13:20:10 -07:00
<ErrorBoundary>
<this.ReadOnlyStatus />
<this.AccountMenu />
<this.EstopButton />
<this.SyncButton />
<this.ConnectionStatus />
<RunTour currentTour={this.props.tour} />
</ErrorBoundary>
2018-04-02 09:23:09 -06:00
</div>
2017-07-07 01:10:36 -06:00
</div>
2017-07-05 10:40:38 -06:00
</div>
2018-04-02 09:23:09 -06:00
</Col>
</Row>
</nav>
</div>
</ErrorBoundary>;
2017-06-29 12:54:02 -06:00
}
}