"use client"; import { useCallback, useMemo } from "react"; import { Box, Button, Center, Loader, Menu, ScrollArea, Select, Stack, Text } from "@mantine/core"; import { IconQuestionMark, IconServer, IconServerOff } from "@tabler/icons-react"; import { clientApi } from "@homarr/api/client"; import { useSession } from "@homarr/auth/client"; import { constructBoardPermissions } from "@homarr/auth/shared"; import { useOptionalBoard } from "@homarr/boards/context"; import { useScopedI18n } from "@homarr/translation/client"; import classes from "./component.module.css"; import type { WidgetComponentProps } from "../definition"; import type { BeszelTimePeriod } from "../beszel/_shared/chart"; import { BeszelIntegrationErrorIndicator } from "../beszel/_shared/error-indicator"; import { BeszelStatsView } from "../beszel/_shared/stats-view"; export default function BeszelSystemStatsWidget({ options, integrationIds, isEditMode, width, boardId, itemId, setOptions, }: WidgetComponentProps<"beszelSystemStats">) { const t = useScopedI18n("widget.beszelSystemStats"); const board = useOptionalBoard(); const { data: session } = useSession(); const hasChangeAccess = board ? constructBoardPermissions(board, session).hasChangeAccess : false; const { mutate: saveItemOptions } = clientApi.widget.options.saveItemOptions.useMutation(); const { data: systemsResult = [], isPending: systemsPending, error: systemsError, } = clientApi.widget.beszel.getSystems.useQuery({ integrationIds }); const systems = useMemo( () => systemsResult.flatMap((result) => result.systems.map((system) => ({ value: system.id, label: system.name }))), [systemsResult], ); const selectedSystem = options.systemId || systems[0]?.value || ""; const selectedLabel = systems.find((system) => system.value === selectedSystem)?.label; const systemExists = systems.some((system) => system.value === selectedSystem); const handleSelectSystem = useCallback( (value: string) => { setOptions({ newOptions: { systemId: value } }); if (hasChangeAccess && boardId && itemId) { saveItemOptions({ boardId, itemId, newOptions: { systemId: value } }); } }, [setOptions, hasChangeAccess, boardId, itemId, saveItemOptions], ); if (systemsError) throw systemsError; if (systemsPending) return (
); if (systems.length === 0) { return (
{t("empty.noSystems")}
); } if (!systemExists) { return (
{t("error.systemNotFound")}