Skip to content

Commit

Permalink
handle clearing focus
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Tackett <[email protected]>
  • Loading branch information
Adam Tackett committed Nov 8, 2024
1 parent 7880492 commit 9cd464a
Showing 1 changed file with 40 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export function ServiceMap({
const [isLoading, setIsLoading] = useState(true);
const [filterChange, setIsFilterChange] = useState(false);
const [focusedService, setFocusedService] = useState<string | null>(null);
const [clearFilterRequest, setClearFilterRequest] = useState(false);

const toggleButtons = [
{
Expand Down Expand Up @@ -138,6 +139,44 @@ export function ServiceMap({
},
];

const clearFilter = () => {
setFocusedService(null);
setClearFilterRequest(true);
};

useEffect(() => {
if (clearFilterRequest && focusedService === null) {
setClearFilterRequest(false);

setQuery('');
currService = '';

Check warning on line 152 in public/components/trace_analytics/components/common/plots/service_map.tsx

View workflow job for this annotation

GitHub Actions / Lint

Assignments to the 'currService' variable from inside React Hook useEffect will be lost after each render. To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property. Otherwise, you can move this variable directly inside useEffect

if (addFilter) {
addFilter({
field: 'serviceName',
operator: 'is',
value: '',
inverted: false,
disabled: true, // Disable the filter to effectively clear it
});
}

// Reset the graph to show the full view
setItems(
getServiceMapGraph(
serviceMap,
idSelected,
ticks,
undefined,
serviceMap[currService!]?.relatedServices,
false // Do not filter by the current service to show the entire graph
)
);

setInvalid(false);
}
}, [focusedService, clearFilterRequest]);

useEffect(() => {
if (items?.graph?.nodes) {
const visibleNodes = items.graph.nodes.map((node) => node.label);
Expand Down Expand Up @@ -288,27 +327,7 @@ export function ServiceMap({

const onFocus = (service: string) => {
if (service.length === 0) {
setFocusedService(null);
if (addFilter) {
addFilter({
field: 'serviceName',
operator: 'is',
value: '',
inverted: false,
disabled: true, // Disable the filter to effectively clear it
});
}
setItems(
getServiceMapGraph(
serviceMap,
idSelected,
ticks,
undefined,
serviceMap[currService!]?.relatedServices,
false // Do not filter by the current service to show the entire graph
)
);
setInvalid(false);
clearFilter();
} else if (serviceMap[service]) {
// Focus on the specified service and add a filter
setFocusedService(service);
Expand Down

0 comments on commit 9cd464a

Please sign in to comment.