Dynamically determine the correct action config struct (#12798)

This commit is contained in:
Philip Allgaier 2022-05-26 19:26:25 +02:00 committed by GitHub
parent 00dcecabb7
commit 73cf0b54c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 13 deletions

View File

@ -159,6 +159,7 @@ export interface CustomActionConfig extends BaseActionConfig {
}
export interface BaseActionConfig {
action: string;
confirmation?: ConfirmationRestrictionConfig;
}

View File

@ -1,14 +1,16 @@
import {
object,
string,
union,
boolean,
optional,
array,
literal,
boolean,
dynamic,
enums,
literal,
object,
optional,
string,
type,
union,
} from "superstruct";
import { BaseActionConfig } from "../../../../data/lovelace";
const actionConfigStructUser = object({
user: string(),
@ -65,10 +67,23 @@ export const actionConfigStructType = object({
confirmation: optional(actionConfigStructConfirmation),
});
export const actionConfigStruct = union([
actionConfigStructType,
actionConfigStructUrl,
actionConfigStructNavigate,
actionConfigStructService,
actionConfigStructCustom,
]);
export const actionConfigStruct = dynamic<any>((value) => {
if (value && typeof value === "object" && "action" in value) {
switch ((value as BaseActionConfig).action!) {
case "call-service": {
return actionConfigStructService;
}
case "fire-dom-event": {
return actionConfigStructCustom;
}
case "navigate": {
return actionConfigStructNavigate;
}
case "url": {
return actionConfigStructUrl;
}
}
}
return actionConfigStructType;
});