fix: finish widget modernization audit
This commit is contained in:
1 parent
bbcfc505bc
commit
747ccdfa9e
27 files changed
+340
-115
No files matched your search
@@ -11,6 +11,8 @@ import { useRegisterSpotlightContextActions } from "@homarr/spotlight";
|
||||
import { useCurrentIntlLocale, useScopedI18n } from "@homarr/translation/client";
|
||||
|
||||
import type { WidgetComponentProps } from "../../definition";
|
||||
import { isSmartHomeTiny } from "../layout";
|
||||
import { getEntityStateLabel } from "./state";
|
||||
|
||||
export default function SmartHomeEntityStateWidget({
|
||||
options,
|
||||
@@ -95,7 +97,7 @@ export default function SmartHomeEntityStateWidget({
|
||||
|
||||
if (entityError && entity === undefined) throw entityError;
|
||||
|
||||
const isTiny = width < 128 || height < 96;
|
||||
const isTiny = isSmartHomeTiny(width, height);
|
||||
const advancedAttributes = [
|
||||
{ key: "unit" as const, value: entity?.attributes.unit_of_measurement },
|
||||
{ key: "deviceClass" as const, value: entity?.attributes.device_class },
|
||||
@@ -105,7 +107,13 @@ export default function SmartHomeEntityStateWidget({
|
||||
displayMode === "advanced" && typeof entity?.attributes.friendly_name === "string"
|
||||
? entity.attributes.friendly_name
|
||||
: options.displayName;
|
||||
const state = entity?.state ?? "—";
|
||||
const knownStates = {
|
||||
on: t("state.on"),
|
||||
off: t("state.off"),
|
||||
unavailable: t("state.unavailable"),
|
||||
unknown: t("state.unknown"),
|
||||
};
|
||||
const state = getEntityStateLabel(entity?.state, knownStates);
|
||||
|
||||
return (
|
||||
<UnstyledButton
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import { getEntityStateLabel } from "./state";
|
||||
|
||||
const labels = { on: "On", off: "Off", unavailable: "Unavailable", unknown: "Unknown" };
|
||||
|
||||
describe("getEntityStateLabel", () => {
|
||||
test("localizes known states and preserves custom states", () => {
|
||||
expect(getEntityStateLabel("on", labels)).toBe("On");
|
||||
expect(getEntityStateLabel("heating", labels)).toBe("heating");
|
||||
});
|
||||
|
||||
test.each(["__proto__", "constructor", "toString"])("preserves prototype-like state %s", (state) => {
|
||||
expect(getEntityStateLabel(state, labels)).toBe(state);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
export interface LocalizedEntityStates {
|
||||
on: string;
|
||||
off: string;
|
||||
unavailable: string;
|
||||
unknown: string;
|
||||
}
|
||||
|
||||
export const getEntityStateLabel = (state: string | undefined, labels: LocalizedEntityStates) => {
|
||||
if (!state) return "—";
|
||||
return Object.hasOwn(labels, state) ? labels[state as keyof LocalizedEntityStates] : state;
|
||||
};
|
||||
@@ -21,6 +21,7 @@ import { useRegisterSpotlightContextActions } from "@homarr/spotlight";
|
||||
import { useCurrentIntlLocale, useI18n } from "@homarr/translation/client";
|
||||
|
||||
import type { WidgetComponentProps } from "../../definition";
|
||||
import { isSmartHomeTiny } from "../layout";
|
||||
|
||||
export default function SmartHomeTriggerAutomationWidget({
|
||||
options,
|
||||
@@ -83,7 +84,7 @@ export default function SmartHomeTriggerAutomationWidget({
|
||||
[canInteract, handleClick, integrationId, isPending, options.automationId, options.displayName],
|
||||
);
|
||||
|
||||
const isTiny = width < 128 || height < 96;
|
||||
const isTiny = isSmartHomeTiny(width, height);
|
||||
|
||||
return (
|
||||
<Box pos="relative" w="100%" h="100%">
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import { isSmartHomeTiny } from "./layout";
|
||||
|
||||
describe("smart home layout", () => {
|
||||
test.each([
|
||||
{ width: 127, height: 96, expected: true },
|
||||
{ width: 128, height: 95, expected: true },
|
||||
{ width: 128, height: 96, expected: false },
|
||||
{ width: 256, height: 192, expected: false },
|
||||
])("classifies $width x $height", ({ width, height, expected }) => {
|
||||
expect(isSmartHomeTiny(width, height)).toBe(expected);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1 @@
|
||||
export const isSmartHomeTiny = (width: number, height: number) => width < 128 || height < 96;
|
||||
Reference in new issue
Block a user