"use client"; import React, { useState } from "react"; import { Center, Stack, Text, UnstyledButton } from "@mantine/core"; import { clientApi } from "@homarr/api/client"; import type { WidgetComponentProps } from "../../definition"; export default function SmartHomeEntityStateWidget({ options, integrationIds, isEditMode, }: WidgetComponentProps<"smartHome-entityState">) { const [lastState, setLastState] = useState<{ entityId: string; state: string; }>(); const utils = clientApi.useUtils(); clientApi.widget.smartHome.subscribeEntityState.useSubscription( { entityId: options.entityId, }, { onData(data) { setLastState(data); }, }, ); const { mutate } = clientApi.widget.smartHome.switchEntity.useMutation({ onSettled: () => { void utils.widget.smartHome.invalidate(); }, }); const attribute = options.entityUnit.length > 0 ? " " + options.entityUnit : ""; const handleClick = React.useCallback(() => { if (isEditMode) { return; } if (!options.clickable) { return; } mutate({ entityId: options.entityId, integrationId: integrationIds[0] ?? "", }); }, []); return (
{options.displayName} {lastState?.state} {attribute}
); }