@@ -17,13 +17,6 @@ import {
1717 Timestamps ,
1818} from './model' ;
1919
20- export const isIncidentFilter = ( filter : unknown ) : filter is IncidentFilters => {
21- return (
22- typeof filter === 'string' &&
23- [ 'Critical' , 'Warning' , 'Firing' , 'Informative' , 'Resolved' ] . includes ( filter )
24- ) ;
25- } ;
26-
2720function consolidateAndMergeIntervals ( data : Incident , dateArray : SpanDates ) {
2821 const severityRank = { 2 : 2 , 1 : 1 , 0 : 0 } ;
2922 const filteredValues = filterAndSortValues ( data , severityRank ) ;
@@ -331,7 +324,7 @@ export function filterIncident(filters: IncidentFiltersCombined, incidents: Arra
331324 } ;
332325
333326 return incidents . filter ( ( incident ) => {
334- if ( ! filters . severity . length && ! filters . state . length ) {
327+ if ( ! filters ? .severity ? .length && ! filters ? .state ?. length && ! filters ?. groupId ? .length ) {
335328 return true ;
336329 }
337330
@@ -345,8 +338,11 @@ export function filterIncident(filters: IncidentFiltersCombined, incidents: Arra
345338 ? filters . state . some ( ( filter ) => incident [ conditions [ filter ] ] === true )
346339 : true ; // True if no state filters
347340
341+ const isIncidentIdMatch =
342+ filters . groupId ?. length > 0 ? filters . groupId . includes ( incident . group_id ) : true ;
343+
348344 // Combine conditions with AND behavior between categories
349- return isSeverityMatch && isStateMatch ;
345+ return isSeverityMatch && isStateMatch && isIncidentIdMatch ;
350346 } ) ;
351347}
352348
@@ -488,6 +484,20 @@ export const changeDaysFilter = (
488484 ) ;
489485} ;
490486
487+ /**
488+ * A wrapper function that handles a user's selection on an incident filter.
489+ *
490+ * This function acts as the public entry point for filter selection,
491+ * passing the event details and filter state to the internal `onSelect`
492+ * helper function to perform the state update.
493+ *
494+ * @param {Event } event - The DOM event from the checkbox or filter selection.
495+ * @param {string } selection - The value of the filter being selected or deselected.
496+ * @param {Function } dispatch - The Redux dispatch function to trigger state changes.
497+ * @param {object } incidentsActiveFilters - The current state of active filters.
498+ * @param {string } filterCategoryType - The category of the filter (e.g., 'Incident ID', 'severity').
499+ * @returns {void }
500+ */
491501export const onIncidentFiltersSelect = (
492502 event ,
493503 selection : IncidentFilters ,
@@ -498,6 +508,20 @@ export const onIncidentFiltersSelect = (
498508 onSelect ( event , selection , dispatch , incidentsActiveFilters , filterCategoryType ) ;
499509} ;
500510
511+ /**
512+ * An internal helper function that manages the logic for selecting or deselecting a filter.
513+ *
514+ * It updates the Redux state based on the filter type. For 'groupId', it replaces the
515+ * existing selection (single-select behavior). For all other filters, it adds or
516+ * removes the selection from the array (multi-select behavior).
517+ *
518+ * @param {Event } event - The DOM event from the checkbox or filter selection.
519+ * @param {string } selection - The value of the filter being selected or deselected.
520+ * @param {Function } dispatch - The Redux dispatch function to trigger state changes.
521+ * @param {object } incidentsActiveFilters - The current state of active filters.
522+ * @param {string } filterCategoryType - The category of the filter.
523+ * @returns {void }
524+ */
501525const onSelect = ( event , selection , dispatch , incidentsActiveFilters , filterCategoryType ) => {
502526 const checked = event . target . checked ;
503527 let effectiveFilterType = filterCategoryType ;
@@ -510,12 +534,20 @@ const onSelect = (event, selection, dispatch, incidentsActiveFilters, filterCate
510534 const targetArray = incidentsActiveFilters [ effectiveFilterType ] || [ ] ;
511535 const newFilters = { ...incidentsActiveFilters } ;
512536
513- if ( checked ) {
514- if ( ! targetArray . includes ( selection ) ) {
515- newFilters [ effectiveFilterType ] = [ ...targetArray , selection ] ;
537+ if ( effectiveFilterType === 'groupId' ) {
538+ if ( checked ) {
539+ newFilters [ effectiveFilterType ] = [ selection ] ;
540+ } else {
541+ newFilters [ effectiveFilterType ] = [ ] ;
516542 }
517543 } else {
518- newFilters [ effectiveFilterType ] = targetArray . filter ( ( value ) => value !== selection ) ;
544+ if ( checked ) {
545+ if ( ! targetArray . includes ( selection ) ) {
546+ newFilters [ effectiveFilterType ] = [ ...targetArray , selection ] ;
547+ }
548+ } else {
549+ newFilters [ effectiveFilterType ] = targetArray . filter ( ( value ) => value !== selection ) ;
550+ }
519551 }
520552
521553 dispatch (
@@ -542,6 +574,16 @@ export const parseUrlParams = (search) => {
542574 return result ;
543575} ;
544576
577+ /**
578+ * Generates an array of unique incident ID options for a filter.
579+ *
580+ * This function iterates through a list of incident objects,
581+ * extracts their unique `group_id`s, and returns them in a format
582+ * suitable for a dropdown or filter component.
583+ *
584+ * @param {Array<Incident> } incidents - An array of incident objects.
585+ * @returns {{value: string}[] } An array of objects, where each object has a `value` key with a unique incident ID.
586+ */
545587export const getIncidentIdOptions = ( incidents : Array < Incident > ) => {
546588 const uniqueIds = new Set < string > ( ) ;
547589 incidents . forEach ( ( incident ) => {
@@ -554,6 +596,17 @@ export const getIncidentIdOptions = (incidents: Array<Incident>) => {
554596 } ) ) ;
555597} ;
556598
599+ /**
600+ * Maps a human-readable filter category name to its corresponding data key.
601+ *
602+ * This function is used to convert a display name (e.g., "Incident ID")
603+ * into the internal key used to access filter data (e.g., "groupId").
604+ * It handles a specific case for "Incident ID" and converts all other
605+ * category names to lowercase.
606+ *
607+ * @param {string } categoryName - The human-readable name of the filter category.
608+ * @returns {string } The corresponding data key for the filter.
609+ */
557610export const getFilterKey = ( categoryName : string ) : string => {
558611 if ( categoryName === 'Incident ID' ) {
559612 return 'groupId' ;
0 commit comments