diff --git a/frontend/src/components/App/Home/RecentClusters.tsx b/frontend/src/components/App/Home/RecentClusters.tsx index a79bcda5caf..f45c13ba854 100644 --- a/frontend/src/components/App/Home/RecentClusters.tsx +++ b/frontend/src/components/App/Home/RecentClusters.tsx @@ -86,12 +86,17 @@ export default function RecentClusters(props: RecentClustersProps) { let recentClusters: Cluster[] = []; - // If we have more than the maximum number of recent clusters allowed, we show the most - // recent ones. Otherwise, just show the clusters in the order they are received. + const clustersByName = React.useMemo(() => { + if (clusters.length <= maxRecentClusters) { + return new Map(); + } + return new Map(clusters.map(cluster => [cluster.name, cluster] as const)); + }, [clusters]); + if (clusters.length > maxRecentClusters) { // Get clusters matching the recent cluster names, if they exist still. recentClusters = recentClusterNames - .map(name => clusters.find(cluster => cluster.name === name)) + .map(name => clustersByName.get(name)) .filter(item => !!item) as Cluster[]; // See whether we need to fill with new clusters (when the recent clusters were less than the // maximum/wanted). diff --git a/frontend/src/components/cluster/Chooser.tsx b/frontend/src/components/cluster/Chooser.tsx index 535224e0bd7..e5527f3990a 100644 --- a/frontend/src/components/cluster/Chooser.tsx +++ b/frontend/src/components/cluster/Chooser.tsx @@ -191,12 +191,17 @@ function ClusterList(props: ClusterListProps) { let recentClusters: Cluster[] = []; - // If we have more than the maximum number of recent clusters allowed, we show the most - // recent ones. Otherwise, just show the clusters in the order they are received. + const clustersByName = React.useMemo(() => { + if (clusters.length <= maxRecentClusters) { + return new Map(); + } + return new Map(clusters.map(cluster => [cluster.name, cluster] as const)); + }, [clusters]); + if (clusters.length > maxRecentClusters) { // Get clusters matching the recent cluster names, if they exist still. recentClusters = recentClusterNames - .map(name => clusters.find(cluster => cluster.name === name)) + .map(name => clustersByName.get(name)) .filter(item => !!item) as Cluster[]; // See whether we need to fill with new clusters (when the recent clusters were less than the // maximum/wanted).