diff --git a/x-pack/solutions/observability/plugins/synthetics/public/apps/synthetics/components/monitors_page/hooks/use_can_use_public_loc_id.ts b/x-pack/solutions/observability/plugins/synthetics/public/apps/synthetics/components/monitors_page/hooks/use_can_use_public_loc_id.ts index 8aa1cd4416f1f..b0d8318e8c56c 100644 --- a/x-pack/solutions/observability/plugins/synthetics/public/apps/synthetics/components/monitors_page/hooks/use_can_use_public_loc_id.ts +++ b/x-pack/solutions/observability/plugins/synthetics/public/apps/synthetics/components/monitors_page/hooks/use_can_use_public_loc_id.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { useMemo } from 'react'; import { useSelector } from 'react-redux'; import { useKibana } from '@kbn/kibana-react-plugin/public'; import { selectServiceLocationsState } from '../../../state'; @@ -13,23 +14,31 @@ import { useEnablement } from '../../../hooks'; export const useCanUsePublicLocById = (configId: string) => { const { allConfigs } = useSelector(selectOverviewStatus); - const { isServiceAllowed } = useEnablement(); - const { locations: allLocations } = useSelector(selectServiceLocationsState); + const kibana = useKibana(); - const listIds = allLocations?.filter((loc) => loc.isServiceManaged).map((loc) => loc.id) ?? []; + const canUsePublicLocations = useMemo( + () => Boolean(kibana.services?.application?.capabilities.uptime.elasticManagedLocationsEnabled), + [kibana.services?.application?.capabilities.uptime.elasticManagedLocationsEnabled] + ); - const hasManagedLocation = allConfigs?.filter( - (mon) => mon.configId === configId && listIds.includes(mon.locationId) + const managedLocationIds = useMemo( + () => new Set(allLocations?.filter((loc) => loc.isServiceManaged).map((loc) => loc.id) ?? []), + [allLocations] ); - const canUsePublicLocations = - useKibana().services?.application?.capabilities.uptime.elasticManagedLocationsEnabled ?? true; + const hasManagedLocation = useMemo( + () => + allConfigs?.some( + (mon) => mon.configId === configId && managedLocationIds.has(mon.locationId) + ) ?? false, + [allConfigs, configId, managedLocationIds] + ); if (!isServiceAllowed) { return false; } - return hasManagedLocation ? !!canUsePublicLocations : true; + return hasManagedLocation ? canUsePublicLocations : true; };