Skip to content

Commit

Permalink
Merge pull request #1425 from headlamp-k8s/double-request
Browse files Browse the repository at this point in the history
Fix resource double requests
  • Loading branch information
illume authored Oct 4, 2023
2 parents 8808d91 + 64a393e commit b0b8f17
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions frontend/src/lib/k8s/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,29 +92,29 @@ export function useClustersConf(): ConfigState['clusters'] {
}

export function useCluster() {
const [cluster, setCluster] = React.useState<string | null>(null);
// Make sure we update when changing clusters.
// @todo: We need a better way to do this.
const location = useLocation();
const clusters = useClustersConf();

React.useEffect(() => {
// This function is similar to the getCluster() but uses the location
// meaning it will return the URL from whatever the router used it (which
// is more accurate than getting it from window.location like the former).
function getClusterFromLocation() {
const urlPath = location?.pathname;
const clusterURLMatch = matchPath<{ cluster?: string }>(urlPath, {
path: getClusterPrefixedPath(),
});
return (!!clusterURLMatch && clusterURLMatch.params.cluster) || null;
}
// This function is similar to the getCluster() but uses the location
// meaning it will return the URL from whatever the router used it (which
// is more accurate than getting it from window.location like the former).
function getClusterFromLocation(): string | null {
const urlPath = location?.pathname;
const clusterURLMatch = matchPath<{ cluster?: string }>(urlPath, {
path: getClusterPrefixedPath(),
});
return (!!clusterURLMatch && clusterURLMatch.params.cluster) || null;
}

const [cluster, setCluster] = React.useState<string | null>(getClusterFromLocation());

React.useEffect(() => {
const currentCluster = getClusterFromLocation();
if (cluster !== currentCluster) {
setCluster(currentCluster);
}
}, [clusters, cluster, location]);
}, [cluster, location]);

return cluster;
}
Expand Down

0 comments on commit b0b8f17

Please sign in to comment.