All files / modules/75-cf/pages/feature-flags-detail/targeting-rules-tab/hooks useTargetingRulesFormData.tsx

100% Statements 30/30
92.05% Branches 81/88
100% Functions 13/13
100% Lines 28/28

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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121              3x   3x                                                   3x       21x 21x 21x 42x   42x 30x 15x           42x 92x 32x   63x             42x   32x         42x 19x                       46x 21x 14x                     21x 14x     21x                 21x 6x   21x             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 { CFVariationColors } from '@cf/constants'
import type { Feature, Segment } from 'services/cf'
import {
  FormVariationMap,
  TargetingRuleItemStatus,
  TargetingRuleItemType,
  TargetingRulesFormValues,
  VariationColorMap,
  VariationPercentageRollout,
  VariationTarget,
  VariationTargetGroup
} from '../types'
 
interface UseTargetingRulesFormData {
  featureFlagData: Feature
  segments: Segment[]
}
 
interface UseTargetingRulesFormDataReturn {
  initialValues: TargetingRulesFormValues
  getNextPriority: (targetingRules: (FormVariationMap | VariationPercentageRollout)[]) => number
  variationColorMap: VariationColorMap
}
 
// convert variations/targets/target groups into a more UI friendly structure
// Individual rules (variations/percentage rollouts) don't have a priority - only target groups do
// So we order the rules based on the target group with the highest priority
// This is to maintain the ordering
const useTargetingRulesFormData = ({
  featureFlagData,
  segments
}: UseTargetingRulesFormData): UseTargetingRulesFormDataReturn => {
  const formVariationMap: FormVariationMap[] = []
  const variationColorMap: VariationColorMap = {}
  featureFlagData.variations.forEach((variation, variationIndex) => {
    variationColorMap[variation.identifier] = CFVariationColors[variationIndex]
    const variationTargets: VariationTarget[] =
      featureFlagData.envProperties?.variationMap
        ?.find(variationMapItem => variation.identifier === variationMapItem.variation)
        ?.targets?.map(target => ({
          label: target.name,
          value: target.identifier
        })) || []
 
    const variationTargetGroups: VariationTargetGroup[] =
      featureFlagData.envProperties?.rules
        ?.filter(rule => rule.serve.variation === variation.identifier)
        .map(targetGroupRule => ({
          priority: targetGroupRule.priority,
          label: segments.find(segment => segment.identifier === targetGroupRule.clauses[0].values[0])?.name as string,
          ruleId: targetGroupRule.ruleId as string,
          value: targetGroupRule.clauses[0].values[0]
        })) || []
 
    // highest priority = lowest number
    const highestPriority =
      variationTargetGroups.reduce(
        (prev: VariationTargetGroup, current: VariationTargetGroup) =>
          Number(prev.priority) < Number(current.priority) ? prev : current,
        {} as VariationTargetGroup
      ).priority || 0
 
    // if this variation doesn't contain any targets/target groups we dont display it initially
    if (variationTargets.length || variationTargetGroups.length) {
      formVariationMap.push({
        status: TargetingRuleItemStatus.LOADED,
        priority: highestPriority,
        type: TargetingRuleItemType.VARIATION,
        variationIdentifier: variation.identifier,
        variationName: variation.name as string,
        targets: variationTargets,
        targetGroups: variationTargetGroups
      })
    }
  })
 
  const percentageRolloutRules = featureFlagData.envProperties?.rules?.filter(rule => rule.serve.distribution)
  const variationPercentageRollouts: VariationPercentageRollout[] = percentageRolloutRules
    ? percentageRolloutRules.map(percentageRollout => ({
        status: TargetingRuleItemStatus.LOADED,
        type: TargetingRuleItemType.PERCENTAGE_ROLLOUT,
        priority: percentageRollout.priority,
        variations: percentageRollout.serve.distribution?.variations || [],
        bucketBy: percentageRollout.serve.distribution?.bucketBy || 'identifier',
        clauses: percentageRollout.clauses,
        ruleId: percentageRollout.ruleId as string
      }))
    : []
 
  const targetingRuleItems = [...formVariationMap, ...variationPercentageRollouts].sort(
    (prev, next) => prev?.priority - next?.priority
  )
 
  const initialValues: TargetingRulesFormValues = {
    state: featureFlagData.envProperties?.state as string,
    onVariation: featureFlagData.envProperties?.defaultServe.variation
      ? featureFlagData.envProperties?.defaultServe.variation
      : featureFlagData.defaultOnVariation,
    offVariation: featureFlagData.envProperties?.offVariation as string,
    targetingRuleItems
  }
 
  const getNextPriority = (targetingRules: (FormVariationMap | VariationPercentageRollout)[]): number => {
    return Math.max(...targetingRules.map(x => x.priority)) + 1
  }
  return {
    initialValues,
    variationColorMap,
    getNextPriority
  }
}
 
export default useTargetingRulesFormData