From fa5ca48472e2df57029f8fe720ff373b7070e870 Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Tue, 30 Jul 2024 12:29:00 -0400 Subject: [PATCH] refactor --- src/app/Shared/Services/api.types.ts | 4 ++++ src/app/Topology/Shared/utils.tsx | 14 ++++++-------- src/app/Topology/Toolbar/TopologyFilters.tsx | 4 ++-- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/app/Shared/Services/api.types.ts b/src/app/Shared/Services/api.types.ts index c70c37a60..99ba8f6f1 100644 --- a/src/app/Shared/Services/api.types.ts +++ b/src/app/Shared/Services/api.types.ts @@ -32,6 +32,10 @@ export const isKeyValue = (o: any): o is KeyValue => { return typeof o === 'object' && _.isEqual(new Set(['key', 'value']), new Set(Object.getOwnPropertyNames(o))); }; +export const keyValueToString = (kv: KeyValue): string => { + return `${kv.key}=${kv.value}`; +}; + export interface Metadata { labels: KeyValue[]; } diff --git a/src/app/Topology/Shared/utils.tsx b/src/app/Topology/Shared/utils.tsx index 9dab5077f..51a5e0d7f 100644 --- a/src/app/Topology/Shared/utils.tsx +++ b/src/app/Topology/Shared/utils.tsx @@ -17,7 +17,7 @@ import { JmxAuthDescription } from '@app/Shared/Components/JmxAuthDescription'; import { JmxSslDescription } from '@app/Shared/Components/JmxSslDescription'; import { TopologyFilters } from '@app/Shared/Redux/Filters/TopologyFilterSlice'; -import { NodeType, EnvironmentNode, TargetNode } from '@app/Shared/Services/api.types'; +import { NodeType, EnvironmentNode, TargetNode, keyValueToString } from '@app/Shared/Services/api.types'; import { DEFAULT_EMPTY_UNIVERSE, isTargetNode } from '@app/Shared/Services/api.utils'; import { Button, @@ -110,8 +110,7 @@ export const isGroupNodeFiltered = ( matched = matched && filter.Name.includes(groupNode.name); } if (filter.Label && filter.Label.length) { - matched = - matched && groupNode.labels.map((kv) => `${kv.key}=${kv.value}`).filter((v) => filter.Label.includes(v)).length > 0; + matched = matched && groupNode.labels.map(keyValueToString).some((v) => filter.Label.includes(v)); } return matched; }; @@ -131,16 +130,15 @@ export const isTargetNodeFiltered = ({ target }: TargetNode, filters?: TopologyF matched = matched && target.jvmId !== undefined && filters.JvmId.includes(target.jvmId); } if (filters.Label && filters.Label.length) { - matched = - matched && target.labels.map((kv) => `${kv.key}=${kv.value}`).some((v) => filters.Label.includes(v)); + matched = matched && target.labels.map(keyValueToString).some((v) => filters.Label.includes(v)); } if (filters.Annotation && filters.Annotation.length) { const annotations = target.annotations; matched = matched && - [...annotations?.cryostat, ...annotations?.platform].filter( - (kv) => filters.Annotation.includes(`${kv.key}=${kv.value}`), - ).length > 0; + [...annotations?.cryostat, ...annotations?.platform].some((kv) => + filters.Annotation.includes(keyValueToString(kv)), + ); } return matched; }; diff --git a/src/app/Topology/Toolbar/TopologyFilters.tsx b/src/app/Topology/Toolbar/TopologyFilters.tsx index 25a7e3ce6..ddb4109ba 100644 --- a/src/app/Topology/Toolbar/TopologyFilters.tsx +++ b/src/app/Topology/Toolbar/TopologyFilters.tsx @@ -25,7 +25,7 @@ import { topologyUpdateCategoryIntent, topologyUpdateCategoryTypeIntent, } from '@app/Shared/Redux/ReduxStore'; -import { EnvironmentNode, TargetNode, isKeyValue } from '@app/Shared/Services/api.types'; +import { EnvironmentNode, TargetNode, isKeyValue, keyValueToString } from '@app/Shared/Services/api.types'; import { flattenTree, getUniqueNodeTypes, isTargetNode } from '@app/Shared/Services/api.utils'; import { getDisplayFieldName } from '@app/utils/utils'; import { @@ -355,7 +355,7 @@ export const fieldValueToStrings = (value: unknown): string[] => { if (Array.isArray(value)) { if (value.length > 0 && typeof value[0] === 'object') { if (isKeyValue(value[0])) { - return value.map((o) => `${o.key}=${o.value}`); + return value.map(keyValueToString); } else { return value.map((o) => { let str = '';