-
Notifications
You must be signed in to change notification settings - Fork 324
/
app.tsx
169 lines (161 loc) · 6.06 KB
/
app.tsx
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import * as React from "react";
import { connect } from "react-redux";
import { init, error } from "./toast/toast";
import { NavBar } from "./nav";
import { Everything, TimeSettings } from "./interfaces";
import { LoadingPlant } from "./loading_plant";
import { BotState, UserEnv } from "./devices/interfaces";
import {
ResourceName, TaggedUser, TaggedLog, Xyz, Alert, FirmwareHardware,
} from "farmbot";
import {
maybeFetchUser,
maybeGetTimeSettings,
getDeviceAccountSettings,
} from "./resources/selectors";
import { HotKeys } from "./hotkeys";
import { ControlsPopup, showControlsPopup } from "./controls_popup";
import { Content } from "./constants";
import { validBotLocationData, validFwConfig, validFbosConfig } from "./util";
import { BooleanSetting } from "./session_keys";
import {
getWebAppConfigValue, GetWebAppConfigValue,
} from "./config_storage/actions";
import { takeSortedLogs } from "./logs/state_to_props";
import { FirmwareConfig } from "farmbot/dist/resources/configs/firmware";
import { getFirmwareConfig, getFbosConfig } from "./resources/getters";
import { intersection } from "lodash";
import { t } from "./i18next_wrapper";
import { ResourceIndex } from "./resources/interfaces";
import { isBotOnlineFromState } from "./devices/must_be_online";
import { getAllAlerts } from "./messages/state_to_props";
import { PingDictionary } from "./devices/connectivity/qos";
import { getEnv, getShouldDisplayFn } from "./farmware/state_to_props";
import { filterAlerts } from "./messages/alerts";
import {
isFwHardwareValue,
} from "./devices/components/firmware_hardware_support";
/** For the logger module */
init();
export interface AppProps {
dispatch: Function;
loaded: ResourceName[];
logs: TaggedLog[];
user: TaggedUser | undefined;
bot: BotState;
consistent: boolean;
timeSettings: TimeSettings;
axisInversion: Record<Xyz, boolean>;
xySwap: boolean;
firmwareConfig: FirmwareConfig | undefined;
animate: boolean;
getConfigValue: GetWebAppConfigValue;
tour: string | undefined;
resources: ResourceIndex;
autoSync: boolean;
alertCount: number;
alerts: Alert[];
apiFirmwareValue: FirmwareHardware | undefined;
pings: PingDictionary;
env: UserEnv;
}
export function mapStateToProps(props: Everything): AppProps {
const webAppConfigValue = getWebAppConfigValue(() => props);
const fbosConfig = validFbosConfig(getFbosConfig(props.resources.index));
const fwHardware = fbosConfig?.firmware_hardware;
const shouldDisplay = getShouldDisplayFn(props.resources.index, props.bot);
const env = getEnv(props.resources.index, shouldDisplay, props.bot);
return {
timeSettings: maybeGetTimeSettings(props.resources.index),
dispatch: props.dispatch,
user: maybeFetchUser(props.resources.index),
bot: props.bot,
logs: takeSortedLogs(250, props.resources.index),
loaded: props.resources.loaded,
consistent: !!(props.bot || {}).consistent,
axisInversion: {
x: !!webAppConfigValue(BooleanSetting.x_axis_inverted),
y: !!webAppConfigValue(BooleanSetting.y_axis_inverted),
z: !!webAppConfigValue(BooleanSetting.z_axis_inverted),
},
xySwap: !!webAppConfigValue(BooleanSetting.xy_swap),
firmwareConfig: validFwConfig(getFirmwareConfig(props.resources.index)),
animate: !webAppConfigValue(BooleanSetting.disable_animations),
getConfigValue: webAppConfigValue,
tour: props.resources.consumers.help.currentTour,
resources: props.resources.index,
autoSync: !!(fbosConfig && fbosConfig.auto_sync),
alertCount: getAllAlerts(props.resources).filter(filterAlerts).length,
alerts: getAllAlerts(props.resources),
apiFirmwareValue: isFwHardwareValue(fwHardware) ? fwHardware : undefined,
pings: props.bot.connectivity.pings,
env,
};
}
/** Time at which the app gives up and asks the user to refresh */
const LOAD_TIME_FAILURE_MS = 25000;
/**
* Relational resources that *must* load before app starts.
* App will crash at load time if they are not pre-loaded.
*/
const MUST_LOAD: ResourceName[] = [
"Sequence",
"Regimen",
"FarmEvent",
"Point",
"Device",
"Tool", // Sequence editor needs this for rendering.
];
export class RawApp extends React.Component<AppProps, {}> {
private get isLoaded() {
return (MUST_LOAD.length ===
intersection(this.props.loaded, MUST_LOAD).length);
}
/**
* If the sync object takes more than 10s to load, the user will be granted
* access into the app, but still warned.
*/
componentDidMount() {
setTimeout(() => {
if (!this.isLoaded) {
error(t(Content.APP_LOAD_TIMEOUT_MESSAGE), t("Warning"));
}
}, LOAD_TIME_FAILURE_MS);
}
render() {
const syncLoaded = this.isLoaded;
const { location_data, mcu_params } = this.props.bot.hardware;
return <div className="app">
{!syncLoaded && <LoadingPlant animate={this.props.animate} />}
<HotKeys dispatch={this.props.dispatch} />
{syncLoaded && <NavBar
timeSettings={this.props.timeSettings}
consistent={this.props.consistent}
user={this.props.user}
bot={this.props.bot}
dispatch={this.props.dispatch}
logs={this.props.logs}
getConfigValue={this.props.getConfigValue}
tour={this.props.tour}
autoSync={this.props.autoSync}
alertCount={this.props.alertCount}
device={getDeviceAccountSettings(this.props.resources)}
alerts={this.props.alerts}
apiFirmwareValue={this.props.apiFirmwareValue}
pings={this.props.pings} />}
{syncLoaded && this.props.children}
{showControlsPopup() &&
<ControlsPopup
dispatch={this.props.dispatch}
axisInversion={this.props.axisInversion}
botPosition={validBotLocationData(location_data).position}
firmwareSettings={this.props.firmwareConfig || mcu_params}
xySwap={this.props.xySwap}
arduinoBusy={!!this.props.bot.hardware.informational_settings.busy}
botOnline={isBotOnlineFromState(this.props.bot)}
env={this.props.env}
stepSize={this.props.bot.stepSize} />}
</div>;
}
}
export const App = connect(mapStateToProps)(RawApp);