import { useTheme } from '@mui/material/styles'; import React from 'react'; import { useHistory } from 'react-router-dom'; import { computeScore, countResults } from '../api/polaris'; import { usePolarisDataContext } from '../api/PolarisDataContext'; /** * Extract the cluster name from the current browser URL. * Headlamp cluster routes follow the pattern /c//... * We read window.location.pathname directly because the AppBar renders * outside the cluster route context, so useCluster() returns null and * React Router's useLocation() may not reflect the cluster prefix. */ function getClusterFromUrl(): string | null { const match = window.location.pathname.match(/\/c\/([^/]+)/); return match ? match[1] : null; } /** * App bar badge showing cluster Polaris score * Clicking navigates to the overview dashboard */ export default function AppBarScoreBadge() { const theme = useTheme(); const { data, loading } = usePolarisDataContext(); const history = useHistory(); if (loading || !data) { return null; // Graceful degradation when Polaris unavailable } const counts = countResults(data); const score = computeScore(counts); // Color based on score using theme palette const getColor = (s: number): string => { if (s >= 80) return theme.palette.success.main; if (s >= 50) return theme.palette.warning.main; return theme.palette.error.main; }; const getContrastColor = (s: number): string => { if (s >= 80) return theme.palette.success.contrastText; if (s >= 50) return theme.palette.warning.contrastText; return theme.palette.error.contrastText; }; const handleClick = () => { const cluster = getClusterFromUrl(); const prefix = cluster ? `/c/${cluster}` : ''; history.push(`${prefix}/polaris`); }; return ( ); }