Farmbot-Web-App/webpack/logs/index.tsx

115 lines
4.2 KiB
TypeScript
Raw Normal View History

2017-11-07 18:16:38 -07:00
import * as React from "react";
import * as moment from "moment";
import { connect } from "react-redux";
import { Col, Row, Page, ToolTip } from "../ui/index";
2017-11-07 18:16:38 -07:00
import { mapStateToProps } from "./state_to_props";
import { t } from "i18next";
import { Popover, Position } from "@blueprintjs/core";
2018-05-17 14:12:33 -06:00
import { LogsState, LogsProps, Filters } from "./interfaces";
2017-11-08 15:51:46 -07:00
import { ToolTips } from "../constants";
2017-12-01 22:02:18 -07:00
import { LogsSettingsMenu } from "./components/settings_menu";
import { LogsFilterMenu } from "./components/filter_menu";
import { LogsTable } from "./components/logs_table";
2017-12-11 04:50:41 -07:00
import { Session, safeNumericSetting } from "../session";
import { isUndefined } from "lodash";
import { NumericSetting } from "../session_keys";
import { NumberConfigKey } from "../config_storage/web_app_configs";
2017-11-07 18:16:38 -07:00
2018-05-17 14:12:33 -06:00
/** Format log date and time for display in the app. */
2017-12-28 13:28:44 -07:00
export const formatLogTime = (created_at: number, timeoffset: number) =>
moment.unix(created_at).utcOffset(timeoffset).format("MMM D, h:mma");
2017-11-07 18:16:38 -07:00
@connect(mapStateToProps)
export class Logs extends React.Component<LogsProps, Partial<LogsState>> {
2018-05-17 14:12:33 -06:00
/** Initialize log type verbosity level to the configured or default value. */
initialize = (name: NumberConfigKey, defaultValue: number): number => {
2018-01-11 14:05:21 -07:00
const currentValue = Session.deprecatedGetNum(safeNumericSetting(name));
2017-12-11 04:50:41 -07:00
if (isUndefined(currentValue)) {
2018-01-11 14:05:21 -07:00
Session.deprecatedSetNum(safeNumericSetting(name), defaultValue);
2017-12-11 04:50:41 -07:00
return defaultValue;
} else {
return currentValue;
}
}
2017-11-07 18:16:38 -07:00
state: LogsState = {
autoscroll: false,
success: this.initialize(NumericSetting.success_log, 1),
busy: this.initialize(NumericSetting.busy_log, 1),
warn: this.initialize(NumericSetting.warn_log, 1),
error: this.initialize(NumericSetting.error_log, 1),
info: this.initialize(NumericSetting.info_log, 1),
fun: this.initialize(NumericSetting.fun_log, 1),
debug: this.initialize(NumericSetting.debug_log, 1),
2017-11-07 18:16:38 -07:00
};
2018-05-17 14:12:33 -06:00
/** Toggle display of a log type. Verbosity level 0 hides all, 3 shows all.*/
toggle = (name: keyof Filters) => {
// If log type is off, set it to verbosity level 1, otherwise turn it off
const newSetting = this.state[name] === 0 ? 1 : 0;
return () => {
this.setState({ [name]: newSetting });
Session.deprecatedSetNum(safeNumericSetting(name + "_log"), newSetting);
};
2017-12-11 04:50:41 -07:00
}
2018-05-17 14:12:33 -06:00
/** Set log type filter level. i.e., level 2 shows verbosity 2 and lower.*/
setFilterLevel = (name: keyof Filters) => {
2017-12-11 04:50:41 -07:00
return (value: number) => {
this.setState({ [name]: value });
2018-01-11 14:05:21 -07:00
Session.deprecatedSetNum(safeNumericSetting(name + "_log"), value);
2017-12-11 04:50:41 -07:00
};
};
2017-11-07 18:16:38 -07:00
2018-05-17 14:12:33 -06:00
/** Determine if log type filters are active. */
2017-11-08 15:51:46 -07:00
get filterActive() {
const filterKeys = Object.keys(this.state)
.filter(x => !(x === "autoscroll"));
const filterValues = filterKeys
2018-05-17 14:12:33 -06:00
.map((key: keyof Filters) => this.state[key]);
// Filters active if every log type level is not equal to 3 (max verbosity)
2017-12-11 04:50:41 -07:00
return !filterValues.every(x => x == 3);
2017-11-08 15:51:46 -07:00
}
2017-11-07 18:16:38 -07:00
render() {
2017-11-08 15:51:46 -07:00
const filterBtnColor = this.filterActive ? "green" : "gray";
2017-11-07 18:16:38 -07:00
return <Page className="logs">
<Row>
2017-12-11 04:50:41 -07:00
<Col xs={10}>
2017-11-07 18:16:38 -07:00
<h3>
<i>{t("Logs")}</i>
</h3>
2017-11-08 15:51:46 -07:00
<ToolTip helpText={ToolTips.LOGS} />
2017-11-07 18:16:38 -07:00
</Col>
2017-12-11 04:50:41 -07:00
<Col xs={2}>
2017-12-01 21:47:57 -07:00
<div className={"settings-menu-button"}>
2018-07-27 15:42:45 -06:00
<Popover position={Position.TOP_RIGHT}>
2017-12-01 21:47:57 -07:00
<i className="fa fa-gear" />
2017-12-11 04:50:41 -07:00
<LogsSettingsMenu
2018-01-27 02:29:13 -07:00
setFilterLevel={this.setFilterLevel}
dispatch={this.props.dispatch}
sourceFbosConfig={this.props.sourceFbosConfig} />
2017-12-01 21:47:57 -07:00
</Popover>
</div>
<div className={"settings-menu-button"}>
2018-07-27 15:42:45 -06:00
<Popover position={Position.TOP_RIGHT}>
2017-12-01 21:47:57 -07:00
<button className={`fb-button ${filterBtnColor}`}>
{this.filterActive ? t("Filters active") : t("filter")}
</button>
2017-12-11 04:50:41 -07:00
<LogsFilterMenu
toggle={this.toggle} state={this.state}
setFilterLevel={this.setFilterLevel} />
2017-12-01 21:47:57 -07:00
</Popover>
</div>
2017-11-07 18:16:38 -07:00
</Col>
</Row>
<Row>
2017-12-28 12:33:11 -07:00
<LogsTable logs={this.props.logs}
state={this.state}
timeOffset={this.props.timeOffset} />
2017-11-07 18:16:38 -07:00
</Row>
</Page>;
}
}