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
@@ -0,0 +1,25 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { getUpsDevices } from "./component";
|
||||
|
||||
describe("UPS source ownership", () => {
|
||||
it("uses integration-scoped keys for overlapping device ids", () => {
|
||||
const devices = getUpsDevices([
|
||||
{
|
||||
integrationId: "ups-a",
|
||||
integrationName: "UPS A",
|
||||
summaries: [{ id: "same", status: "online" as const }],
|
||||
},
|
||||
{
|
||||
integrationId: "ups-b",
|
||||
integrationName: "UPS B",
|
||||
summaries: [{ id: "same", status: "online" as const }],
|
||||
},
|
||||
]);
|
||||
|
||||
expect(devices.map(({ key, integrationName }) => ({ key, integrationName }))).toEqual([
|
||||
{ key: "ups-a:same", integrationName: "UPS A" },
|
||||
{ key: "ups-b:same", integrationName: "UPS B" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -1,15 +1,29 @@
|
||||
"use client";
|
||||
|
||||
import { Badge, Box, Card, Group, Progress, RingProgress, ScrollArea, Stack, Text } from "@mantine/core";
|
||||
import {
|
||||
Badge,
|
||||
Box,
|
||||
Card,
|
||||
Group,
|
||||
Progress,
|
||||
RingProgress,
|
||||
ScrollArea,
|
||||
SimpleGrid,
|
||||
Stack,
|
||||
Text,
|
||||
VisuallyHidden,
|
||||
} from "@mantine/core";
|
||||
|
||||
import { clientApi } from "@homarr/api/client";
|
||||
import { formatDuration } from "@homarr/common";
|
||||
import type { UpsStatus, UpsSummary } from "@homarr/integrations/types";
|
||||
import type { ScopedTranslationFunction } from "@homarr/translation";
|
||||
import { useScopedI18n } from "@homarr/translation/client";
|
||||
import { useI18n } from "@homarr/translation/client";
|
||||
|
||||
import { WidgetEmptyState } from "../common/empty-state";
|
||||
import { IntegrationErrorIndicator } from "../common/integration-error-indicator";
|
||||
import type { WidgetComponentProps } from "../definition";
|
||||
import { getUsableWidgetQueryData } from "../common/query-state";
|
||||
import { NoIntegrationSelectedError } from "../errors/no-integration-selected";
|
||||
|
||||
const statusColors: Record<UpsStatus, string> = {
|
||||
@@ -20,34 +34,68 @@ const statusColors: Record<UpsStatus, string> = {
|
||||
unknown: "gray",
|
||||
};
|
||||
|
||||
const neutralSurfaceBackground = "rgb(from var(--mantine-color-default-hover) r g b / calc(var(--opacity, 1) * 0.12))";
|
||||
const neutralSurfaceBorder = "rgb(from var(--mantine-color-default-border) r g b / calc(var(--opacity, 1) * 0.45))";
|
||||
|
||||
type UpsLayout = "mini" | "compact" | "full";
|
||||
|
||||
export default function UpsWidget({ options, integrationIds, width }: WidgetComponentProps<"ups">) {
|
||||
export default function UpsWidget({
|
||||
options,
|
||||
integrationIds,
|
||||
width,
|
||||
height,
|
||||
displayMode = "compact",
|
||||
displayScale = 1,
|
||||
}: WidgetComponentProps<"ups">) {
|
||||
if (integrationIds.length === 0) {
|
||||
throw new NoIntegrationSelectedError();
|
||||
}
|
||||
|
||||
return <UpsContent integrationIds={integrationIds} options={options} width={width} />;
|
||||
let responsiveWidth = width;
|
||||
let responsiveHeight = height;
|
||||
if (displayMode !== "advanced") {
|
||||
responsiveWidth *= displayScale;
|
||||
responsiveHeight *= displayScale;
|
||||
}
|
||||
|
||||
return (
|
||||
<UpsContent
|
||||
integrationIds={integrationIds}
|
||||
options={options}
|
||||
width={responsiveWidth}
|
||||
height={responsiveHeight}
|
||||
displayMode={displayMode}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
interface UpsContentProps {
|
||||
integrationIds: string[];
|
||||
options: WidgetComponentProps<"ups">["options"];
|
||||
width: number;
|
||||
height: number;
|
||||
displayMode: "compact" | "advanced";
|
||||
}
|
||||
|
||||
function UpsContent({ integrationIds, options, width }: UpsContentProps) {
|
||||
const t = useScopedI18n("widget.ups");
|
||||
const { data } = clientApi.widget.ups.getSummaries.useQuery({ integrationIds });
|
||||
function UpsContent({ integrationIds, options, width, height, displayMode }: UpsContentProps) {
|
||||
const t = useI18n("widget.ups");
|
||||
const summariesQuery = clientApi.widget.ups.getSummaries.useQuery({ integrationIds });
|
||||
const data = getUsableWidgetQueryData(summariesQuery);
|
||||
|
||||
if (!data) return <WidgetEmptyState />;
|
||||
|
||||
const devices = data.flatMap((instance) =>
|
||||
instance.summaries.map((summary) => ({ key: `${instance.integrationId}:${summary.id}`, summary })),
|
||||
);
|
||||
const devices = getUpsDevices(data);
|
||||
const showSource = displayMode === "advanced" || data.length > 1;
|
||||
|
||||
if (devices.length === 0) {
|
||||
return <WidgetEmptyState />;
|
||||
return (
|
||||
<Box h="100%" pos="relative">
|
||||
<Group pos="absolute" top={4} right={8} gap={0} style={{ zIndex: 2 }}>
|
||||
<IntegrationErrorIndicator results={data} />
|
||||
</Group>
|
||||
<WidgetEmptyState />
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
// Pick a layout from the available width so the widget stays useful at any size. The surrounding
|
||||
@@ -55,28 +103,81 @@ function UpsContent({ integrationIds, options, width }: UpsContentProps) {
|
||||
// - full: ring beside a column of stats (runtime, load, voltage)
|
||||
// - compact (~2 columns): ring beside the name and a status badge
|
||||
// - mini (~1 column): a text badge no longer fits, so status becomes a colour dot next to the name
|
||||
const layout: UpsLayout = width < 150 ? "mini" : width < 256 ? "compact" : "full";
|
||||
const layout: UpsLayout =
|
||||
displayMode === "advanced" ? "full" : width < 150 || height < 110 ? "mini" : width < 256 ? "compact" : "full";
|
||||
|
||||
const cards = devices.map(({ key, integrationName, summary }) => (
|
||||
<UpsDeviceCard
|
||||
key={key}
|
||||
summary={summary}
|
||||
sourceName={showSource ? integrationName : undefined}
|
||||
options={options}
|
||||
layout={layout}
|
||||
advanced={displayMode === "advanced"}
|
||||
t={t}
|
||||
/>
|
||||
));
|
||||
|
||||
return (
|
||||
<ScrollArea h="100%">
|
||||
<Stack gap="xs" p="xs">
|
||||
{devices.map(({ key, summary }) => (
|
||||
<UpsDeviceCard key={key} summary={summary} options={options} layout={layout} t={t} />
|
||||
))}
|
||||
</Stack>
|
||||
</ScrollArea>
|
||||
<Box h="100%" pos="relative">
|
||||
<Group pos="absolute" top={4} right={8} gap={0} style={{ zIndex: 2 }}>
|
||||
<IntegrationErrorIndicator results={data} />
|
||||
</Group>
|
||||
<ScrollArea h="100%">
|
||||
{displayMode === "advanced" ? (
|
||||
<SimpleGrid cols={width >= 760 ? 2 : 1} spacing="md" p="md">
|
||||
{cards}
|
||||
</SimpleGrid>
|
||||
) : (
|
||||
<SimpleGrid cols={Math.min(cards.length, Math.max(1, Math.floor(width / 360)))} spacing="xs" p="xs">
|
||||
{cards}
|
||||
</SimpleGrid>
|
||||
)}
|
||||
</ScrollArea>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
const statusPriority: Record<UpsStatus, number> = {
|
||||
lowBattery: 0,
|
||||
onBattery: 1,
|
||||
charging: 2,
|
||||
unknown: 3,
|
||||
online: 4,
|
||||
};
|
||||
|
||||
export const getUpsDevices = <T extends Pick<UpsSummary, "id" | "status">>(
|
||||
instances: readonly {
|
||||
integrationId: string;
|
||||
integrationName: string;
|
||||
summaries: readonly T[];
|
||||
}[],
|
||||
) =>
|
||||
instances
|
||||
.flatMap(({ integrationId, integrationName, summaries }) =>
|
||||
summaries.map((summary) => ({
|
||||
key: `${integrationId}:${summary.id}`,
|
||||
integrationId,
|
||||
integrationName,
|
||||
summary,
|
||||
})),
|
||||
)
|
||||
.toSorted((left, right) => statusPriority[left.summary.status] - statusPriority[right.summary.status]);
|
||||
|
||||
interface UpsDeviceCardProps {
|
||||
summary: UpsSummary;
|
||||
sourceName?: string;
|
||||
options: WidgetComponentProps<"ups">["options"];
|
||||
layout: UpsLayout;
|
||||
advanced: boolean;
|
||||
t: ScopedTranslationFunction<"widget.ups">;
|
||||
}
|
||||
|
||||
function UpsDeviceCard({ summary, options, layout, t }: UpsDeviceCardProps) {
|
||||
const showRing = options.showBattery && summary.batteryCharge !== null;
|
||||
function UpsDeviceCard({ summary, sourceName, options, layout, advanced, t }: UpsDeviceCardProps) {
|
||||
const showBattery = advanced || options.showBattery;
|
||||
const showLoad = advanced || options.showLoad;
|
||||
const showVoltage = advanced || options.showVoltage;
|
||||
const showRing = showBattery && summary.batteryCharge !== null;
|
||||
const statusBadge = (
|
||||
<Badge
|
||||
color={statusColors[summary.status]}
|
||||
@@ -90,27 +191,35 @@ function UpsDeviceCard({ summary, options, layout, t }: UpsDeviceCardProps) {
|
||||
|
||||
if (layout === "mini") {
|
||||
return (
|
||||
<Card p={6} radius="md">
|
||||
<Card p={6} radius="md" bg="transparent">
|
||||
<Group gap={4} wrap="nowrap" justify="center" mb={showRing ? 4 : 0}>
|
||||
<Box
|
||||
w={8}
|
||||
h={8}
|
||||
aria-hidden="true"
|
||||
style={{
|
||||
borderRadius: "50%",
|
||||
backgroundColor: `var(--mantine-color-${statusColors[summary.status]}-6)`,
|
||||
flexShrink: 0,
|
||||
}}
|
||||
/>
|
||||
<VisuallyHidden>{t(`status.${summary.status}`)}</VisuallyHidden>
|
||||
<Text fw={600} size="xs" truncate>
|
||||
{summary.name}
|
||||
</Text>
|
||||
</Group>
|
||||
{sourceName && (
|
||||
<Text size="10px" c="dimmed" ta="center" truncate>
|
||||
{sourceName}
|
||||
</Text>
|
||||
)}
|
||||
{showRing && summary.batteryCharge !== null && (
|
||||
<Group justify="center">
|
||||
<RingProgress
|
||||
size={44}
|
||||
thickness={4}
|
||||
roundCaps
|
||||
rootColor={neutralSurfaceBorder}
|
||||
sections={[{ value: clampPercent(summary.batteryCharge), color: getChargeColor(summary.batteryCharge) }]}
|
||||
label={
|
||||
<Text ta="center" size="10px" fw={700}>
|
||||
@@ -126,13 +235,14 @@ function UpsDeviceCard({ summary, options, layout, t }: UpsDeviceCardProps) {
|
||||
|
||||
if (layout === "compact") {
|
||||
return (
|
||||
<Card p={6} radius="md">
|
||||
<Card p={6} radius="md" bg="transparent">
|
||||
<Group gap={8} wrap="nowrap" align="center">
|
||||
{showRing && summary.batteryCharge !== null && (
|
||||
<RingProgress
|
||||
size={40}
|
||||
thickness={4}
|
||||
roundCaps
|
||||
rootColor={neutralSurfaceBorder}
|
||||
sections={[{ value: clampPercent(summary.batteryCharge), color: getChargeColor(summary.batteryCharge) }]}
|
||||
label={
|
||||
<Text ta="center" size="9px" fw={700}>
|
||||
@@ -145,6 +255,11 @@ function UpsDeviceCard({ summary, options, layout, t }: UpsDeviceCardProps) {
|
||||
<Text fw={600} size="xs" truncate>
|
||||
{summary.name}
|
||||
</Text>
|
||||
{sourceName && (
|
||||
<Text size="10px" c="dimmed" truncate>
|
||||
{sourceName}
|
||||
</Text>
|
||||
)}
|
||||
{statusBadge}
|
||||
</Stack>
|
||||
</Group>
|
||||
@@ -153,11 +268,18 @@ function UpsDeviceCard({ summary, options, layout, t }: UpsDeviceCardProps) {
|
||||
}
|
||||
|
||||
return (
|
||||
<Card p="xs" radius="md">
|
||||
<Card p="xs" radius="md" bg="transparent">
|
||||
<Group justify="space-between" wrap="nowrap" mb={4}>
|
||||
<Text fw={600} size="sm" truncate>
|
||||
{summary.name}
|
||||
</Text>
|
||||
<Stack gap={0} style={{ minWidth: 0 }}>
|
||||
<Text fw={600} size="sm" truncate>
|
||||
{summary.name}
|
||||
</Text>
|
||||
{sourceName && (
|
||||
<Text size="xs" c="dimmed" truncate>
|
||||
{sourceName}
|
||||
</Text>
|
||||
)}
|
||||
</Stack>
|
||||
{statusBadge}
|
||||
</Group>
|
||||
|
||||
@@ -167,6 +289,7 @@ function UpsDeviceCard({ summary, options, layout, t }: UpsDeviceCardProps) {
|
||||
size={68}
|
||||
thickness={6}
|
||||
roundCaps
|
||||
rootColor={neutralSurfaceBorder}
|
||||
sections={[{ value: clampPercent(summary.batteryCharge), color: getChargeColor(summary.batteryCharge) }]}
|
||||
label={
|
||||
<Text ta="center" size="xs" fw={700}>
|
||||
@@ -177,11 +300,11 @@ function UpsDeviceCard({ summary, options, layout, t }: UpsDeviceCardProps) {
|
||||
)}
|
||||
|
||||
<Stack gap={2} style={{ flex: 1, minWidth: 0 }}>
|
||||
{options.showBattery && summary.batteryRuntime !== null && (
|
||||
{showBattery && summary.batteryRuntime !== null && (
|
||||
<StatRow label={t("field.runtime")} value={formatDuration(summary.batteryRuntime * 1000)} />
|
||||
)}
|
||||
|
||||
{options.showLoad && summary.load !== null && (
|
||||
{showLoad && summary.load !== null && (
|
||||
<Box>
|
||||
<Group justify="space-between" gap="xs" mb={2}>
|
||||
<Text size="xs" c="dimmed">
|
||||
@@ -189,11 +312,16 @@ function UpsDeviceCard({ summary, options, layout, t }: UpsDeviceCardProps) {
|
||||
</Text>
|
||||
<Text size="xs">{Math.round(summary.load)}%</Text>
|
||||
</Group>
|
||||
<Progress value={clampPercent(summary.load)} size="sm" color={getLoadColor(summary.load)} />
|
||||
<Progress
|
||||
value={clampPercent(summary.load)}
|
||||
size="sm"
|
||||
color={getLoadColor(summary.load)}
|
||||
styles={{ root: { backgroundColor: neutralSurfaceBackground } }}
|
||||
/>
|
||||
</Box>
|
||||
)}
|
||||
|
||||
{options.showVoltage && (summary.inputVoltage !== null || summary.outputVoltage !== null) && (
|
||||
{showVoltage && (summary.inputVoltage !== null || summary.outputVoltage !== null) && (
|
||||
<Group gap="md">
|
||||
{summary.inputVoltage !== null && (
|
||||
<StatRow label={t("field.input")} value={`${Math.round(summary.inputVoltage)} V`} />
|
||||
@@ -205,6 +333,20 @@ function UpsDeviceCard({ summary, options, layout, t }: UpsDeviceCardProps) {
|
||||
)}
|
||||
</Stack>
|
||||
</Group>
|
||||
{advanced && (
|
||||
<SimpleGrid cols={2} spacing={4} mt="sm">
|
||||
{summary.batteryVoltage !== null && (
|
||||
<StatRow label={t("field.batteryVoltage")} value={`${summary.batteryVoltage} V`} />
|
||||
)}
|
||||
{summary.power !== null && <StatRow label={t("field.power")} value={`${summary.power} W`} />}
|
||||
{summary.temperature !== null && (
|
||||
<StatRow label={t("field.temperature")} value={`${summary.temperature} °C`} />
|
||||
)}
|
||||
{summary.manufacturer && <StatRow label={t("field.manufacturer")} value={summary.manufacturer} />}
|
||||
{summary.model && <StatRow label={t("field.model")} value={summary.model} />}
|
||||
{summary.serial && <StatRow label={t("field.serial")} value={summary.serial} />}
|
||||
</SimpleGrid>
|
||||
)}
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { IconBatteryCharging, IconBatteryOff } from "@tabler/icons-react";
|
||||
|
||||
import { getIntegrationKindsByCategory } from "@homarr/definitions";
|
||||
import { getWidgetIntegrationConfig } from "@homarr/definitions";
|
||||
|
||||
import { createWidgetDefinition } from "../definition";
|
||||
import { optionsBuilder } from "../options";
|
||||
|
||||
export const { definition, componentLoader } = createWidgetDefinition("ups", {
|
||||
icon: IconBatteryCharging,
|
||||
supportsAdvancedFocus: true,
|
||||
createOptions() {
|
||||
return optionsBuilder.from((factory) => ({
|
||||
showBattery: factory.switch({
|
||||
@@ -20,7 +21,7 @@ export const { definition, componentLoader } = createWidgetDefinition("ups", {
|
||||
}),
|
||||
}));
|
||||
},
|
||||
supportedIntegrations: getIntegrationKindsByCategory("ups"),
|
||||
...getWidgetIntegrationConfig("ups"),
|
||||
errors: {
|
||||
INTERNAL_SERVER_ERROR: {
|
||||
icon: IconBatteryOff,
|
||||
|
||||
Reference in new issue
Block a user