Move overrides to a single configuration section.

This commit is contained in:
Dermot Duffy
2022-01-14 21:31:16 -08:00
parent 55ca108a9b
commit ee9f22f9f7
4 changed files with 82 additions and 44 deletions
+26 -15
View File
@@ -1,5 +1,6 @@
import type {
FrigateCardCondition,
OverrideConfigurationKey,
RawFrigateCardConfig,
} from './types';
import { merge, cloneDeep } from 'lodash-es';
@@ -27,7 +28,8 @@ export function evaluateCondition(
result &&= !!state.view && condition.view.includes(state.view);
}
if (condition?.fullscreen !== undefined) {
result &&= state.fullscreen !== undefined && condition.fullscreen == state.fullscreen;
result &&=
state.fullscreen !== undefined && condition.fullscreen == state.fullscreen;
}
if (condition?.camera?.length) {
result &&= !!state.camera && condition.camera.includes(state.camera);
@@ -78,32 +80,41 @@ export function conditionStateRequestHandler(
ev.conditionState = conditionState;
}
type Overrides = {
type RawOverrides = {
conditions: FrigateCardCondition;
overrides: RawFrigateCardConfig;
}[];
export function getOverriddenConfig(
config: Readonly<RawFrigateCardConfig>,
overrides: Readonly<RawOverrides> | undefined,
conditionState?: Readonly<ConditionState>,
overrides?: Readonly<Overrides>,
): RawFrigateCardConfig {
const overridesSource =
overrides || (config['overrides'] as Readonly<Overrides> | undefined);
if (!overridesSource) {
return config;
}
const output = cloneDeep(config);
let overridden = false;
for (const override of overridesSource) {
if (evaluateCondition(override.conditions, conditionState)) {
merge(output, override.overrides);
overridden = true;
if (overrides) {
for (const override of overrides) {
if (evaluateCondition(override.conditions, conditionState)) {
merge(output, override.overrides);
overridden = true;
}
}
}
// Attempt to return the same configuration object if it has not been
// overridden (to reduce re-renders for a configuration that has not changed).
return overridden ? output : config;
}
}
export function getOverridesByKey(
overrides: Readonly<RawOverrides> | undefined,
key: OverrideConfigurationKey,
): RawOverrides {
return (
overrides
?.filter((o) => key in o.overrides)
.map((o) => ({
conditions: o.conditions,
overrides: o.overrides[key] as RawFrigateCardConfig,
})) ?? []
);
}