"use client"; import { Badge, Box, Center, Group, ScrollArea, Stack, Text, Tooltip } from "@mantine/core"; import { IconDeviceDesktop, IconListNumbers, IconTarget, IconWorld } from "@tabler/icons-react"; import { clientApi } from "@homarr/api/client"; import type { WazuhTopListEntry, WazuhTopListKind } from "@homarr/integrations/types"; import { useCurrentIntlLocale, useI18n } from "@homarr/translation/client"; import type { TablerIcon } from "@homarr/ui"; import { WidgetEmptyState } from "../common/empty-state"; import { getUsableWidgetQueryData, isInitialWidgetQueryPending } from "../common/query-state"; import type { WidgetComponentProps } from "../definition"; import { isPrivateIp, WazuhChip, WazuhLevelBadge } from "../wazuh/_shared/badges"; import { WazuhErrorState } from "../wazuh/_shared/error-state"; import { WazuhPendingState, WazuhWidgetFrame } from "../wazuh/_shared/frame"; import type { WazuhDashboardLinks } from "../wazuh/_shared/links"; import { formatWazuhCount, getWazuhSeverityForLevel, wazuhSeverityChartColors } from "../wazuh/_shared/severity"; import { useWazuhLinks } from "../wazuh/_shared/use-wazuh-links"; import classes from "../wazuh/_shared/wazuh.module.css"; const kindIcons: Record = { rules: IconListNumbers, agents: IconDeviceDesktop, sourceIps: IconWorld, mitreTactics: IconTarget, mitreTechniques: IconTarget, }; const kindColors: Record = { rules: "blue", agents: "teal", sourceIps: "cyan", mitreTactics: "violet", mitreTechniques: "violet", }; const getEntryHref = ( kind: WazuhTopListKind, entry: WazuhTopListEntry, links: WazuhDashboardLinks | null, range: string, ): string | undefined => { if (!links) return undefined; switch (kind) { case "rules": return links.events({ query: `rule.id:${entry.key}`, from: `now-${range}` }); case "agents": return entry.detail ? links.agent(entry.detail) : undefined; case "sourceIps": return links.events({ query: `data.srcip:"${entry.key}"`, from: `now-${range}` }); default: return links.mitre; } }; export default function WazuhTopListWidget({ integrationIds, options, width, height, }: WidgetComponentProps<"wazuhTopList">) { const t = useI18n("widget.wazuhTopList"); const tShared = useI18n("widget.wazuh"); const locale = useCurrentIntlLocale(); const integrationId = integrationIds[0] ?? ""; const query = clientApi.widget.wazuh.getTopList.useQuery( { integrationId, kind: options.kind, range: options.range, limit: options.limit, minLevel: options.minLevel }, { staleTime: 60_000, refetchInterval: 2 * 60_000 }, ); const result = getUsableWidgetQueryData(query); const links = useWazuhLinks(result?.dashboardUrl); const compact = height < 170 || width < 240; const title = t(`title.${options.kind}`, { range: tShared(`range.${options.range}`) }); const Icon = kindIcons[options.kind]; if (isInitialWidgetQueryPending(query)) { return ( ); } if (!result) return ; if (result.error) return ; const list = result.data; const max = Math.max(1, ...list.entries.map((entry) => entry.count)); const headerHref = options.kind === "mitreTactics" || options.kind === "mitreTechniques" ? links?.mitre : options.kind === "agents" ? links?.agents : links?.events({ from: `now-${options.range}` }); const showDetailColumn = width >= 300; const showAgentsColumn = width >= 380 && options.kind !== "agents"; return ( {formatWazuhCount(list.total, locale)} } > {list.entries.length === 0 ? (
{t(`empty.${options.kind}`)}
) : ( {list.entries.map((entry, index) => { const href = getEntryHref(options.kind, entry, links, options.range); const severity = entry.maxLevel !== null ? getWazuhSeverityForLevel(entry.maxLevel) : "low"; const share = entry.count / max; const isIp = options.kind === "sourceIps"; return ( {index + 1} {options.kind === "rules" && entry.maxLevel !== null && } {entry.label} {isIp && width >= 260 && ( {isPrivateIp(entry.key) ? t("private") : t("public")} )} {showDetailColumn && entry.detail && options.kind !== "agents" && ( {options.kind === "rules" ? `#${entry.detail}` : entry.detail} )} {showAgentsColumn && entry.agents !== null && ( {entry.agents} )} {formatWazuhCount(entry.count, locale)} {options.showBars && (
)} ); })} {list.others > 0 && height >= 200 && ( {t("others", { count: formatWazuhCount(list.others, locale) })} )} )} ); }