All files / modules/75-cf/pages/feature-flags-detail/targeting-rules-tab/components/flag-enabled-rules-card/hooks useAvailableTargeting.tsx

100% Statements 29/29
92.86% Branches 13/14
100% Functions 13/13
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93                3x                                                 3x           70x   140x 300x 288x       70x 70x 150x   144x 85x 85x   59x 59x   234x             70x   912x     70x 70x 150x   144x 85x 85x       70x   280x   70x             3x  
/*
 * Copyright 2022 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import type { Segment, Target, Variation } from 'services/cf'
import {
  FormVariationMap,
  TargetingRuleItemStatus,
  TargetingRuleItemType,
  VariationPercentageRollout,
  VariationTarget,
  VariationTargetGroup
} from '../../../types'
 
// Currently 1 target/target group can be added per targeting rule
// This hook returns the available target groups/targets that has not been added to a rule,
// and returns the availble variations used in the "Add Targeting" button/dropdown
interface UseAvailableTargetingProps {
  featureFlagVariations: Variation[]
  targetingRuleItems: (FormVariationMap | VariationPercentageRollout)[]
  segments: Segment[]
  targets: Target[]
}
 
interface UseAvailableTargetingReturn {
  targetingDropdownVariations: Variation[]
  availableSegments: Segment[]
  availableTargets: Target[]
}
 
const useAvailableTargeting = ({
  featureFlagVariations,
  targetingRuleItems,
  segments,
  targets
}: UseAvailableTargetingProps): UseAvailableTargetingReturn => {
  const targetingDropdownVariations = featureFlagVariations.filter(
    variation =>
      !targetingRuleItems
        .filter(x => x.status !== TargetingRuleItemStatus.DELETED)
        .map(targetingRuleItem => (targetingRuleItem as FormVariationMap).variationIdentifier)
        .includes(variation.identifier)
  )
 
  const usedSegments: VariationTargetGroup[] = []
  targetingRuleItems
    .filter(x => x.status !== TargetingRuleItemStatus.DELETED)
    .forEach(targetingRuleItem => {
      if (targetingRuleItem.type === TargetingRuleItemType.VARIATION) {
        const item = targetingRuleItem as FormVariationMap
        usedSegments.push(...item.targetGroups)
      } else {
        const item = targetingRuleItem as VariationPercentageRollout
        usedSegments.push({
          priority: item.priority,
          label: segments.find(segment => segment.identifier === item.clauses[0].values[0])?.name as string,
          value: item.clauses[0].values[0],
          ruleId: item.ruleId || ''
        })
      }
    })
 
  const availableSegments = segments.filter(
    segment =>
      !usedSegments.some(usedSegment => segment.identifier === usedSegment.value || segment.name === usedSegment.label)
  )
 
  const usedTargets: VariationTarget[] = []
  targetingRuleItems
    .filter(x => x.status !== TargetingRuleItemStatus.DELETED)
    .forEach(targetingRuleItem => {
      if (targetingRuleItem.type === TargetingRuleItemType.VARIATION) {
        const item = targetingRuleItem as FormVariationMap
        usedTargets.push(...item.targets)
      }
    })
 
  const availableTargets = targets.filter(
    target =>
      !usedTargets.some(usedTarget => target.identifier === usedTarget.value || target.name === usedTarget.label)
  )
  return {
    targetingDropdownVariations,
    availableSegments,
    availableTargets
  }
}
 
export default useAvailableTargeting