Skip to content

Commit 671f133

Browse files
committed
Add validations for topic name
1 parent 00fa329 commit 671f133

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

packages/webviz-core/src/panels/Teleop/index.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// You may not use this file except in compliance with the License.
88
import React, { useState, useReducer } from "react";
99
import { hot } from "react-hot-loader/root";
10+
import styled from "styled-components";
1011

1112
import helpContent from "./index.help.md";
1213
import { PanelToolbarInput, PanelToolbarLabel } from "webviz-core/shared/panelToolbarStyles";
@@ -35,6 +36,14 @@ type Props = {
3536
datatypes: RosDatatypes,
3637
};
3738

39+
const SErrorText = styled.div`
40+
flex: 1 1 auto;
41+
display: flex;
42+
align-items: center;
43+
padding: 4px;
44+
color: ${colors.red};
45+
`;
46+
3847
function Teleop(props: Props) {
3948
const { config, saveConfig, capabilities } = props;
4049
const { topicName } = config;
@@ -49,6 +58,10 @@ function Teleop(props: Props) {
4958
" ": false,
5059
});
5160

61+
const isValidTopic = (t) => {
62+
return t.startsWith("/") && t.length >= 3;
63+
};
64+
5265
const composeTwist = (key) => {
5366
const COMMANDS = {
5467
// Map x, y, z, th here
@@ -81,12 +94,12 @@ function Teleop(props: Props) {
8194
const handleKey = (event) => {
8295
const newPressing = pressing;
8396
newPressing[event.key] = event.type === "keydown";
84-
if (publisher.current) {
97+
if (publisher.current && isValidTopic(topicName)) {
8598
if (event.type === "keydown") {
8699
publisher.current.publish(composeTwist(event.key));
87100
}
88101
} else {
89-
console.error("Publisher not set!");
102+
console.error("Publisher not set or topic name invalid");
90103
}
91104
setPressing(newPressing);
92105

@@ -111,8 +124,28 @@ function Teleop(props: Props) {
111124

112125
const buttonColor = "#00A871";
113126

114-
const canPublish = capabilities.includes(PlayerCapabilities.advertise) && topicName;
115-
127+
const canPublish = capabilities.includes(PlayerCapabilities.advertise) && isValidTopic(topicName);
128+
129+
const renderError = () => {
130+
if (topicName.length === 0) {
131+
return (
132+
<>
133+
<SErrorText>Topic name can&apos;t be empty!</SErrorText>
134+
</>
135+
);
136+
} else if (!isValidTopic(topicName)) {
137+
return (
138+
<>
139+
{
140+
<SErrorText>
141+
Topic name &quot;{topicName}&quot; is invalid (must be at least 2 characters long and start with /)
142+
</SErrorText>
143+
}
144+
</>
145+
);
146+
}
147+
return <></>;
148+
};
116149
const renderMenuContent = () => {
117150
return (
118151
<>
@@ -179,6 +212,9 @@ function Teleop(props: Props) {
179212
Stop
180213
</Button>
181214
</Flex>
215+
<Flex row style={{ flex: "0 0 auto", justifyContent: "center", paddingTop: "5px", paddingBottom: "5px" }}>
216+
{renderError()}
217+
</Flex>
182218
</Flex>
183219
);
184220
}

packages/webviz-core/src/panels/Teleop/index.stories.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const getFixture = (allowPublish) => {
2323
};
2424
};
2525

26-
const publishConfig = () => ({
27-
topicName: "/cmd_vel",
26+
const publishConfig = (topic = "/cmd_vel") => ({
27+
topicName: topic,
2828
});
2929

3030
storiesOf("<Teleop>", module)
@@ -44,6 +44,24 @@ storiesOf("<Teleop>", module)
4444
</PanelSetup>
4545
);
4646
})
47+
.add("example with invalid topic name", () => {
48+
const allowPublish = true;
49+
// Will attempt to use topic name /a, which is too short
50+
return (
51+
<PanelSetup fixture={getFixture(allowPublish)}>
52+
<Teleop config={publishConfig("/a")} />
53+
</PanelSetup>
54+
);
55+
})
56+
.add("example with empty topic name", () => {
57+
const allowPublish = true;
58+
// Will attempt to use the empty topic
59+
return (
60+
<PanelSetup fixture={getFixture(allowPublish)}>
61+
<Teleop config={publishConfig("")} />
62+
</PanelSetup>
63+
);
64+
})
4765
.add("Example with datatype that no longer exists", () => {
4866
return (
4967
<PanelSetup fixture={{ topics: [], datatypes: {}, frame: {}, capabilities: [] }}>

0 commit comments

Comments
 (0)