Skip to content

Commit

Permalink
Add status to ToggleButton
Browse files Browse the repository at this point in the history
  • Loading branch information
rafal-gorecki committed Dec 23, 2024
1 parent 4395c36 commit 1db5f9c
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 108 deletions.
32 changes: 2 additions & 30 deletions packages/studio-base/src/panels/EStop/EStop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,6 @@ const useStyles = makeStyles<{ state?: string }>()((theme, { state }) => {
};
});

function parseInput(value: string): { error?: string; parsedObject?: unknown } {
let parsedObject;
let error = undefined;
try {
const parsedAny: unknown = JSON.parse(value);
if (Array.isArray(parsedAny)) {
error = "Request content must be an object, not an array";
} else if (parsedAny == undefined) {
error = "Request content must be an object, not null";
} else if (typeof parsedAny !== "object") {
error = `Request content must be an object, not ‘${typeof parsedAny}’`;
} else {
parsedObject = parsedAny;
}
} catch (e) {
error = value.length !== 0 ? e.message : "Enter valid request content as JSON";
}
return { error, parsedObject };
}

function getSingleDataItem(results: unknown[]) {
if (results.length <= 1) {
return results[0];
Expand Down Expand Up @@ -253,11 +233,6 @@ function EStopContent(
};
}, [context, state.parsedPath?.topicName]);

const { error: requestParseError, parsedObject } = useMemo(
() => parseInput(config.requestPayload ?? ""),
[config.requestPayload],
);

const settingsActionHandler = useCallback(
(action: SettingsTreeAction) => {
setConfig((prevConfig) => settingsActionReducer(prevConfig, action));
Expand Down Expand Up @@ -285,12 +260,9 @@ function EStopContent(

const canEStop = Boolean(
context.callService != undefined &&
config.requestPayload &&
config.goServiceName &&
config.stopServiceName &&
eStopAction != undefined &&
parsedObject != undefined &&
requestParseError == undefined &&
reqState?.status !== "requesting",
);

Expand All @@ -309,7 +281,7 @@ function EStopContent(

try {
setReqState({ status: "requesting", value: `Calling ${serviceName}...` });
const response = await context.callService(serviceName, JSON.parse(config.requestPayload!));
const response = await context.callService(serviceName, {});
setReqState({
status: "success",
value: JSON.stringify(response, (_key, value) => (typeof value === "bigint" ? value.toString() : value), 2) ?? "",
Expand All @@ -319,7 +291,7 @@ function EStopContent(
setReqState({ status: "error", value: (err as Error).message });
log.error(err);
}
}, [context, eStopAction, config.goServiceName, config.stopServiceName, config.requestPayload]);
}, [context, eStopAction, config.goServiceName, config.stopServiceName]);

// Setting eStopAction based on state.latestMatchingQueriedData
useEffect(() => {
Expand Down
37 changes: 11 additions & 26 deletions packages/studio-base/src/panels/EStop/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import { Config } from "./types";

const successResponseJson = JSON.stringify({ success: true }, undefined, 2);
const baseConfig: Config = {
goServiceName: "/set_bool",
requestPayload: `{\n "data": true\n}`,
goServiceName: "/trigger",
stopServiceName: "/reset",
statusTopicName: "/status",
};

const getFixture = ({ allowEStop }: { allowEStop: boolean }): Fixture => {
const eStop = async (service: string, _request: unknown) => {
const getFixture = ({ allowCallService }: { allowCallService: boolean }): Fixture => {
const callService = async (service: string, _request: unknown) => {
if (service !== baseConfig.goServiceName) {
throw new Error(`Service "${service}" does not exist`);
}
Expand All @@ -35,8 +36,8 @@ const getFixture = ({ allowEStop }: { allowEStop: boolean }): Fixture => {
}),
),
frame: {},
capabilities: allowEStop ? [PlayerCapabilities.eStops] : [],
eStop,
capabilities: allowCallService ? [PlayerCapabilities.callServices] : [],
callService,
};
};

Expand Down Expand Up @@ -68,7 +69,7 @@ export const EStopEnabled: StoryObj = {
return <EStopPanel />;
},

parameters: { panelSetup: { fixture: getFixture({ allowEStop: true }) } },
parameters: { panelSetup: { fixture: getFixture({ allowCallService: true }) } },
};

export const EStopEnabledServiceName: StoryObj = {
Expand All @@ -88,7 +89,7 @@ export const EStopEnabledServiceName: StoryObj = {
}
},

parameters: { panelSetup: { fixture: getFixture({ allowEStop: true }) } },
parameters: { panelSetup: { fixture: getFixture({ allowCallService: true }) } },
};

export const EStopEnabledWithCustomButtonSettings: StoryObj = {
Expand All @@ -107,23 +108,7 @@ export const EStopEnabledWithCustomButtonSettings: StoryObj = {
});
},

parameters: { panelSetup: { fixture: getFixture({ allowEStop: true }) } },
};

const validJSON = `{\n "a": 1,\n "b": 2,\n "c": 3\n}`;

export const WithValidJSON: StoryObj = {
render: () => {
return <EStopPanel overrideConfig={{ ...baseConfig, requestPayload: validJSON }} />;
},
};

const invalidJSON = `{\n "a": 1,\n 'b: 2,\n "c": 3\n}`;

export const WithInvalidJSON: StoryObj = {
render: () => {
return <EStopPanel overrideConfig={{ ...baseConfig, requestPayload: invalidJSON }} />;
},
parameters: { panelSetup: { fixture: getFixture({ allowCallService: true }) } },
};

export const CallingServiceThatDoesNotExist: StoryObj = {
Expand All @@ -150,5 +135,5 @@ export const CallingServiceThatDoesNotExist: StoryObj = {
}
},

parameters: { panelSetup: { fixture: getFixture({ allowEStop: true }) } },
parameters: { panelSetup: { fixture: getFixture({ allowCallService: true }) } },
};
1 change: 0 additions & 1 deletion packages/studio-base/src/panels/EStop/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { SettingsTreeAction, SettingsTreeNodes } from "@foxglove/studio";
import { Config } from "./types";

export const defaultConfig: Config = {
requestPayload: "{}",
goServiceName: "",
stopServiceName: "",
statusTopicName: "",
Expand Down
1 change: 0 additions & 1 deletion packages/studio-base/src/panels/EStop/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ export type Config = {
goServiceName: string;
stopServiceName: string;
statusTopicName: string;
requestPayload?: string;
};
Loading

0 comments on commit 1db5f9c

Please sign in to comment.