"use client"; import { useCallback, useMemo } from "react"; import { Box, Button, Center, Loader, Menu, ScrollArea, SegmentedControl, 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 { showErrorNotification } from "@homarr/notifications"; import { useScopedI18n } from "@homarr/translation/client"; import classes from "./component.module.css"; import type { WidgetComponentProps } from "../definition"; import { setWidgetRuntimeQueries } from "../definition"; import type { BeszelTimePeriod } from "../beszel/_shared/chart"; import { IntegrationErrorIndicator } from "../common/integration-error-indicator"; import { BeszelStatsView } from "../beszel/_shared/stats-view"; import { createBeszelSystemChoices, resolveBeszelSystemChoice } from "./selection"; export default function BeszelSystemStatsWidget({ options, integrationIds, isEditMode, width, height, displayMode = "compact", boardId, itemId, setOptions, widgetStateRef, }: 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({ onError: () => showErrorNotification({ title: t("error.selectionSaveTitle"), message: t("error.selectionSaveMessage"), }), }); const { data: systemsResult = [], isPending: systemsPending, error: systemsError, } = clientApi.widget.beszel.getSystems.useQuery({ integrationIds }); const systems = useMemo(() => createBeszelSystemChoices(systemsResult), [systemsResult]); const selectedSystem = resolveBeszelSystemChoice(systems, options.systemId); const selectedValue = selectedSystem?.value ?? ""; const selectedLabel = selectedSystem?.label; const systemExists = selectedSystem !== undefined; const includeDocker = (displayMode === "advanced" || height >= 380) && (options.showDockerCpu || options.showDockerMemory || options.showDockerNetwork); setWidgetRuntimeQueries( widgetStateRef, selectedSystem && options.timePeriod !== "1m" ? [ { path: ["widget", "beszel", "getSystemStats"], input: { integrationIds: [selectedSystem.integrationId], systemId: selectedSystem.systemId, timePeriod: options.timePeriod, includeDocker, }, }, ] : [], ); const handleSelectSystem = useCallback( (value: string) => { setOptions({ newOptions: { systemId: value } }); if (hasChangeAccess && boardId && itemId) { saveItemOptions({ boardId, itemId, newOptions: { systemId: value } }); } }, [setOptions, hasChangeAccess, boardId, itemId, saveItemOptions], ); const handleTimePeriod = useCallback( (value: string) => { setOptions({ newOptions: { timePeriod: value as BeszelTimePeriod } }); if (hasChangeAccess && boardId && itemId) { saveItemOptions({ boardId, itemId, newOptions: { timePeriod: value } }); } }, [boardId, hasChangeAccess, itemId, saveItemOptions, setOptions], ); if (systemsError) throw systemsError; if (systemsPending) return (