Skip to content

Commit

Permalink
Add button state
Browse files Browse the repository at this point in the history
  • Loading branch information
rafal-gorecki committed Jan 7, 2025
1 parent bfc2de7 commit 17e3f47
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Props = {
context: PanelExtensionContext;
};

type ButtonState = "activated" | "deactivated" | undefined;

type SrvState = {
status: "requesting" | "error" | "success";
value: string;
Expand All @@ -45,8 +47,8 @@ type Action =
| { type: "path"; path: string }
| { type: "seek" };

const useStyles = makeStyles<{ action?: boolean; config: Config }>()((theme, { action, config }) => {
const buttonColor = action === true ? config.activationColor : config.deactivationColor;
const useStyles = makeStyles<{ action?: ButtonState; config: Config }>()((theme, { action, config }) => {
const buttonColor = action === "activated" ? config.activationColor : config.deactivationColor;
const augmentedButtonColor = theme.palette.augmentColor({
color: { main: buttonColor },
});
Expand Down Expand Up @@ -160,7 +162,7 @@ function ToggleSrvButtonContent(
...defaultConfig,
...(context.initialState as Partial<Config>),
}));
const [buttonAction, setButtonAction] = useState<boolean | undefined>(undefined);
const [buttonAction, setButtonAction] = useState<ButtonState | undefined>(undefined);
const { classes } = useStyles({ action: buttonAction, config });

const [state, dispatch] = useReducer(
Expand Down Expand Up @@ -267,7 +269,7 @@ function ToggleSrvButtonContent(
try {
if (buttonAction != undefined) {
setSrvState({ status: "requesting", value: `Calling ${config.serviceName}...` });
const requestPayload = { data: config.reverseLogic ? !buttonAction : buttonAction };
const requestPayload = { data: buttonAction === "activated" ? false : true };
setButtonAction(undefined);
const response = await context.callService(config.serviceName, requestPayload) as { success?: boolean };
setSrvState({
Expand All @@ -285,9 +287,10 @@ function ToggleSrvButtonContent(
useEffect(() => {
const data = state.latestMatchingQueriedData;
if (typeof data === "boolean") {
setButtonAction(!data);
const isDeactivated = data === config.reverseLogic;
setButtonAction(isDeactivated ? "deactivated" : "activated");
}
}, [state.latestMatchingQueriedData]);
}, [state.latestMatchingQueriedData, config.reverseLogic]);

// Indicate render is complete - the effect runs after the dom is updated
useEffect(() => {
Expand Down Expand Up @@ -325,7 +328,7 @@ function ToggleSrvButtonContent(
borderRadius: "0.3rem",
}}
>
{buttonAction === true ? config.activationText : buttonAction === false ? config.deactivationText : "Unknown"}
{buttonAction === "activated" ? config.deactivationText : buttonAction === "deactivated" ? config.activationText : "Unknown"}
</Button>
</span>
</Stack>
Expand Down

0 comments on commit 17e3f47

Please sign in to comment.