entity filter by integration
This commit is contained in:
1 parent
34a4bd93bd
commit
dde8d76772
15 files changed
+397
-263
No files matched your search
@@ -1,98 +1,98 @@
|
||||
import "./InputTime.css";
|
||||
import { useState, useEffect, FC } from "react";
|
||||
import TextField from '@mui/material/TextField';
|
||||
import EditIcon from '@mui/icons-material/Edit';
|
||||
import TextField from "@mui/material/TextField";
|
||||
import EditIcon from "@mui/icons-material/Edit";
|
||||
import CloseIcon from "@mui/icons-material/Close";
|
||||
import IconButton from "@mui/material/IconButton"
|
||||
import IconButton from "@mui/material/IconButton";
|
||||
import { AutomationTime, AutomationTimeObject } from "types/automations/common";
|
||||
import { convertAutomationTimeToTimeObject, convertObjectToAutomationTimeString, milisecondsToObject } from "utils/time";
|
||||
import { convertStringToAutomationTimeObject, convertAutomationTimeToTimeString } from 'utils/time';
|
||||
import {
|
||||
convertAutomationTimeToTimeObject,
|
||||
convertObjectToAutomationTimeString,
|
||||
} from "utils/time";
|
||||
import { convertAutomationTimeToTimeString } from "utils/time";
|
||||
import { prettyName } from "utils/formatting";
|
||||
import FormControl from "@mui/material/FormControl";
|
||||
import InputLabel from '@mui/material/InputLabel';
|
||||
import Input from "@mui/material/Input";
|
||||
import FormHelperText from '@mui/material/FormHelperText';
|
||||
import InputLabel from "@mui/material/InputLabel";
|
||||
|
||||
// constants
|
||||
const fields: Array<keyof AutomationTimeObject> = [
|
||||
'hours',
|
||||
'minutes',
|
||||
'seconds',
|
||||
'milliseconds'
|
||||
]
|
||||
"hours",
|
||||
"minutes",
|
||||
"seconds",
|
||||
"milliseconds",
|
||||
];
|
||||
export interface Props {
|
||||
label: string;
|
||||
value?: AutomationTime;
|
||||
onChange: (v?: AutomationTimeObject) => void;
|
||||
}
|
||||
export const InputTime: FC<Props> = (props) => {
|
||||
|
||||
|
||||
// state
|
||||
const [disabled, setDisabled] = useState(!props.value)
|
||||
const [displayValue, setDisplayValue] = useState(convertAutomationTimeToTimeObject(props.value))
|
||||
const [disabled, setDisabled] = useState(!props.value);
|
||||
const [displayValue, setDisplayValue] = useState(
|
||||
convertAutomationTimeToTimeObject(props.value)
|
||||
);
|
||||
|
||||
// process display input
|
||||
const updateOne = (k: keyof AutomationTimeObject) => (v: string) => {
|
||||
if (Number(v) < 0) {
|
||||
setDisplayValue({
|
||||
...displayValue,
|
||||
[k]: 0
|
||||
})
|
||||
[k]: 0,
|
||||
});
|
||||
} else if (k === "hours") {
|
||||
setDisplayValue({
|
||||
...displayValue,
|
||||
[k]: Number(v)
|
||||
})
|
||||
[k]: Number(v),
|
||||
});
|
||||
} else if (k === "minutes") {
|
||||
let minutes = Number(v);
|
||||
let { hours = 0 } = displayValue
|
||||
let { hours = 0 } = displayValue;
|
||||
if (minutes > 59) {
|
||||
const additonalHours = Math.floor(minutes / 60)
|
||||
hours += additonalHours
|
||||
minutes = minutes - additonalHours * 60
|
||||
const additonalHours = Math.floor(minutes / 60);
|
||||
hours += additonalHours;
|
||||
minutes = minutes - additonalHours * 60;
|
||||
}
|
||||
setDisplayValue({
|
||||
...displayValue,
|
||||
minutes,
|
||||
hours,
|
||||
})
|
||||
});
|
||||
} else if (k === "seconds") {
|
||||
let seconds = Number(v);
|
||||
let { hours = 0, minutes = 0 } = displayValue
|
||||
let { hours = 0, minutes = 0 } = displayValue;
|
||||
if (seconds > 59) {
|
||||
const additonalHours = Math.floor(seconds / (60 * 60))
|
||||
hours += additonalHours
|
||||
seconds = seconds - additonalHours * 60
|
||||
const additonalHours = Math.floor(seconds / (60 * 60));
|
||||
hours += additonalHours;
|
||||
seconds = seconds - additonalHours * 60;
|
||||
}
|
||||
if (seconds > 59) {
|
||||
const additionalMinutes = Math.floor(seconds / 60)
|
||||
minutes += additionalMinutes
|
||||
seconds = seconds - additionalMinutes * 60
|
||||
const additionalMinutes = Math.floor(seconds / 60);
|
||||
minutes += additionalMinutes;
|
||||
seconds = seconds - additionalMinutes * 60;
|
||||
}
|
||||
setDisplayValue({
|
||||
...displayValue,
|
||||
minutes,
|
||||
hours,
|
||||
seconds,
|
||||
})
|
||||
});
|
||||
} else if (k === "milliseconds") {
|
||||
let milliseconds = Number(v);
|
||||
let { hours = 0, minutes = 0, seconds = 0 } = displayValue
|
||||
let { hours = 0, minutes = 0, seconds = 0 } = displayValue;
|
||||
if (milliseconds > 1000) {
|
||||
const additonalHours = Math.floor(milliseconds / (60 * 60 * 1000))
|
||||
hours += additonalHours
|
||||
milliseconds = milliseconds - additonalHours * 60 * 60 * 1000
|
||||
const additonalHours = Math.floor(milliseconds / (60 * 60 * 1000));
|
||||
hours += additonalHours;
|
||||
milliseconds = milliseconds - additonalHours * 60 * 60 * 1000;
|
||||
}
|
||||
if (milliseconds > 1000) {
|
||||
const additionalMinutes = Math.floor(milliseconds / (60 * 1000))
|
||||
minutes += additionalMinutes
|
||||
milliseconds = milliseconds - additionalMinutes * 60 * 1000
|
||||
const additionalMinutes = Math.floor(milliseconds / (60 * 1000));
|
||||
minutes += additionalMinutes;
|
||||
milliseconds = milliseconds - additionalMinutes * 60 * 1000;
|
||||
}
|
||||
if (milliseconds > 1000) {
|
||||
const additionalSeconds = Math.floor(milliseconds / 1000)
|
||||
seconds += additionalSeconds
|
||||
milliseconds = milliseconds - additionalSeconds * 1000
|
||||
const additionalSeconds = Math.floor(milliseconds / 1000);
|
||||
seconds += additionalSeconds;
|
||||
milliseconds = milliseconds - additionalSeconds * 1000;
|
||||
}
|
||||
setDisplayValue({
|
||||
...displayValue,
|
||||
@@ -100,49 +100,50 @@ export const InputTime: FC<Props> = (props) => {
|
||||
hours,
|
||||
seconds,
|
||||
milliseconds,
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// effect
|
||||
useEffect(() => {
|
||||
if (disabled && !props.value) {
|
||||
return
|
||||
return;
|
||||
}
|
||||
if (disabled) {
|
||||
props.onChange(undefined)
|
||||
props.onChange(undefined);
|
||||
} else {
|
||||
const pVal = convertAutomationTimeToTimeString(props.value);
|
||||
const cVal = convertObjectToAutomationTimeString(displayValue);
|
||||
if (pVal !== cVal) {
|
||||
props.onChange(displayValue)
|
||||
props.onChange(displayValue);
|
||||
}
|
||||
}
|
||||
}, [disabled, displayValue])
|
||||
// eslint-disable-next-line
|
||||
}, [disabled, displayValue]);
|
||||
|
||||
|
||||
return <div className="input-time">
|
||||
<InputLabel >{props.label}</InputLabel>
|
||||
<div className="input-time--inner">
|
||||
{fields.map(name =>
|
||||
<TextField
|
||||
key={name}
|
||||
variant="filled"
|
||||
label={prettyName(name)}
|
||||
defaultValue="00"
|
||||
InputProps={{
|
||||
disabled,
|
||||
type: "number",
|
||||
title: prettyName(name),
|
||||
}}
|
||||
value={displayValue[name] ?? 0}
|
||||
onChange={e => updateOne(name)(e.target.value)}
|
||||
/>
|
||||
)}
|
||||
<IconButton onClick={() => setDisabled(!disabled)}>
|
||||
{disabled ? <EditIcon /> : <CloseIcon />}
|
||||
</IconButton>
|
||||
return (
|
||||
<div className="input-time">
|
||||
<InputLabel>{props.label}</InputLabel>
|
||||
<div className="input-time--inner">
|
||||
{fields.map((name) => (
|
||||
<TextField
|
||||
key={name}
|
||||
variant="filled"
|
||||
label={prettyName(name)}
|
||||
defaultValue="00"
|
||||
InputProps={{
|
||||
disabled,
|
||||
type: "number",
|
||||
title: prettyName(name),
|
||||
}}
|
||||
value={displayValue[name] ?? 0}
|
||||
onChange={(e) => updateOne(name)(e.target.value)}
|
||||
/>
|
||||
))}
|
||||
<IconButton onClick={() => setDisabled(!disabled)}>
|
||||
{disabled ? <EditIcon /> : <CloseIcon />}
|
||||
</IconButton>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
);
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
import { FC } from "react";
|
||||
import { useHAEntities } from "haService";
|
||||
import { useHAEntities } from "haService/HAEntities";
|
||||
import {
|
||||
InputAutoComplete,
|
||||
InputAutoCompletePropsBase,
|
||||
@@ -8,13 +8,17 @@ import { prettyName } from "utils/formatting";
|
||||
|
||||
export type InputEntityProps = InputAutoCompletePropsBase & {
|
||||
restrictToDomain?: string[];
|
||||
restrictedIntegrations?: string[];
|
||||
preSelectedEntityIds?: string[];
|
||||
};
|
||||
|
||||
export const InputEntity: FC<InputEntityProps> = (props) => {
|
||||
// state
|
||||
const entities = useHAEntities();
|
||||
let options = entities.getOptions(props.restrictToDomain);
|
||||
let options = entities.getOptions(
|
||||
props.restrictToDomain,
|
||||
props.restrictedIntegrations
|
||||
);
|
||||
if (props.preSelectedEntityIds) {
|
||||
options = options.filter((opt) =>
|
||||
props.preSelectedEntityIds?.includes(entities.getID(opt))
|
||||
@@ -29,7 +33,13 @@ export const InputEntity: FC<InputEntityProps> = (props) => {
|
||||
}
|
||||
});
|
||||
}
|
||||
errors.concat(entities.validateOptions(v, props.restrictToDomain) ?? []);
|
||||
errors.concat(
|
||||
entities.validateOptions(
|
||||
v,
|
||||
props.restrictToDomain,
|
||||
props.restrictedIntegrations
|
||||
) ?? []
|
||||
);
|
||||
if (errors.length > 0) {
|
||||
return errors;
|
||||
}
|
||||
@@ -42,7 +52,11 @@ export const InputEntity: FC<InputEntityProps> = (props) => {
|
||||
options={options}
|
||||
getID={entities.getID}
|
||||
getLabel={entities.getLabel}
|
||||
groupBy={(opt) => (typeof opt !== "string" ? prettyName(opt.domain) : "")}
|
||||
groupBy={(opt) =>
|
||||
typeof opt !== "string"
|
||||
? `${prettyName(opt.integration)}/ ${prettyName(opt.domain)}`
|
||||
: ""
|
||||
}
|
||||
/>
|
||||
);
|
||||
};
|
||||
@@ -1,7 +1,6 @@
|
||||
import "./InputYaml.css";
|
||||
import * as yaml from "js-yaml";
|
||||
import { useEffect, useState, ReactNode } from "react";
|
||||
import { useDelayEffect } from "utils/useDelay";
|
||||
|
||||
import CodeMirror from "@uiw/react-codemirror";
|
||||
import FormControl from "@mui/material/FormControl";
|
||||
|
||||
Reference in new issue
Block a user