feat: expand advanced widget experiences
This commit is contained in:
1 parent
9437d211c8
commit
c099a8eef6
119 files changed
+3624
-844
No files matched your search
@@ -16,6 +16,7 @@ import {
|
||||
VisuallyHidden,
|
||||
} from "@mantine/core";
|
||||
import { IconBellOff, IconCircleCheck, IconFlame, IconHistory } from "@tabler/icons-react";
|
||||
import { getQueryKey } from "@trpc/react-query";
|
||||
import dayjs from "dayjs";
|
||||
import relativeTime from "dayjs/plugin/relativeTime";
|
||||
import type { LucideIcon } from "lucide-react";
|
||||
@@ -27,6 +28,10 @@ import { useScopedI18n } from "@homarr/translation/client";
|
||||
import type { WidgetComponentProps } from "../definition";
|
||||
import { IntegrationErrorIndicator } from "../common/integration-error-indicator";
|
||||
import { getUsableWidgetQueryData } from "../common/query-state";
|
||||
import { WidgetQueryErrorIndicator } from "../common/query-state-indicator";
|
||||
import { useWidgetRuntimeQueries } from "../runtime-hooks";
|
||||
import { getBeszelAlertsQueryInput } from "./display";
|
||||
import { buildBeszelSystemNameMap, getBeszelSystemName } from "./system-name-map";
|
||||
|
||||
const alertIconMap: Record<string, LucideIcon> = {
|
||||
CPU: Cpu,
|
||||
@@ -50,43 +55,64 @@ export default function BeszelAlertsWidget({
|
||||
integrationIds,
|
||||
isEditMode,
|
||||
height,
|
||||
displayMode,
|
||||
widgetRuntimeRef,
|
||||
}: WidgetComponentProps<"beszelAlerts">) {
|
||||
const t = useScopedI18n("widget.beszelAlerts");
|
||||
const isAdvanced = displayMode === "advanced";
|
||||
const alertsInput = useMemo(
|
||||
() => ({ integrationIds, includeHistory: options.showHistory, maxHistoryItems: options.maxHistoryItems }),
|
||||
[integrationIds, options.showHistory, options.maxHistoryItems],
|
||||
() =>
|
||||
getBeszelAlertsQueryInput(
|
||||
integrationIds,
|
||||
{ showHistory: options.showHistory, maxHistoryItems: options.maxHistoryItems },
|
||||
isAdvanced,
|
||||
),
|
||||
[integrationIds, options.showHistory, options.maxHistoryItems, isAdvanced],
|
||||
);
|
||||
const alertsQuery = clientApi.widget.beszel.getAlerts.useQuery(alertsInput);
|
||||
const results = getUsableWidgetQueryData(alertsQuery) ?? [];
|
||||
const runtimeQueries = useMemo(
|
||||
() => [getQueryKey(clientApi.widget.beszel.getAlerts, alertsInput, "query")],
|
||||
[alertsInput],
|
||||
);
|
||||
useWidgetRuntimeQueries(widgetRuntimeRef, runtimeQueries);
|
||||
const resultData = getUsableWidgetQueryData(alertsQuery);
|
||||
const results = useMemo(() => resultData ?? [], [resultData]);
|
||||
const { isPending } = alertsQuery;
|
||||
|
||||
const systemNameMap = useMemo(() => {
|
||||
const map: Record<string, string> = {};
|
||||
for (const r of results) {
|
||||
if (r.systemNameMap) {
|
||||
Object.assign(map, r.systemNameMap);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}, [results]);
|
||||
const systemNameMap = useMemo(() => buildBeszelSystemNameMap(results), [results]);
|
||||
const showIntegrationName = isAdvanced || results.length > 1;
|
||||
|
||||
const alerts = useMemo(
|
||||
() => results.flatMap((r) => r.alerts.map((a) => ({ ...a, _key: `${r.integrationId}:${a.id}` }))),
|
||||
() =>
|
||||
results.flatMap((r) =>
|
||||
r.alerts.map((a) => ({
|
||||
...a,
|
||||
integrationId: r.integrationId,
|
||||
integrationName: r.integrationName,
|
||||
key: `${r.integrationId}:${a.id}`,
|
||||
})),
|
||||
),
|
||||
[results],
|
||||
);
|
||||
|
||||
const history = useMemo(() => {
|
||||
const all = results.flatMap((r) => r.history.map((h) => ({ ...h, _key: `${r.integrationId}:${h.id}` })));
|
||||
return all
|
||||
.toSorted((a, b) => new Date(b.created).getTime() - new Date(a.created).getTime())
|
||||
.slice(0, options.maxHistoryItems);
|
||||
}, [results, options.maxHistoryItems]);
|
||||
const all = results.flatMap((r) =>
|
||||
r.history.map((h) => ({
|
||||
...h,
|
||||
integrationId: r.integrationId,
|
||||
integrationName: r.integrationName,
|
||||
key: `${r.integrationId}:${h.id}`,
|
||||
})),
|
||||
);
|
||||
const sorted = all.toSorted((a, b) => new Date(b.created).getTime() - new Date(a.created).getTime());
|
||||
return isAdvanced ? sorted : sorted.slice(0, options.maxHistoryItems);
|
||||
}, [results, options.maxHistoryItems, isAdvanced]);
|
||||
|
||||
const triggeredAlerts = alerts.filter((a) => a.triggered);
|
||||
const okAlerts = alerts.filter((a) => !a.triggered);
|
||||
const showOkAlerts = triggeredAlerts.length === 0 || height >= 260;
|
||||
const showHistory = options.showHistory && height >= 360;
|
||||
const showAlertDescriptions = height >= 190;
|
||||
const showOkAlerts = isAdvanced || triggeredAlerts.length === 0 || height >= 260;
|
||||
const showHistory = isAdvanced || (options.showHistory && height >= 360);
|
||||
const showAlertDescriptions = isAdvanced || height >= 190;
|
||||
|
||||
if (isPending) {
|
||||
return (
|
||||
@@ -99,7 +125,10 @@ export default function BeszelAlertsWidget({
|
||||
return (
|
||||
<Box h="100%" pos="relative">
|
||||
<Box pos="absolute" top={4} right={8} style={{ zIndex: 1 }}>
|
||||
<IntegrationErrorIndicator results={results} />
|
||||
<Group gap={0}>
|
||||
<WidgetQueryErrorIndicator error={alertsQuery.error} label={t("name")} />
|
||||
<IntegrationErrorIndicator results={results} />
|
||||
</Group>
|
||||
</Box>
|
||||
<ScrollArea h="100%" style={{ pointerEvents: isEditMode ? "none" : undefined }}>
|
||||
<Stack gap="sm" p="sm">
|
||||
@@ -124,11 +153,12 @@ export default function BeszelAlertsWidget({
|
||||
</Group>
|
||||
{triggeredAlerts.map((alert) => (
|
||||
<AlertRow
|
||||
key={alert._key}
|
||||
key={alert.key}
|
||||
name={alert.name}
|
||||
value={alert.value}
|
||||
min={alert.min}
|
||||
systemName={systemNameMap[alert.system] ?? alert.system}
|
||||
systemName={getBeszelSystemName(systemNameMap, alert.integrationId, alert.system)}
|
||||
integrationName={showIntegrationName ? alert.integrationName : undefined}
|
||||
triggered
|
||||
showDescription={showAlertDescriptions}
|
||||
/>
|
||||
@@ -148,11 +178,12 @@ export default function BeszelAlertsWidget({
|
||||
</Group>
|
||||
{okAlerts.map((alert) => (
|
||||
<AlertRow
|
||||
key={alert._key}
|
||||
key={alert.key}
|
||||
name={alert.name}
|
||||
value={alert.value}
|
||||
min={alert.min}
|
||||
systemName={systemNameMap[alert.system] ?? alert.system}
|
||||
systemName={getBeszelSystemName(systemNameMap, alert.integrationId, alert.system)}
|
||||
integrationName={showIntegrationName ? alert.integrationName : undefined}
|
||||
triggered={false}
|
||||
showDescription={showAlertDescriptions}
|
||||
/>
|
||||
@@ -171,11 +202,12 @@ export default function BeszelAlertsWidget({
|
||||
</Text>
|
||||
</Group>
|
||||
{history.map((entry) => {
|
||||
const systemName = systemNameMap[entry.system] ?? entry.system;
|
||||
const systemName = getBeszelSystemName(systemNameMap, entry.integrationId, entry.system);
|
||||
const systemLabel = showIntegrationName ? `${systemName} · ${entry.integrationName}` : systemName;
|
||||
const isResolved = !!entry.resolved;
|
||||
const HistoryIcon = alertIconMap[entry.name] ?? Server;
|
||||
return (
|
||||
<Group key={entry._key} justify="space-between" wrap="nowrap" gap="xs" pl={4}>
|
||||
<Group key={entry.key} justify="space-between" wrap="nowrap" gap="xs" pl={4}>
|
||||
<Group gap="xs" wrap="nowrap" style={{ minWidth: 0 }}>
|
||||
<Box
|
||||
w={3}
|
||||
@@ -189,7 +221,7 @@ export default function BeszelAlertsWidget({
|
||||
{entry.name}
|
||||
</Text>
|
||||
<Text size="xs" c="dimmed" truncate>
|
||||
{systemName}
|
||||
{systemLabel}
|
||||
</Text>
|
||||
</Stack>
|
||||
</Group>
|
||||
@@ -218,6 +250,7 @@ interface AlertRowProps {
|
||||
value: number;
|
||||
min: number;
|
||||
systemName: string;
|
||||
integrationName?: string;
|
||||
triggered: boolean;
|
||||
showDescription: boolean;
|
||||
}
|
||||
@@ -243,7 +276,7 @@ function formatAlertDescription(name: string, value: number, min: number): strin
|
||||
return `exceeds ${value}${suffix} over ${min} min`;
|
||||
}
|
||||
|
||||
function AlertRow({ name, value, min, systemName, triggered, showDescription }: AlertRowProps) {
|
||||
function AlertRow({ name, value, min, systemName, integrationName, triggered, showDescription }: AlertRowProps) {
|
||||
const Icon = alertIconMap[name] ?? Server;
|
||||
const description = formatAlertDescription(name, value, min);
|
||||
return (
|
||||
@@ -263,7 +296,7 @@ function AlertRow({ name, value, min, systemName, triggered, showDescription }:
|
||||
<Stack gap={0} style={{ flex: 1, minWidth: 0 }}>
|
||||
<Group gap={6} wrap="nowrap">
|
||||
<Text size="xs" fw={600} truncate>
|
||||
{systemName}
|
||||
{integrationName ? `${systemName} · ${integrationName}` : systemName}
|
||||
</Text>
|
||||
<Text size="xs" c="dimmed">
|
||||
·
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import { getBeszelAlertsQueryInput } from "./display";
|
||||
|
||||
describe("Beszel alerts query input", () => {
|
||||
test("preserves compact history settings", () => {
|
||||
expect(getBeszelAlertsQueryInput(["beszel-a"], { showHistory: false, maxHistoryItems: 10 }, false)).toEqual({
|
||||
integrationIds: ["beszel-a"],
|
||||
includeHistory: false,
|
||||
maxHistoryItems: 10,
|
||||
});
|
||||
});
|
||||
|
||||
test("requests a useful bounded history in advanced mode", () => {
|
||||
expect(getBeszelAlertsQueryInput(["beszel-a"], { showHistory: false, maxHistoryItems: 10 }, true)).toEqual({
|
||||
integrationIds: ["beszel-a"],
|
||||
includeHistory: true,
|
||||
maxHistoryItems: 50,
|
||||
});
|
||||
expect(getBeszelAlertsQueryInput(["beszel-a"], { showHistory: true, maxHistoryItems: 150 }, true)).toEqual({
|
||||
integrationIds: ["beszel-a"],
|
||||
includeHistory: true,
|
||||
maxHistoryItems: 100,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
export const BESZEL_ALERTS_ADVANCED_MIN_HISTORY_ITEMS = 50;
|
||||
const BESZEL_ALERTS_MAX_HISTORY_ITEMS = 100;
|
||||
|
||||
interface BeszelAlertsDisplayOptions {
|
||||
showHistory: boolean;
|
||||
maxHistoryItems: number;
|
||||
}
|
||||
|
||||
export const getBeszelAlertsQueryInput = (
|
||||
integrationIds: string[],
|
||||
options: BeszelAlertsDisplayOptions,
|
||||
isAdvanced: boolean,
|
||||
) => ({
|
||||
integrationIds,
|
||||
includeHistory: isAdvanced || options.showHistory,
|
||||
maxHistoryItems: isAdvanced
|
||||
? Math.min(
|
||||
BESZEL_ALERTS_MAX_HISTORY_ITEMS,
|
||||
Math.max(BESZEL_ALERTS_ADVANCED_MIN_HISTORY_ITEMS, options.maxHistoryItems),
|
||||
)
|
||||
: options.maxHistoryItems,
|
||||
});
|
||||
@@ -1,19 +1,23 @@
|
||||
import { IconBell, IconServerOff } from "@tabler/icons-react";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { createWidgetDefinition, widgetQueryInputMatches } from "../definition";
|
||||
import { createWidgetDefinition, matchesWidgetRuntimeQuery, widgetQueryInputMatches } from "../definition";
|
||||
import { optionsBuilder } from "../options";
|
||||
|
||||
export const { definition, componentLoader } = createWidgetDefinition("beszelAlerts", {
|
||||
icon: IconBell,
|
||||
supportsAdvancedFocus: false,
|
||||
supportsAdvancedFocus: true,
|
||||
queryKey: [["widget", "beszel", "getAlerts"]],
|
||||
queryMatcher: ({ input }, scope) =>
|
||||
widgetQueryInputMatches(input, {
|
||||
queryMatcher(query, scope) {
|
||||
const hasRuntimeAlertsQuery = scope.runtimeQueries.some(({ path }) => path.at(-1) === "getAlerts");
|
||||
if (hasRuntimeAlertsQuery) return matchesWidgetRuntimeQuery(query, scope);
|
||||
|
||||
return widgetQueryInputMatches(query.input, {
|
||||
integrationIds: scope.integrationIds,
|
||||
includeHistory: scope.options.showHistory,
|
||||
maxHistoryItems: scope.options.maxHistoryItems,
|
||||
}),
|
||||
});
|
||||
},
|
||||
supportedIntegrations: ["beszel", "mock"],
|
||||
integrationsRequired: true,
|
||||
createOptions() {
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import { buildBeszelSystemNameMap, getBeszelSystemName } from "./system-name-map";
|
||||
|
||||
describe("Beszel alert system names", () => {
|
||||
test("keeps identical system IDs scoped to their integration", () => {
|
||||
const systemNames = buildBeszelSystemNameMap([
|
||||
{ integrationId: "beszel-a", systemNameMap: { shared: "Alpha server" } },
|
||||
{ integrationId: "beszel-b", systemNameMap: { shared: "Beta server" } },
|
||||
]);
|
||||
|
||||
expect(getBeszelSystemName(systemNames, "beszel-a", "shared")).toBe("Alpha server");
|
||||
expect(getBeszelSystemName(systemNames, "beszel-b", "shared")).toBe("Beta server");
|
||||
});
|
||||
|
||||
test("falls back to the system ID when no name is available", () => {
|
||||
expect(getBeszelSystemName(new Map(), "beszel-a", "unknown")).toBe("unknown");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
interface BeszelSystemNamesByIntegration {
|
||||
integrationId: string;
|
||||
systemNameMap: Record<string, string>;
|
||||
}
|
||||
|
||||
export const getBeszelSystemNameKey = (integrationId: string, systemId: string) => `${integrationId}:${systemId}`;
|
||||
|
||||
export const buildBeszelSystemNameMap = (results: readonly BeszelSystemNamesByIntegration[]) => {
|
||||
const systemNames = new Map<string, string>();
|
||||
|
||||
for (const result of results) {
|
||||
for (const [systemId, systemName] of Object.entries(result.systemNameMap)) {
|
||||
systemNames.set(getBeszelSystemNameKey(result.integrationId, systemId), systemName);
|
||||
}
|
||||
}
|
||||
|
||||
return systemNames;
|
||||
};
|
||||
|
||||
export const getBeszelSystemName = (
|
||||
systemNames: ReadonlyMap<string, string>,
|
||||
integrationId: string,
|
||||
systemId: string,
|
||||
) => systemNames.get(getBeszelSystemNameKey(integrationId, systemId)) ?? systemId;
|
||||
Reference in new issue
Block a user