diff --git a/public/components/trace_analytics/components/common/plots/service_map.tsx b/public/components/trace_analytics/components/common/plots/service_map.tsx index ae53fb5f8..0442ab61f 100644 --- a/public/components/trace_analytics/components/common/plots/service_map.tsx +++ b/public/components/trace_analytics/components/common/plots/service_map.tsx @@ -123,17 +123,6 @@ export function ServiceMap({ ); }, [filters, focusedService]); - const onChangeSelectable = (value: React.SetStateAction>>) => { - // if the change is changing for the first time then callback servicemap with metrics - if (selectableValue.length === 0 && value.length !== 0) { - if (includeMetricsCallback) { - includeMetricsCallback(); - } - } - setIdSelected(value); - setSelectableValue(value); - }; - const metricOptions: Array> = [ { value: 'latency', @@ -149,6 +138,19 @@ export function ServiceMap({ }, ]; + // For the traces custom page + useEffect(() => { + if (!selectableValue || selectableValue.length === 0) { + // Set to the first option ("latency") and trigger the onChange function + const defaultValue = metricOptions[0].value; + setSelectableValue(defaultValue); // Update the state + setIdSelected(defaultValue); // Propagate the default to parent + if (includeMetricsCallback) { + includeMetricsCallback(); + } + } + }, [selectableValue, setIdSelected]); + const removeFilter = (field: string, value: string) => { if (!setFilters) return; const updatedFilters = filters.filter( @@ -504,7 +506,10 @@ export function ServiceMap({ compressed options={metricOptions} valueOfSelected={selectableValue} - onChange={(value) => onChangeSelectable(value)} + onChange={(value) => { + setSelectableValue(value); + setIdSelected(value); + }} /> )} diff --git a/public/components/trace_analytics/components/traces/services_list.tsx b/public/components/trace_analytics/components/traces/services_list.tsx index afb7737cf..9254ae942 100644 --- a/public/components/trace_analytics/components/traces/services_list.tsx +++ b/public/components/trace_analytics/components/traces/services_list.tsx @@ -21,6 +21,8 @@ interface ServicesListProps { addFilter?: (filter: FilterType) => void; filteredService: string; setFilteredService: React.Dispatch>; + filters: FilterType[]; + setFilters: (filters: FilterType[]) => void; } export const ServicesList = ({ @@ -28,11 +30,31 @@ export const ServicesList = ({ addFilter, filteredService, setFilteredService, + filters = [], + setFilters, }: ServicesListProps) => { - const [options, setOptions] = useState>([]); + const [options, setOptions] = useState>([]); + + const removeFilter = (field: string, value: string) => { + if (!setFilters) return; + const updatedFilters = filters.filter( + (filter) => !(filter.field === field && filter.value === value) + ); + setFilters(updatedFilters); + }; const nameColumnAction = (serviceName: string) => { if (!addFilter) return; + + // Check if the service is already selected + if (filteredService === serviceName) { + // Remove the filter if the service is deselected + removeFilter('serviceName', filteredService); + setFilteredService(''); // Reset the selected service + return; + } + + // Add the filter if a new service is selected addFilter({ field: 'serviceName', operator: 'is', @@ -58,14 +80,14 @@ export const ServicesList = ({ ); useEffect(() => { + // Update selectable options based on the current filtered service setOptions( - Object.keys(serviceMap).map((key) => { - return filteredService === key - ? { label: key, checked: 'on', bordered: false } - : { label: key, bordered: false }; - }) + Object.keys(serviceMap).map((key) => ({ + label: key, + checked: filteredService === key ? 'on' : undefined, + })) ); - }, [serviceMap]); + }, [serviceMap, filteredService]); return ( @@ -74,14 +96,26 @@ export const ServicesList = ({
{ - setOptions(newOptions); - nameColumnAction(newOptions.filter((option) => option.checked === 'on')[0].label); + const selectedOption = newOptions.find((option) => option.checked === 'on'); + + // Handle deselection + if (!selectedOption) { + nameColumnAction(filteredService); + setOptions(newOptions); + return; + } + + // Handle selection + if (selectedOption) { + nameColumnAction(selectedOption.label); + setOptions(newOptions); + } }} singleSelection={true} > diff --git a/public/components/trace_analytics/components/traces/traces_content.tsx b/public/components/trace_analytics/components/traces/traces_content.tsx index b809372fc..dc15ce457 100644 --- a/public/components/trace_analytics/components/traces/traces_content.tsx +++ b/public/components/trace_analytics/components/traces/traces_content.tsx @@ -301,6 +301,8 @@ export function TracesContent(props: TracesProps) {