feat!: release v2 (#6545)
Co-authored-by: Artur <145776561+arturict@users.noreply.github.com> Co-authored-by: arturict <arturict@users.noreply.github.com> Co-authored-by: Thomas <thomas@ajnart.fr> Co-authored-by: Afonso Jorge Ramos <afonsojorgeramos@gmail.com> Co-authored-by: Danura30082 <danura30082+github@gmail.com> Co-authored-by: Kushie <122986913+kushiemoon-dev@users.noreply.github.com> Co-authored-by: ajnart <thomas@ajnart.fr> Co-authored-by: ajnart <thomas@ajnart.dev> Co-authored-by: chenjun <chenjun@foxmail.com> Co-authored-by: Wikibear <wikibear@users.noreply.github.com> Co-authored-by: Bergasha <bergasha@gmail.com> Co-authored-by: leafyelin <43306196+leafyelin@users.noreply.github.com> Co-authored-by: Lucas Araujo <lucas.lra@gmail.com> Co-authored-by: GreenMonito <86085570+GreenMonito@users.noreply.github.com> Co-authored-by: Nerdy-Technician <roffo@nerdy-technician.social> Co-authored-by: Jun <34545896+jcstrong@users.noreply.github.com> Co-authored-by: Edmond Abraham <54119952+gitedmond@users.noreply.github.com> Co-authored-by: thrawnx <tight.bulb9384@fastmail.com> Co-authored-by: Chris <58918656+nordada@users.noreply.github.com> Co-authored-by: yi111 <153097222+Yi-111-a@users.noreply.github.com> Co-authored-by: Yanis <naamaneyanis@gmail.com> Co-authored-by: Wikibear <me@sej.de> Co-authored-by: alessiottavo <93441886+alessiottavo@users.noreply.github.com>
This commit is contained in:
3286 files changed
+284386
-139176
No files matched your search
@@ -1,50 +1,76 @@
|
||||
"use client";
|
||||
|
||||
import React from "react";
|
||||
import { Center, LoadingOverlay, Overlay, Stack, Text, ThemeIcon, UnstyledButton } from "@mantine/core";
|
||||
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 { useI18n } from "@homarr/translation/client";
|
||||
import { useCurrentIntlLocale, useI18n } from "@homarr/translation/client";
|
||||
import { zoomCompensatedSize } from "@homarr/ui";
|
||||
|
||||
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<Date | null>(null);
|
||||
const [isShowSuccess, { open: showSuccess, close: closeSuccess }] = useDisclosure();
|
||||
const utils = clientApi.useUtils();
|
||||
const { start } = useTimeout(() => {
|
||||
closeSuccess();
|
||||
}, 1000);
|
||||
|
||||
const { mutateAsync, isPending } = clientApi.widget.smartHome.executeAutomation.useMutation({
|
||||
const { mutateAsync, isPending, error } = clientApi.widget.smartHome.executeAutomation.useMutation({
|
||||
onSuccess: () => {
|
||||
void utils.widget.smartHome.entityDetails.invalidate();
|
||||
setLastExecutedAt(new Date());
|
||||
showSuccess();
|
||||
start();
|
||||
},
|
||||
});
|
||||
const handleClick = React.useCallback(async () => {
|
||||
if (isEditMode) {
|
||||
if (isEditMode || isPending || !integrationId || !canInteract) {
|
||||
return;
|
||||
}
|
||||
await mutateAsync({
|
||||
automationId: options.automationId,
|
||||
integrationId: integrationIds[0] ?? "",
|
||||
});
|
||||
}, [integrationIds, isEditMode, mutateAsync, options.automationId]);
|
||||
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 t = useI18n("widget.smartHome-executeAutomation");
|
||||
const locale = useCurrentIntlLocale();
|
||||
useRegisterSpotlightContextActions(
|
||||
`smartHome-automation-${options.automationId}`,
|
||||
[
|
||||
{
|
||||
id: options.automationId,
|
||||
name: t("widget.smartHome-executeAutomation.spotlightAction.run", { name: options.displayName }),
|
||||
name: t("spotlightAction.run", { name: options.displayName }),
|
||||
icon: IconAutomation,
|
||||
interaction() {
|
||||
return {
|
||||
@@ -55,38 +81,84 @@ export default function SmartHomeTriggerAutomationWidget({
|
||||
},
|
||||
};
|
||||
},
|
||||
disabled: !integrationId || !canInteract || isPending,
|
||||
},
|
||||
],
|
||||
[handleClick, options.automationId, options.displayName],
|
||||
[canInteract, handleClick, integrationId, isPending, options.automationId, options.displayName],
|
||||
);
|
||||
|
||||
const isTiny = width < 128;
|
||||
const isTiny = isSmartHomeTiny(width, height);
|
||||
|
||||
return (
|
||||
<UnstyledButton
|
||||
onClick={handleClick}
|
||||
style={{ cursor: !isEditMode ? "pointer" : "initial", pointerEvents: isEditMode ? "none" : undefined }}
|
||||
w="100%"
|
||||
h="100%"
|
||||
>
|
||||
{isShowSuccess && (
|
||||
<Overlay>
|
||||
<Center w="100%" h="100%">
|
||||
<ThemeIcon variant="filled" color="green" size="xl" radius="xl">
|
||||
<IconCheck style={{ width: "70%", height: "70%" }} stroke={1.5} />
|
||||
<Box pos="relative" w="100%" h="100%">
|
||||
<VisuallyHidden component="output">
|
||||
{isPending ? t("status.running") : isShowSuccess ? t("status.success") : ""}
|
||||
</VisuallyHidden>
|
||||
{error && <VisuallyHidden role="alert">{t("error.executeFailed")}</VisuallyHidden>}
|
||||
<UnstyledButton
|
||||
onClick={handleClick}
|
||||
disabled={!integrationId || !canInteract || isPending}
|
||||
aria-label={t("spotlightAction.run", { name: options.displayName })}
|
||||
style={{
|
||||
cursor: !isEditMode && integrationId && canInteract ? "pointer" : "initial",
|
||||
pointerEvents: isEditMode ? "none" : undefined,
|
||||
}}
|
||||
aria-description={error ? t("error.executeFailed") : undefined}
|
||||
w="100%"
|
||||
h="100%"
|
||||
>
|
||||
{isShowSuccess && (
|
||||
<Overlay>
|
||||
<Center w="100%" h="100%">
|
||||
<ThemeIcon variant="filled" color="green" size="xl" radius="xl">
|
||||
<IconCheck style={{ width: "70%", height: "70%" }} stroke={1.5} />
|
||||
</ThemeIcon>
|
||||
</Center>
|
||||
</Overlay>
|
||||
)}
|
||||
<LoadingOverlay visible={isPending} />
|
||||
{error && displayMode !== "advanced" && (
|
||||
<Overlay>
|
||||
<Center w="100%" h="100%" p="xs">
|
||||
<Text size="xs" c="red" ta="center" lineClamp={3}>
|
||||
{t("error.executeFailed")}
|
||||
</Text>
|
||||
</Center>
|
||||
</Overlay>
|
||||
)}
|
||||
<Center w="100%" h="100%">
|
||||
<Stack align="center" gap={isTiny ? 6 : "md"} p="xs" maw="100%">
|
||||
<ThemeIcon variant="light" size={isTiny ? "md" : "xl"} radius="xl">
|
||||
<IconAutomation style={zoomCompensatedSize(isTiny ? 14 : 24)} />
|
||||
</ThemeIcon>
|
||||
</Center>
|
||||
</Overlay>
|
||||
)}
|
||||
<LoadingOverlay visible={isPending} />
|
||||
<Center w="100%" h="100%">
|
||||
<Stack align="center" gap="md">
|
||||
<IconAutomation size={isTiny ? 16 : undefined} />
|
||||
<Text ta="center" fw="bold" fz={isTiny ? "xs" : undefined}>
|
||||
{options.displayName}
|
||||
</Text>
|
||||
</Stack>
|
||||
</Center>
|
||||
</UnstyledButton>
|
||||
<Text ta="center" fw={600} fz={isTiny ? "xs" : "sm"} lineClamp={2} maw="100%">
|
||||
{options.displayName}
|
||||
</Text>
|
||||
{displayMode === "advanced" && (
|
||||
<Stack gap={2} align="center">
|
||||
<Text ta="center" size="xs" c="dimmed">
|
||||
{t("advanced.automationId", { id: options.automationId })}
|
||||
</Text>
|
||||
<Text ta="center" size="xs" c={canInteract ? "dimmed" : "orange"}>
|
||||
{t(canInteract ? "advanced.ready" : "advanced.noPermission")}
|
||||
</Text>
|
||||
{lastExecutedAt && (
|
||||
<Text ta="center" size="xs" c="dimmed">
|
||||
{t("advanced.lastExecuted", {
|
||||
time: lastExecutedAt.toLocaleTimeString(locale),
|
||||
})}
|
||||
</Text>
|
||||
)}
|
||||
{error && (
|
||||
<Text ta="center" size="xs" c="red">
|
||||
{t("error.executeFailed")}
|
||||
</Text>
|
||||
)}
|
||||
</Stack>
|
||||
)}
|
||||
</Stack>
|
||||
</Center>
|
||||
</UnstyledButton>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user