forked from arduino/arduino-serial-plotter-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageToBoard.tsx
125 lines (118 loc) · 3.52 KB
/
MessageToBoard.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
import React, { useState } from "react";
import Select from "react-select";
import { isEOL, MonitorSettings, PluggableMonitor } from "./utils";
export default React.memo(function MessageToBoard({
config,
wsSend,
}: {
config: Partial<MonitorSettings>;
wsSend: (
clientCommand: PluggableMonitor.Protocol.ClientCommandMessage
) => void;
}): React.ReactElement {
const [message, setMessage] = useState("");
const baudRate = config?.pluggableMonitorSettings?.baudrate?.selectedValue;
const disabled = !config?.monitorUISettings?.connected;
const lineEnding = config?.monitorUISettings?.lineEnding;
const lineendings = [
{ value: "", label: "No Line Ending" },
{ value: "\n", label: "New Line" },
{ value: "\r", label: "Carriage Return" },
{ value: "\r\n", label: "Both NL & CR" },
];
const baudrates = config?.pluggableMonitorSettings?.baudrate?.values?.map(
(baud) => ({
value: baud,
label: `${baud} baud`,
})
);
return (
<div className="message-to-board">
<form
className="message-container"
onSubmit={(e) => {
wsSend({
command: PluggableMonitor.Protocol.ClientCommand.SEND_MESSAGE,
data: message + lineEnding,
});
setMessage("");
e.preventDefault();
e.stopPropagation();
}}
>
<input
className="message-to-board-input"
type="text"
disabled={disabled}
value={message}
onChange={(event) => setMessage(event.target.value)}
placeholder="Type Message"
/>
<button
type="submit"
className={"message-to-board-send-button"}
disabled={message.length === 0 || disabled}
>
Send
</button>
<Select
className="singleselect lineending"
classNamePrefix="select"
isDisabled={disabled}
value={
lineendings[lineendings.findIndex((l) => l.value === lineEnding)]
}
name="lineending"
options={lineendings}
menuPlacement="top"
onChange={(event) => {
if (event && isEOL(event.value)) {
wsSend({
command:
PluggableMonitor.Protocol.ClientCommand.CHANGE_SETTINGS,
data: {
monitorUISettings: {
lineEnding: event.value,
},
},
});
}
}}
/>
</form>
<div>
<div className="baud">
{baudrates && (
<Select
className="singleselect"
classNamePrefix="select"
isDisabled={disabled}
value={
baudrates[baudrates.findIndex((b) => b.value === baudRate)]
}
name="baudrate"
options={baudrates}
menuPlacement="top"
onChange={(val) => {
if (val) {
wsSend({
command:
PluggableMonitor.Protocol.ClientCommand.CHANGE_SETTINGS,
data: {
pluggableMonitorSettings: {
baudrate: {
...config?.pluggableMonitorSettings?.baudrate,
selectedValue: val.value,
},
},
},
});
}
}}
/>
)}
</div>
</div>
</div>
);
});