"use client"; import React from "react"; import { Box, Center, LoadingOverlay, Overlay, Stack, Text, ThemeIcon, UnstyledButton, VisuallyHidden, } from "@mantine/core"; import { useDisclosure, useTimeout } from "@mantine/hooks"; import { IconAutomation, IconCheck } from "@tabler/icons-react"; import { clientApi } from "@homarr/api/client"; import { useIntegrationsWithInteractAccess } from "@homarr/auth/client"; 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, integrationIds, isEditMode, width, height, displayMode, }: WidgetComponentProps<"smartHome-executeAutomation">) { const integrationId = integrationIds[0]; const canInteract = useIntegrationsWithInteractAccess().some(({ id }) => id === integrationId); const [lastExecutedAt, setLastExecutedAt] = React.useState(null); const [isShowSuccess, { open: showSuccess, close: closeSuccess }] = useDisclosure(); const { start } = useTimeout(() => { closeSuccess(); }, 1000); const { mutateAsync, isPending, error } = clientApi.widget.smartHome.executeAutomation.useMutation({ onSuccess: () => { setLastExecutedAt(new Date()); showSuccess(); start(); }, }); const handleClick = React.useCallback(async () => { if (isEditMode || isPending || !integrationId || !canInteract) { return; } try { await mutateAsync({ automationId: options.automationId, integrationId, }); } catch { // The mutation exposes the error below and remains retryable. } }, [canInteract, integrationId, isEditMode, isPending, mutateAsync, options.automationId]); const t = useI18n(); const locale = useCurrentIntlLocale(); useRegisterSpotlightContextActions( `smartHome-automation-${options.automationId}`, [ { id: options.automationId, name: t("widget.smartHome-executeAutomation.spotlightAction.run", { name: options.displayName }), icon: IconAutomation, interaction() { return { type: "javaScript", // eslint-disable-next-line no-restricted-syntax async onSelect() { await handleClick(); }, }; }, disabled: !integrationId || !canInteract || isPending, }, ], [canInteract, handleClick, integrationId, isPending, options.automationId, options.displayName], ); const isTiny = isSmartHomeTiny(width, height); return ( {isPending ? t("widget.smartHome-executeAutomation.status.running") : isShowSuccess ? t("widget.smartHome-executeAutomation.status.success") : ""} {error && ( {t("widget.smartHome-executeAutomation.error.executeFailed")} )} {isShowSuccess && (
)} {error && displayMode !== "advanced" && (
{t("widget.smartHome-executeAutomation.error.executeFailed")}
)}
{options.displayName} {displayMode === "advanced" && ( {t("widget.smartHome-executeAutomation.advanced.automationId", { id: options.automationId })} {t( canInteract ? "widget.smartHome-executeAutomation.advanced.ready" : "widget.smartHome-executeAutomation.advanced.noPermission", )} {lastExecutedAt && ( {t("widget.smartHome-executeAutomation.advanced.lastExecuted", { time: lastExecutedAt.toLocaleTimeString(locale), })} )} {error && ( {t("widget.smartHome-executeAutomation.error.executeFailed")} )} )}
); }