Move overrides to a single configuration section.
This commit is contained in:
+26
-15
@@ -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,
|
||||
})) ?? []
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user