Skip to content

Commit 6fde15a

Browse files
committed
Add a snackbar with an alert for running plans
1 parent 292f1bd commit 6fde15a

File tree

3 files changed

+82
-56
lines changed

3 files changed

+82
-56
lines changed

src/blueapi/BlueapiComponents.tsx

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,81 @@ import {
99
Typography,
1010
} from "@mui/material";
1111

12+
type SeverityLevel = "success" | "info" | "warning" | "error";
1213
type VariantChoice = "outlined" | "contained";
14+
type ButtonSize = "small" | "medium" | "large";
1315

14-
type PlanButtonProps = {
16+
type RunPlanButtonProps = {
1517
btnLabel: string;
1618
planName: string;
1719
planParams?: object;
1820
title?: string;
19-
// btnColour?: string;
2021
btnVariant?: VariantChoice;
22+
btnSize?: ButtonSize;
2123
};
2224

23-
export function RunPlanButton(props: PlanButtonProps) {
25+
export function RunPlanButton(props: RunPlanButtonProps) {
26+
const [openSnackbar, setOpenSnackbar] = React.useState<boolean>(false);
27+
const [msg, setMsg] = React.useState<string>("Running plan...");
28+
const [severity, setSeverity] = React.useState<SeverityLevel>("info");
29+
2430
const params = props.planParams ? props.planParams : {};
2531
const variant = props.btnVariant ? props.btnVariant : "outlined";
32+
const size = props.btnSize ? props.btnSize : "medium";
33+
34+
const handleClick = () => {
35+
setOpenSnackbar(true);
36+
submitAndRunPlanImmediately({
37+
planName: props.planName,
38+
planParams: params,
39+
}).catch((error) => {
40+
setSeverity("error");
41+
setMsg(
42+
`Failed to run plan ${props.planName}, see console and logs for full error`
43+
);
44+
console.log(`${msg}. Reason: ${error}`);
45+
});
46+
};
47+
48+
const handleSnackbarClose = (
49+
_event: React.SyntheticEvent | Event,
50+
reason?: SnackbarCloseReason
51+
) => {
52+
if (reason === "clickaway") {
53+
return;
54+
}
55+
56+
setOpenSnackbar(false);
57+
};
2658

2759
return (
2860
<div>
2961
<Tooltip title={props.title ? props.title : ""} placement="bottom">
3062
<Button
3163
variant={variant}
3264
color="custom"
33-
onClick={() =>
34-
submitAndRunPlanImmediately({
35-
planName: props.planName,
36-
planParams: params,
37-
})
38-
}
65+
size={size}
66+
onClick={handleClick}
3967
>
40-
{props.btnLabel}
68+
<Typography
69+
variant="button"
70+
fontWeight="fontWeightBold"
71+
sx={{ display: "block" }}
72+
>
73+
{props.btnLabel}
74+
</Typography>
4175
</Button>
4276
</Tooltip>
77+
<Snackbar
78+
open={openSnackbar}
79+
autoHideDuration={5000}
80+
onClose={handleSnackbarClose}
81+
anchorOrigin={{ vertical: "bottom", horizontal: "right" }}
82+
>
83+
<Alert onClose={handleSnackbarClose} severity={severity}>
84+
{msg}
85+
</Alert>
86+
</Snackbar>
4387
</div>
4488
);
4589
}

src/blueapi/blueapi.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,11 @@ function runTask(taskId: string): Promise<string | void> {
102102
export function submitAndRunPlanImmediately(
103103
request: BlueApiRequestBody
104104
): Promise<string | void> {
105-
return submitTask(request)
106-
.then((res) => {
107-
if (res) {
108-
runTask(res);
109-
}
110-
})
111-
.catch((error) => console.log(`NOPE ${error}`));
105+
return submitTask(request).then((res) => {
106+
if (res) {
107+
runTask(res);
108+
}
109+
});
112110
}
113111

114112
export function abortCurrentPlan(): Promise<BlueApiWorkerState> {

src/screens/CollectionPanel.tsx

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { MapView, PumpProbeOptions } from "../components/CollectionComponents";
1717
import { submitAndRunPlanImmediately } from "../blueapi/blueapi";
1818
import { chipTypes, MapTypes, pumpProbeMode } from "../components/params";
1919
import { forceString } from "../pv/util";
20-
import { AbortButton } from "../blueapi/BlueapiComponents";
20+
import { AbortButton, RunPlanButton } from "../blueapi/BlueapiComponents";
2121

2222
/**
2323
* A couple of read-only boxes showing what the visit and detector in use are.
@@ -64,44 +64,28 @@ function RunButtons(props: ParametersProps) {
6464
return (
6565
<Grid2 size={12}>
6666
<Stack direction={"row"} spacing={8} justifyContent={"center"}>
67-
{/* See
68-
https://github.com/DiamondLightSource/mx-daq-ui/issues/3?issue=DiamondLightSource%7Cmx-daq-ui%7C18 */}
69-
<Tooltip title="Start fixed target collection" placement="bottom">
70-
<Button
71-
variant="outlined"
72-
color="custom"
73-
size="large"
74-
onClick={() =>
75-
submitAndRunPlanImmediately({
76-
planName: "gui_run_chip_collection",
77-
planParams: {
78-
sub_dir: props.subDir,
79-
chip_name: props.chipName,
80-
exp_time: props.expTime,
81-
det_dist: props.detDist,
82-
transmission: props.transFract,
83-
n_shots: props.nShots,
84-
chip_type: props.chipType,
85-
map_type: props.mapType,
86-
chip_format: props.chipFormat,
87-
checker_pattern: props.checkerPattern,
88-
pump_probe: props.pumpProbe,
89-
laser_dwell: props.pumpInputs[0],
90-
laser_delay: props.pumpInputs[1],
91-
pre_pump: props.pumpInputs[2],
92-
},
93-
})
94-
}
95-
>
96-
<Typography
97-
variant="button"
98-
fontWeight="fontWeightBold"
99-
sx={{ display: "block" }}
100-
>
101-
Start!
102-
</Typography>
103-
</Button>
104-
</Tooltip>
67+
<RunPlanButton
68+
btnLabel="Start!"
69+
planName="gui_run_chip_collection"
70+
planParams={{
71+
sub_dir: props.subDir,
72+
chip_name: props.chipName,
73+
exp_time: props.expTime,
74+
det_dist: props.detDist,
75+
transmission: props.transFract,
76+
n_shots: props.nShots,
77+
chip_type: props.chipType,
78+
map_type: props.mapType,
79+
chip_format: props.chipFormat,
80+
checker_pattern: props.checkerPattern,
81+
pump_probe: props.pumpProbe,
82+
laser_dwell: props.pumpInputs[0],
83+
laser_delay: props.pumpInputs[1],
84+
pre_pump: props.pumpInputs[2],
85+
}}
86+
title="Start fixed target collection"
87+
btnSize="large"
88+
/>
10589
<AbortButton />
10690
</Stack>
10791
</Grid2>

0 commit comments

Comments
 (0)