All files / modules/75-cf/components/FlagActivation FlagActivation.tsx

34.27% Statements 73/213
27.36% Branches 81/296
10.71% Functions 6/56
35.78% Lines 73/204

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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587              2x 2x 2x   2x 2x                       2x 2x 2x                         2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x       2x                                         2x                     2x 6x 6x 6x 6x 6x 6x                             6x                   6x   6x   6x       6x   6x                   4x                   6x   6x         6x   6x                                                                                                                                                                                                                                                                                                                                                                                                     6x                                   6x                                                 6x                             6x 6x 6x 6x 6x 6x   6x 6x         6x 1x     5x       5x 1x                                                         4x                           5x                                                                                                                                 1x 1x 1x                                                               2x  
/*
 * 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 React, { MouseEvent, useCallback, useEffect, useMemo, useState } from 'react'
import { useHistory, useParams } from 'react-router-dom'
import { v4 as uuid } from 'uuid'
import type { FormikActions } from 'formik'
import { cloneDeep, get, isEqual } from 'lodash-es'
import {
  Button,
  Container,
  FlexExpander,
  Formik,
  FormikForm,
  Layout,
  PageError,
  Tab,
  Tabs,
  Text
} from '@wings-software/uicore'
import * as yup from 'yup'
import cx from 'classnames'
import {
  Clause,
  Feature,
  FeatureState,
  GitDetails,
  GitSyncErrorResponse,
  PatchFeatureQueryParams,
  Serve,
  ServingRule,
  TargetMap,
  usePatchFeature,
  VariationMap
} from 'services/cf'
import { useStrings } from 'framework/strings'
import { extraOperatorReference } from '@cf/constants'
import { useToaster } from '@common/exports'
import { useQueryParams } from '@common/hooks'
import { useEnvironmentSelectV2 } from '@cf/hooks/useEnvironmentSelectV2'
import { useGovernance } from '@cf/hooks/useGovernance'
import { FFDetailPageTab, getErrorMessage, rewriteCurrentLocationWithActiveEnvironment } from '@cf/utils/CFUtils'
import routes from '@common/RouteDefinitions'
import useActiveEnvironment from '@cf/hooks/useActiveEnvironment'
import type { FeatureFlagPathProps, ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { AUTO_COMMIT_MESSAGES } from '@cf/constants/GitSyncConstants'
import { GIT_SYNC_ERROR_CODE, UseGitSync } from '@cf/hooks/useGitSync'
import { useFeatureFlag } from '@common/hooks/useFeatureFlag'
import { FeatureFlag } from '@common/featureFlags'
import TargetingRulesTab from '@cf/pages/feature-flags-detail/targeting-rules-tab/TargetingRulesTab'
import { ContainerSpinner } from '@common/components/ContainerSpinner/ContainerSpinner'
import TabTargeting from '../EditFlagTabs/TabTargeting'
import TabActivity from '../EditFlagTabs/TabActivity'
import { CFEnvironmentSelect } from '../CFEnvironmentSelect/CFEnvironmentSelect'
import patch, { ClauseData, getDiff } from '../../utils/instructions'
import { MetricsView } from './views/MetricsView'
import { NoEnvironment } from '../NoEnvironment/NoEnvironment'
import SaveFlagToGitSubFormModal from '../SaveFlagToGitSubFormModal/SaveFlagToGitSubFormModal'
import css from './FlagActivation.module.scss'
 
// Show loading and wait 3s when the first environment is created before reloading
// current detail page. See https://harness.atlassian.net/browse/FFM-565
const WAIT_TIME_FOR_NEWLY_CREATED_ENVIRONMENT = 3000
 
interface FlagActivationProps {
  projectIdentifier: string
  flagData: Feature
  gitSync: UseGitSync
  refetchFlag: () => Promise<unknown>
  refetchFlagLoading: boolean
}
 
export interface FlagActivationFormValues {
  [key: string]: unknown
  state: string
  offVariation: string
  defaultServe: Serve
  customRules: ServingRule[]
  variationMap: VariationMap[]
  gitDetails: GitDetails
  autoCommit: boolean
}
 
const fromVariationMapToObj = (variationMap: VariationMap[]) =>
  variationMap.reduce((acc: any, vm: VariationMap) => {
    if (acc[vm.variation]) {
      acc[vm.variation].targets = acc[vm.variation].targets.concat(vm.targets || [])
      acc[vm.variation].targetSegments = acc[vm.variation].targetSegments.concat(vm.targetSegments || [])
    } else {
      acc[vm.variation] = { targets: vm.targets, targetSegments: vm.targetSegments }
    }
    return acc
  }, {})
 
const FlagActivation: React.FC<FlagActivationProps> = props => {
  const { flagData, projectIdentifier, refetchFlag, refetchFlagLoading, gitSync } = props
  const { showError } = useToaster()
  const [editing, setEditing] = useState(false)
  const { orgIdentifier, accountId: accountIdentifier } = useParams<Record<string, string>>()
  const { activeEnvironment: environmentIdentifier, withActiveEnvironment } = useActiveEnvironment()
  const { mutate: patchFeature } = usePatchFeature({
    identifier: flagData.identifier as string,
    queryParams: {
      projectIdentifier,
      environmentIdentifier,
      accountIdentifier,
      orgIdentifier
    } as PatchFeatureQueryParams
  })
  const {
    EnvironmentSelect,
    loading: envsLoading,
    error: envsError,
    refetch: refetchEnvironments,
    environments
  } = useEnvironmentSelectV2({
    selectedEnvironmentIdentifier: environmentIdentifier,
    onChange: (_value, _environment, _userEvent) => {
      rewriteCurrentLocationWithActiveEnvironment(_environment)
 
      if (_userEvent) {
        refetchFlag()
      }
    }
  })
  const { handleError: handleGovernanceError, isGovernanceError } = useGovernance()
 
  const FFM_1513 = useFeatureFlag(FeatureFlag.FFM_1513)
 
  const { gitSyncValidationSchema, gitSyncInitialValues } = gitSync?.getGitSyncFormMeta(
    AUTO_COMMIT_MESSAGES.UPDATED_FLAG_RULES
  )
 
  const initialValues = useMemo(
    () =>
      cloneDeep({
        state: flagData.envProperties?.state as string,
        onVariation: flagData.envProperties?.defaultServe.variation
          ? flagData.envProperties?.defaultServe.variation
          : flagData.defaultOnVariation,
        offVariation: flagData.envProperties?.offVariation as string,
        defaultServe: flagData.envProperties?.defaultServe as Serve,
        customRules: flagData.envProperties?.rules ?? [],
        variationMap: cloneDeep(
          // filter out variations with no targets. UI needs reworked to suit the use case: https://harness.atlassian.net/browse/FFM-1267
          flagData.envProperties?.variationMap?.filter(variationMapItem => !!variationMapItem?.targets?.length) ?? []
        ),
        flagName: flagData.name,
        flagIdentifier: flagData.identifier,
        gitDetails: gitSyncInitialValues.gitDetails,
        autoCommit: gitSyncInitialValues.autoCommit
      }),
    [gitSyncInitialValues.gitDetails, gitSyncInitialValues.autoCommit]
  )
 
  const noEnvironmentExists = !envsLoading && !envsError && environments?.length === 0
 
  const onCancelEditHandler = (): void => {
    setEditing(false)
    patch.feature.reset()
  }
 
  const [isGitSyncOpenModal, setIsGitSyncModalOpen] = useState(false)
 
  const onSaveChanges = useCallback(
    (values: FlagActivationFormValues, formikActions: FormikActions<FlagActivationFormValues>): void => {
      // handle flag state changed - e.g. toggled from off to on
      if (values.state !== initialValues.state) {
        patch.feature.addInstruction(patch.creators.setFeatureFlagState(values?.state as FeatureState))
      }
      // handle flag off variation changed
      if (!isEqual(values.offVariation, initialValues.offVariation)) {
        patch.feature.addInstruction(patch.creators.updateOffVariation(values.offVariation as string))
      }
      // handle flag default on variation changed
      if (!isEqual(values.onVariation, initialValues.onVariation)) {
        patch.feature.addInstruction(patch.creators.updateDefaultServeByVariation(values.onVariation as string))
      }
      // handle custom rules changed (does not include target variations)
      if (!isEqual(values.customRules, initialValues.customRules)) {
        const toClauseData = (c: Clause): ClauseData => {
          if (c.op === extraOperatorReference.customRules.matchSegment.value) {
            return {
              op: c.op,
              values: c.values
            }
          } else {
            return {
              attribute: c.attribute as string,
              op: c.op,
              values: c.values
            }
          }
        }
 
        // handle removed custom rules
        patch.feature.addAllInstructions(
          initialValues.customRules
            .filter(rule => !values.customRules.find(r => r.ruleId === rule.ruleId))
            .map(r => patch.creators.removeRule(r.ruleId as string))
        )
 
        const newRuleIds: string[] = []
        values.customRules = values.customRules.map(rule => {
          if (!rule.ruleId) {
            const newId = uuid()
            newRuleIds.push(newId)
            rule.ruleId = newId
          }
          return rule
        })
        const isNewRule = (id: string): boolean => newRuleIds.includes(id)
 
        // handle newly added custom rules
        patch.feature.addAllInstructions(
          values.customRules
            .filter(rule => isNewRule(rule.ruleId as string))
            .map(rule =>
              patch.creators.addRule({
                uuid: rule.ruleId,
                priority: rule.priority,
                serve: rule.serve,
                clauses: rule.clauses.map(toClauseData)
              })
            )
        )
 
        // handle updates to existing rules
        values.customRules
          .filter(rule => !isNewRule(rule.ruleId as string))
          .map(rule => [initialValues.customRules.find(r => r.ruleId === rule.ruleId), rule])
          .filter(([initial, current]) => !isEqual(initial, current))
          .forEach(([initial, current]) => {
            // handle clause added to existing rule
            current?.clauses
              .filter(c => !c.id)
              .forEach(c =>
                patch.feature.addInstruction(patch.creators.addClause(current.ruleId as string, toClauseData(c)))
              )
 
            // handle clause removed from existing rule
            initial?.clauses
              .filter(c => !current?.clauses.find(cl => cl.id === c.id))
              .forEach(c =>
                patch.feature.addInstruction(patch.creators.removeClause(initial.ruleId as string, c.id as string))
              )
 
            // handle clause changed on existing rule
            initial?.clauses
              .reduce((acc: any, prev) => {
                const currentValue = current?.clauses.find(c => c.id === prev.id)
                if (currentValue && !isEqual(prev, currentValue)) {
                  return [...acc, [prev, currentValue]]
                }
                return acc
              }, [])
              .forEach(([c, updated]: [Clause, Clause]) =>
                patch.feature.addInstruction(
                  patch.creators.updateClause(initial.ruleId as string, c.id as string, toClauseData(updated))
                )
              )
 
            // handle update to existing rule serve value (true/false)
            if (current?.serve?.variation && !isEqual(initial?.serve?.variation, current?.serve?.variation)) {
              patch.feature.addInstruction(
                patch.creators.updateRuleVariation(current?.ruleId as string, current.serve.variation)
              )
            }
 
            if (current?.serve?.distribution && !isEqual(initial?.serve?.distribution, current.serve.distribution)) {
              patch.feature.addInstruction(
                patch.creators.updateRuleVariation(current?.ruleId as string, current.serve.distribution)
              )
            }
          })
      }
 
      // TODO: this whole complication needs to be refactored
      const normalize = (variationMap: VariationMap[]) =>
        (variationMap || []).map(item => {
          if (item.targets?.length) {
            item.targets = item.targets?.map(target => target?.identifier || target) as unknown as TargetMap[]
          }
          return item
        })
      const normalizedInitialVariationMap = normalize(initialValues.variationMap)
      const normalizedVariationMap = normalize(values.variationMap)
 
      // handle target variations updated
      if (!isEqual(normalizedVariationMap, normalizedInitialVariationMap)) {
        const initial = fromVariationMapToObj(normalizedInitialVariationMap)
        const updated = fromVariationMapToObj(normalizedVariationMap)
 
        const variations = Array.from(new Set(Object.keys(initial).concat(Object.keys(updated))))
        variations.forEach((variation: string) => {
          const [added, removed] = getDiff<string, string>(
            initial[variation]?.targets || [],
            updated[variation]?.targets || []
          )
          // handle newly added target variations
          if (added.length > 0) {
            patch.feature.addInstruction(patch.creators.addTargetsToVariationTargetMap(variation, added))
          }
          // handle removed target variations
          if (removed.length > 0) {
            patch.feature.addInstruction(patch.creators.removeTargetsToVariationTargetMap(variation, removed))
          }
        })
      }
 
      const prevOrder = initialValues.customRules.map(x => x.ruleId)
      const newOrder = values.customRules.map(x => x.ruleId)
      // handle reordered custom rules
      if (!isEqual(prevOrder, newOrder)) {
        patch.feature.addInstruction(patch.creators.reorderRules(newOrder as string[]))
      }
 
      patch.feature
        .onPatchAvailable(data => {
          patchFeature(
            gitSync?.isGitSyncEnabled
              ? {
                  ...data,
                  gitDetails: values.gitDetails
                }
              : data
          )
            .then(async () => {
              if (!gitSync?.isAutoCommitEnabled && values.autoCommit) {
                await gitSync?.handleAutoCommit(values.autoCommit)
              }
 
              setEditing(false)
              return refetchFlag()
            })
            .then(() => {
              formikActions.resetForm()
            })
            .catch(err => {
              if (err.status === GIT_SYNC_ERROR_CODE) {
                gitSync.handleError(err.data as GitSyncErrorResponse)
              } else {
                if (isGovernanceError(err)) {
                  handleGovernanceError(err.data)
                } else {
                  showError(get(err, 'data.message', err?.message), 0)
                }
              }
            })
            .finally(() => {
              patch.feature.reset()
            })
        })
        .onEmptyPatch(() => setEditing(false))
    },
    [initialValues, patchFeature, showError]
  )
 
  type RuleErrors = { [K: number]: { [P: number]: 'required' } }
  const validateRules = (rules: ServingRule[]): [RuleErrors, boolean] => {
    const errors: RuleErrors = {}
    let valid = true
    rules.forEach((rule: ServingRule, ruleIdx: number) => {
      rule.clauses.map((clause: Clause, clauseIdx: number) => {
        if (clause.values.length === 0) {
          if (!errors[ruleIdx]) {
            errors[ruleIdx] = {}
          }
          errors[ruleIdx][clauseIdx] = 'required'
          valid = false
        }
      })
    })
    return [errors, valid]
  }
 
  type VariationMapErrors = { [K: number]: { variation?: 'required'; targets?: 'required' } }
  const validateVariationMap = (variationMap: VariationMap[]): [VariationMapErrors, boolean] => {
    let valid = true
    const errors = variationMap.reduce((acc: VariationMapErrors, next: VariationMap, idx: number) => {
      if (!next.variation.length) {
        valid = false
        acc[idx] = {
          variation: 'required'
        }
      }
      if (!next.targets?.length) {
        valid = false
        acc[idx] = {
          ...(acc[idx] || {}),
          targets: 'required'
        }
      }
      return acc
    }, {})
    return [errors, valid]
  }
 
  type FormErrors = {
    rules?: RuleErrors
    variationMap?: VariationMapErrors
  }
  const validateForm = (values: FlagActivationFormValues): FormErrors => {
    const errors: FormErrors = {}
 
    const [rules, validRules] = validateRules(values.customRules)
    if (!validRules) {
      errors.rules = rules
    }
 
    const [variationMap, validVariationMap] = validateVariationMap(values.variationMap)
    if (!validVariationMap) {
      errors.variationMap = variationMap
    }
 
    return errors
  }
  const { tab = FFDetailPageTab.TARGETING } = useQueryParams<{ tab?: string }>()
  const [activeTabId, setActiveTabId] = useState(tab)
  const [newEnvironmentCreateLoading, setNewEnvironmentCreateLoading] = useState(false)
  const { getString } = useStrings()
  const history = useHistory()
  const pathParams = useParams<ProjectPathProps & FeatureFlagPathProps>()
 
  useEffect(() => {
    Iif (tab !== activeTabId) {
      history.replace(withActiveEnvironment(routes.toCFFeatureFlagsDetail(pathParams) + `?tab=${activeTabId}`))
    }
  }, [activeTabId, history, pathParams, tab, withActiveEnvironment])
 
  if (envsError) {
    return <PageError message={getErrorMessage(envsError)} onClick={() => refetchEnvironments()} />
  }
 
  Iif (envsLoading || newEnvironmentCreateLoading || (refetchFlagLoading && !FFM_1513)) {
    return <ContainerSpinner height="100%" flex={{ justifyContent: 'center', alignItems: 'center' }} />
  }
 
  if (noEnvironmentExists) {
    return (
      <Container style={{ height: '100%', display: 'grid', alignItems: 'center' }}>
        <NoEnvironment
          style={{ marginTop: '-100px' }}
          onCreated={response => {
            history.replace(
              withActiveEnvironment(
                routes.toCFFeatureFlagsDetail({
                  orgIdentifier,
                  projectIdentifier: flagData.project,
                  featureFlagIdentifier: flagData.identifier,
                  accountId: accountIdentifier
                }),
                response?.data?.identifier
              )
            )
 
            // See https://harness.atlassian.net/browse/FFM-565
            setNewEnvironmentCreateLoading(true)
            setTimeout(() => {
              setNewEnvironmentCreateLoading(false)
              refetchEnvironments()
            }, WAIT_TIME_FOR_NEWLY_CREATED_ENVIRONMENT)
          }}
        />
      </Container>
    )
  }
 
  return (
    <Formik
      enableReinitialize={true}
      validateOnChange={false}
      validateOnBlur={false}
      formName="flagActivation"
      initialValues={initialValues}
      validate={validateForm}
      validationSchema={yup.object().shape({
        gitDetails: gitSyncValidationSchema
      })}
      onSubmit={onSaveChanges}
    >
      {formikProps => {
        return (
          <FormikForm>
            <Container className={css.formContainer}>
              <Layout.Horizontal className={css.environmentHeaderContainer} flex={{ alignItems: 'center' }}>
                <FlexExpander />
                <CFEnvironmentSelect component={<EnvironmentSelect />} />
              </Layout.Horizontal>
 
              <Container className={FFM_1513 ? css.tabContainer : cx(css.tabContainer, css.tabContainerHeight)}>
                {flagData && (
                  <>
                    <Tabs
                      id="editFlag"
                      defaultSelectedTabId={activeTabId}
                      onChange={(tabId: string) => setActiveTabId(tabId)}
                    >
                      <Tab
                        id={FFDetailPageTab.TARGETING}
                        title={<Text className={css.tabTitle}>{getString('cf.featureFlags.targeting')}</Text>}
                        panel={
                          <>
                            {FFM_1513 ? (
                              <TargetingRulesTab
                                featureFlagData={flagData}
                                refetchFlag={refetchFlag}
                                refetchFlagLoading={refetchFlagLoading}
                              />
                            ) : (
                              <TabTargeting
                                formikProps={formikProps}
                                editing={editing}
                                projectIdentifier={projectIdentifier}
                                environmentIdentifier={environmentIdentifier}
                                setEditing={setEditing}
                                feature={flagData}
                                orgIdentifier={orgIdentifier}
                                accountIdentifier={accountIdentifier}
                              />
                            )}
                          </>
                        }
                      />
                      <Tab
                        id={FFDetailPageTab.METRICS}
                        title={<Text className={css.tabTitle}>{getString('cf.featureFlags.metrics.title')}</Text>}
                        panel={<MetricsView flagData={flagData} />}
                      />
 
                      <Tab
                        id={FFDetailPageTab.ACTIVITY}
                        title={<Text className={css.tabTitle}>{getString('cf.featureFlags.activity')}</Text>}
                        panel={<TabActivity flagData={flagData} />}
                      />
                    </Tabs>
                  </>
                )}
              </Container>
              {(editing || formikProps.values.state !== flagData.envProperties?.state) &&
                activeTabId === FFDetailPageTab.TARGETING && (
                  <Layout.Horizontal className={css.actionButtons} padding="medium" spacing="small">
                    <Button
                      type="submit"
                      intent="primary"
                      text={getString('save')}
                      onClick={event => {
                        Eif (gitSync?.isGitSyncEnabled && !gitSync?.isAutoCommitEnabled) {
                          event.preventDefault()
                          setIsGitSyncModalOpen(true)
                        }
                      }}
                    />
                    <Button
                      minimal
                      text={getString('cancel')}
                      onClick={(e: MouseEvent) => {
                        e.preventDefault()
                        onCancelEditHandler()
                        formikProps.handleReset()
                      }}
                    />
                  </Layout.Horizontal>
                )}
            </Container>
            {isGitSyncOpenModal && (
              <SaveFlagToGitSubFormModal
                title={getString('cf.gitSync.saveFlagToGit', {
                  flagName: flagData.name
                })}
                onSubmit={formikProps.submitForm}
                onClose={() => setIsGitSyncModalOpen(false)}
              />
            )}
          </FormikForm>
        )
      }}
    </Formik>
  )
}
 
export default FlagActivation