fix: extract cluster from URL path in AppBar badge (useCluster returns null outside cluster routes)

useCluster() returns null when called from AppBar context because the
component renders outside the cluster route hierarchy. Parse the cluster
name from location.pathname instead, with useCluster() as fallback.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
2026-03-16 11:34:33 +00:00
parent bf3bf63db3
commit 233b93cfdf
5 changed files with 204 additions and 101 deletions
+8 -2
View File
@@ -1,7 +1,7 @@
import { K8s } from '@kinvolk/headlamp-plugin/lib';
import { useTheme } from '@mui/material/styles';
import React from 'react';
import { useHistory } from 'react-router-dom';
import { useHistory, useLocation } from 'react-router-dom';
import { computeScore, countResults } from '../api/polaris';
import { usePolarisDataContext } from '../api/PolarisDataContext';
@@ -13,7 +13,13 @@ export default function AppBarScoreBadge() {
const theme = useTheme();
const { data, loading } = usePolarisDataContext();
const history = useHistory();
const cluster = K8s.useCluster();
const location = useLocation();
const clusterFromHook = K8s.useCluster();
// useCluster() returns null in AppBar context (outside cluster routes),
// so extract cluster from the current URL path as primary source.
const clusterMatch = location.pathname.match(/^\/c\/([^/]+)/);
const cluster = clusterMatch ? clusterMatch[1] : clusterFromHook;
if (loading || !data) {
return null; // Graceful degradation when Polaris unavailable