v1 of service editor
This commit is contained in:
1 parent
0cecae5faa
commit
7b816c3fbf
16 files changed
+1185
-435
No files matched your search
@@ -0,0 +1,5 @@
|
||||
.service-editor--field {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import "./ServiceEditorField.css";
|
||||
import { FC } from "react";
|
||||
import { ServiceEditorOption } from "./options";
|
||||
import InputText from "components/Inputs/InputText";
|
||||
import InputNumber from "components/Inputs/InputNumber";
|
||||
import { IconButton } from "@mui/material";
|
||||
import { RemoveCircle } from "@mui/icons-material";
|
||||
|
||||
export type ServiceEditorFieldProps = {
|
||||
option: ServiceEditorOption;
|
||||
value: any;
|
||||
onChange: (v: any) => void;
|
||||
onRemove?: () => void;
|
||||
};
|
||||
export const ServiceEditorField: FC<ServiceEditorFieldProps> = ({
|
||||
option,
|
||||
value,
|
||||
onChange,
|
||||
onRemove,
|
||||
}) => {
|
||||
const description = option.data.description ?? "";
|
||||
const isRequired = !onRemove;
|
||||
const label = option.label;
|
||||
let example = "";
|
||||
const removeIcon = !onRemove ? (
|
||||
<></>
|
||||
) : (
|
||||
<IconButton className="service-editor--field--remove" onClick={onRemove}>
|
||||
<RemoveCircle />
|
||||
</IconButton>
|
||||
);
|
||||
|
||||
let input = (
|
||||
<InputText
|
||||
className="service-editor--field"
|
||||
label={label}
|
||||
value={value}
|
||||
onChange={onChange}
|
||||
endAdornment={removeIcon}
|
||||
required={isRequired}
|
||||
/>
|
||||
);
|
||||
if (option.type === "field") {
|
||||
example = option.data.example ? `Example: ${option.data.example}` : "";
|
||||
if (option.data.selector?.number) {
|
||||
return (
|
||||
<InputNumber
|
||||
className="service-editor--field"
|
||||
label={label}
|
||||
value={value ?? option.data.default}
|
||||
min={option.data.selector.number.min}
|
||||
max={option.data.selector.number.max}
|
||||
step={option.data.selector.number.step}
|
||||
unit={option.data.selector.number.unit_of_measurement}
|
||||
onChange={onChange}
|
||||
required={isRequired}
|
||||
placeholder={example}
|
||||
/>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<InputText
|
||||
className="service-editor--field"
|
||||
label={label}
|
||||
title={description}
|
||||
helperText={description}
|
||||
value={value ?? option.data.default}
|
||||
onChange={onChange}
|
||||
placeholder={example}
|
||||
required={isRequired}
|
||||
endAdornment={removeIcon}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return input;
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
.service-editor {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.service-editor h4 {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.service-editor--name {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.service-editor--name span {
|
||||
font-size: 0.75em;
|
||||
text-overflow: clip;
|
||||
margin-left: 0.25em;
|
||||
}
|
||||
|
||||
.service-editor--add {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.service-editor--add--list {
|
||||
flex: 1;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import React, { FC, useEffect, useState } from "react";
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { Page } from "components/Page";
|
||||
import { ServiceEditor } from ".";
|
||||
import { TypedHassService } from "haService/fieldTypes";
|
||||
|
||||
export default {
|
||||
title: "Services/ServiceEditor",
|
||||
component: ServiceEditor,
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
args: {},
|
||||
} as ComponentMeta<typeof ServiceEditor>;
|
||||
|
||||
const Template: ComponentStory<typeof ServiceEditor> = (props) => {
|
||||
return (
|
||||
<Page>
|
||||
<ServiceEditor {...props} />
|
||||
<code>{JSON.stringify(props.service)}</code>
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
|
||||
const makeExample = (service: TypedHassService) => {
|
||||
const Eg = Template.bind({});
|
||||
Eg.args = {
|
||||
...Eg,
|
||||
service,
|
||||
};
|
||||
return Eg;
|
||||
};
|
||||
|
||||
export const NumberSetValue = makeExample({
|
||||
name: "Set",
|
||||
description: "Set the value of a Number entity.",
|
||||
fields: {
|
||||
value: {
|
||||
name: "Value",
|
||||
description: "The target value the entity should be set to.",
|
||||
example: 42,
|
||||
selector: { text: null },
|
||||
},
|
||||
},
|
||||
target: { entity: { domain: "number" } },
|
||||
});
|
||||
@@ -0,0 +1,184 @@
|
||||
import "./index.css";
|
||||
|
||||
import { InputList } from "components/Inputs/InputList";
|
||||
import { cleanUpUndefined } from "components/NodeEditor/OptionManager/OptionManager";
|
||||
import { TypedHassService } from "haService/fieldTypes";
|
||||
import { FC, useState } from "react";
|
||||
import {
|
||||
ServiceEditorOption,
|
||||
splitUpOptions,
|
||||
convertFieldToOption,
|
||||
convertTargetToOption,
|
||||
} from "./options";
|
||||
import { ServiceEditorField } from "./ServiceEditorField";
|
||||
import { InputAutoComplete } from "components/Inputs/InputAutoComplete";
|
||||
|
||||
export type ServiceEditorData = {
|
||||
field: Record<string, any>;
|
||||
target: Record<string, any>;
|
||||
};
|
||||
export type ServiceEditorProps = {
|
||||
serviceId: string;
|
||||
service: TypedHassService;
|
||||
data: ServiceEditorData;
|
||||
onUpdate: (d: ServiceEditorData) => void;
|
||||
};
|
||||
export const ServiceEditor: FC<ServiceEditorProps> = ({
|
||||
serviceId,
|
||||
service,
|
||||
data,
|
||||
onUpdate,
|
||||
}) => {
|
||||
const options = splitUpOptions(service);
|
||||
const [additionalFields, setAdditionalFields] = useState<{
|
||||
field: string[];
|
||||
target: string[];
|
||||
optionalList: ServiceEditorOption[];
|
||||
}>({
|
||||
field: [],
|
||||
target: [],
|
||||
optionalList: options.additional,
|
||||
});
|
||||
|
||||
// aliases
|
||||
const hasRequired = options.required.length > 0;
|
||||
const hasOptional =
|
||||
additionalFields.field.length + additionalFields.target.length > 0;
|
||||
const hasNothing =
|
||||
!hasRequired && !hasOptional && additionalFields.optionalList.length === 0;
|
||||
|
||||
// render
|
||||
return (
|
||||
<div className="service-editor">
|
||||
<div className="service-editor--name">
|
||||
<b>
|
||||
{service.name} /<span>{serviceId}</span>
|
||||
</b>
|
||||
<span>{service.description}</span>
|
||||
</div>
|
||||
{hasNothing ? (
|
||||
<span>This Service Requires no inputs</span>
|
||||
) : (
|
||||
<>
|
||||
{options.required.map((opt) => (
|
||||
<ServiceEditorField
|
||||
key={opt.type + opt.id}
|
||||
option={opt}
|
||||
value={data[opt.type][opt.id]}
|
||||
onChange={(v) =>
|
||||
onUpdate({
|
||||
...data,
|
||||
[opt.type]: {
|
||||
...data[opt.type],
|
||||
[opt.id]: v,
|
||||
},
|
||||
})
|
||||
}
|
||||
/>
|
||||
))}
|
||||
<div className="service-editor--used">
|
||||
{additionalFields.field.map((key) => {
|
||||
const serviceData = service.fields[key];
|
||||
const option = convertFieldToOption(key, serviceData);
|
||||
return (
|
||||
<ServiceEditorField
|
||||
key={"fields-" + key}
|
||||
option={option}
|
||||
value={data.field[key] ?? ""}
|
||||
onRemove={() => {
|
||||
setAdditionalFields({
|
||||
...additionalFields,
|
||||
field: additionalFields.field.filter((k) => k !== key),
|
||||
optionalList:
|
||||
additionalFields.optionalList.concat(option),
|
||||
});
|
||||
onUpdate({
|
||||
...data,
|
||||
field: cleanUpUndefined({
|
||||
...data.field,
|
||||
[key]: undefined,
|
||||
}),
|
||||
});
|
||||
}}
|
||||
onChange={(v) =>
|
||||
onUpdate({
|
||||
...data,
|
||||
field: {
|
||||
...data.field,
|
||||
[key]: v,
|
||||
},
|
||||
})
|
||||
}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
{additionalFields.target.map((key) => {
|
||||
const targetData = (service.target as any)[key];
|
||||
const option = convertTargetToOption(key, targetData);
|
||||
return (
|
||||
<ServiceEditorField
|
||||
key={"fields-" + key}
|
||||
option={option}
|
||||
value={data.target[key] ?? ""}
|
||||
onRemove={() => {
|
||||
setAdditionalFields({
|
||||
...additionalFields,
|
||||
target: additionalFields.target.filter((k) => k !== key),
|
||||
optionalList:
|
||||
additionalFields.optionalList.concat(option),
|
||||
});
|
||||
onUpdate({
|
||||
...data,
|
||||
target: cleanUpUndefined({
|
||||
...data.target,
|
||||
[key]: undefined,
|
||||
}),
|
||||
});
|
||||
}}
|
||||
onChange={(v) =>
|
||||
onUpdate({
|
||||
...data,
|
||||
target: {
|
||||
...data.target,
|
||||
[key]: v,
|
||||
},
|
||||
})
|
||||
}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
<hr style={{ width: "90%" }} />
|
||||
{additionalFields.optionalList.length > 0 && (
|
||||
<div className="service-editor--add">
|
||||
<InputAutoComplete
|
||||
label="Additional Field"
|
||||
helperText="Include an additional none-optional fields from this list."
|
||||
className="service-editor--add--list"
|
||||
value={""}
|
||||
onChange={(id) => {
|
||||
const newField = additionalFields.optionalList.filter(
|
||||
(opt) => opt.id === id
|
||||
)[0];
|
||||
if (newField) {
|
||||
setAdditionalFields({
|
||||
...additionalFields,
|
||||
[newField.type]: additionalFields[newField.type].concat([
|
||||
newField.id,
|
||||
]),
|
||||
optionalList: additionalFields.optionalList.filter(
|
||||
(opt) =>
|
||||
!(opt.type === newField.type && opt.id === newField.id)
|
||||
),
|
||||
});
|
||||
}
|
||||
}}
|
||||
options={additionalFields.optionalList}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,52 @@
|
||||
import { BaseOption, Option } from "haService";
|
||||
import { FieldData, TargetData, TypedHassService } from "haService/fieldTypes";
|
||||
import { prettyName } from "utils/formatting";
|
||||
export type ServiceEditorOption = BaseOption<
|
||||
| {
|
||||
type: "field";
|
||||
data: {
|
||||
description: string;
|
||||
} & FieldData;
|
||||
}
|
||||
| {
|
||||
type: "target";
|
||||
data: {
|
||||
description: string;
|
||||
} & TargetData;
|
||||
}
|
||||
>;
|
||||
|
||||
export const convertFieldToOption = (id: string, data: FieldData): ServiceEditorOption => ({
|
||||
id,
|
||||
label: data.name ?? prettyName(id),
|
||||
type: "field",
|
||||
data,
|
||||
})
|
||||
|
||||
export const convertTargetToOption = (id: string, data: TargetData): ServiceEditorOption => ({
|
||||
id,
|
||||
type: "target",
|
||||
label: ((data ?? {}) as any).name ?? prettyName(id),
|
||||
data: (data as any) ?? {
|
||||
description: "",
|
||||
},
|
||||
})
|
||||
|
||||
export const splitUpOptions = (service: TypedHassService) => {
|
||||
const required: ServiceEditorOption[] = [];
|
||||
const additional: ServiceEditorOption[] = [];
|
||||
Object.keys(service.fields ?? {}).forEach((key) => {
|
||||
const serviceData = service.fields[key];
|
||||
const opt = convertFieldToOption(key, serviceData);
|
||||
if (serviceData.required) {
|
||||
required.push(opt);
|
||||
} else {
|
||||
additional.push(opt);
|
||||
}
|
||||
});
|
||||
Object.keys(service.target ?? {}).forEach((key) =>{
|
||||
const serviceData = (service.target ?? ({} as any))[key];
|
||||
additional.push(convertTargetToOption(key, serviceData))
|
||||
});
|
||||
return { required, additional };
|
||||
};
|
||||
Reference in new issue
Block a user