1
- import React from "react" ;
1
+ import React , { useCallback , useEffect } from "react" ;
2
2
import {
3
3
AlertFacetsProps ,
4
4
FacetValue ,
@@ -12,11 +12,10 @@ import {
12
12
import { useLocalStorage } from "utils/hooks/useLocalStorage" ;
13
13
import { AlertDto } from "./models" ;
14
14
import {
15
- DynamicFacet ,
16
15
DynamicFacetWrapper ,
17
16
AddFacetModal ,
18
17
} from "./alert-table-facet-dynamic" ;
19
- import { PlusIcon , TrashIcon } from "@heroicons/react/24/outline" ;
18
+ import { PlusIcon } from "@heroicons/react/24/outline" ;
20
19
21
20
export const AlertFacets : React . FC < AlertFacetsProps > = ( {
22
21
alerts,
@@ -76,88 +75,92 @@ export const AlertFacets: React.FC<AlertFacetsProps> = ({
76
75
setFacetFilters ( newFilters ) ;
77
76
} ;
78
77
79
- const getFacetValues = ( key : keyof AlertDto | string ) : FacetValue [ ] => {
80
- const filteredAlerts = getFilteredAlertsForFacet (
81
- alerts ,
82
- facetFilters ,
83
- key ,
84
- timeRange
85
- ) ;
86
- const valueMap = new Map < string , number > ( ) ;
87
- let nullCount = 0 ;
78
+ const getFacetValues = useCallback (
79
+ ( key : keyof AlertDto | string ) : FacetValue [ ] => {
80
+ const filteredAlerts = getFilteredAlertsForFacet (
81
+ alerts ,
82
+ facetFilters ,
83
+ key ,
84
+ timeRange
85
+ ) ;
86
+ const valueMap = new Map < string , number > ( ) ;
87
+ let nullCount = 0 ;
88
88
89
- filteredAlerts . forEach ( ( alert ) => {
90
- let value ;
89
+ filteredAlerts . forEach ( ( alert ) => {
90
+ let value ;
91
91
92
- // Handle nested keys like "labels.host"
93
- if ( typeof key === "string" && key . includes ( "." ) ) {
94
- const [ parentKey , childKey ] = key . split ( "." ) ;
95
- const parentValue = alert [ parentKey as keyof AlertDto ] ;
92
+ // Handle nested keys like "labels.host"
93
+ if ( typeof key === "string" && key . includes ( "." ) ) {
94
+ const [ parentKey , childKey ] = key . split ( "." ) ;
95
+ const parentValue = alert [ parentKey as keyof AlertDto ] ;
96
96
97
- if (
98
- typeof parentValue === "object" &&
99
- parentValue !== null &&
100
- ! Array . isArray ( parentValue ) &&
101
- ! ( parentValue instanceof Date )
102
- ) {
103
- value = ( parentValue as Record < string , unknown > ) [ childKey ] ;
97
+ if (
98
+ typeof parentValue === "object" &&
99
+ parentValue !== null &&
100
+ ! Array . isArray ( parentValue ) &&
101
+ ! ( parentValue instanceof Date )
102
+ ) {
103
+ value = ( parentValue as Record < string , unknown > ) [ childKey ] ;
104
+ } else {
105
+ value = undefined ;
106
+ }
104
107
} else {
105
- value = undefined ;
108
+ value = alert [ key as keyof AlertDto ] ;
106
109
}
107
- } else {
108
- value = alert [ key as keyof AlertDto ] ;
109
- }
110
110
111
- if ( Array . isArray ( value ) ) {
112
- if ( value . length === 0 ) {
113
- nullCount ++ ;
111
+ if ( Array . isArray ( value ) ) {
112
+ if ( value . length === 0 ) {
113
+ nullCount ++ ;
114
+ } else {
115
+ value . forEach ( ( v ) => {
116
+ valueMap . set ( v , ( valueMap . get ( v ) || 0 ) + 1 ) ;
117
+ } ) ;
118
+ }
119
+ } else if ( value !== undefined && value !== null ) {
120
+ const strValue = String ( value ) ;
121
+ valueMap . set ( strValue , ( valueMap . get ( strValue ) || 0 ) + 1 ) ;
114
122
} else {
115
- value . forEach ( ( v ) => {
116
- valueMap . set ( v , ( valueMap . get ( v ) || 0 ) + 1 ) ;
117
- } ) ;
123
+ nullCount ++ ;
118
124
}
119
- } else if ( value !== undefined && value !== null ) {
120
- const strValue = String ( value ) ;
121
- valueMap . set ( strValue , ( valueMap . get ( strValue ) || 0 ) + 1 ) ;
122
- } else {
123
- nullCount ++ ;
124
- }
125
- } ) ;
126
-
127
- let values = Array . from ( valueMap . entries ( ) ) . map ( ( [ label , count ] ) => ( {
128
- label,
129
- count,
130
- isSelected :
131
- facetFilters [ key ] ?. includes ( label ) || ! facetFilters [ key ] ?. length ,
132
- } ) ) ;
125
+ } ) ;
133
126
134
- if ( [ "assignee" , "incident" ] . includes ( key as string ) && nullCount > 0 ) {
135
- values . push ( {
136
- label : "n/a" ,
137
- count : nullCount ,
127
+ let values = Array . from ( valueMap . entries ( ) ) . map ( ( [ label , count ] ) => ( {
128
+ label,
129
+ count,
138
130
isSelected :
139
- facetFilters [ key ] ?. includes ( "n/a" ) || ! facetFilters [ key ] ?. length ,
140
- } ) ;
141
- }
131
+ facetFilters [ key ] ?. includes ( label ) || ! facetFilters [ key ] ?. length ,
132
+ } ) ) ;
142
133
143
- if ( key === "severity" ) {
144
- values . sort ( ( a , b ) => {
145
- if ( a . label === "n/a" ) return 1 ;
146
- if ( b . label === "n/a" ) return - 1 ;
147
- const orderDiff = getSeverityOrder ( a . label ) - getSeverityOrder ( b . label ) ;
148
- if ( orderDiff !== 0 ) return orderDiff ;
149
- return b . count - a . count ;
150
- } ) ;
151
- } else {
152
- values . sort ( ( a , b ) => {
153
- if ( a . label === "n/a" ) return 1 ;
154
- if ( b . label === "n/a" ) return - 1 ;
155
- return b . count - a . count ;
156
- } ) ;
157
- }
134
+ if ( [ "assignee" , "incident" ] . includes ( key as string ) && nullCount > 0 ) {
135
+ values . push ( {
136
+ label : "n/a" ,
137
+ count : nullCount ,
138
+ isSelected :
139
+ facetFilters [ key ] ?. includes ( "n/a" ) || ! facetFilters [ key ] ?. length ,
140
+ } ) ;
141
+ }
158
142
159
- return values ;
160
- } ;
143
+ if ( key === "severity" ) {
144
+ values . sort ( ( a , b ) => {
145
+ if ( a . label === "n/a" ) return 1 ;
146
+ if ( b . label === "n/a" ) return - 1 ;
147
+ const orderDiff =
148
+ getSeverityOrder ( a . label ) - getSeverityOrder ( b . label ) ;
149
+ if ( orderDiff !== 0 ) return orderDiff ;
150
+ return b . count - a . count ;
151
+ } ) ;
152
+ } else {
153
+ values . sort ( ( a , b ) => {
154
+ if ( a . label === "n/a" ) return 1 ;
155
+ if ( b . label === "n/a" ) return - 1 ;
156
+ return b . count - a . count ;
157
+ } ) ;
158
+ }
159
+
160
+ return values ;
161
+ } ,
162
+ [ alerts , facetFilters , timeRange ]
163
+ ) ;
161
164
162
165
const staticFacets = [
163
166
"severity" ,
0 commit comments