All files / modules/75-cf/pages/segment-details/flags-use-segment FlagsUseSegment.tsx

63.1% Statements 53/84
42.02% Branches 50/119
43.48% Functions 10/23
61.25% Lines 49/80

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              2x 2x 2x   2x 2x 2x             2x 2x 2x       2x 2x 2x   2x 2x 2x 2x 2x 2x           2x 5x 5x           5x 5x 5x   5x                     5x       5x         5x                                                                                         5x                                                                                               5x                                               1x 1x                                   2x 1x 1x                                                                                           2x 5x 5x 5x 5x   5x 4x     1x             1x                                                                                             2x               1x   1x 1x   1x                                                                                    
/*
 * Copyright 2021 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, { ReactElement, useState } from 'react'
import { useParams } from 'react-router-dom'
import { Container, FlexExpander, Heading, Layout, Text, PageError, useToaster } from '@wings-software/uicore'
import type { HeadingProps } from '@wings-software/uicore/dist/components/Heading/Heading'
import { useStrings } from 'framework/strings'
import { useGovernance } from '@cf/hooks/useGovernance'
import {
  GitSyncErrorResponse,
  SegmentFlag,
  SegmentFlagsResponseResponse,
  useGetSegmentFlags,
  usePatchFeature
} from 'services/cf'
import { ContainerSpinner } from '@common/components/ContainerSpinner/ContainerSpinner'
import { getErrorMessage, EntityAddingMode } from '@cf/utils/CFUtils'
import {
  SelectedFeatureFlag,
  SelectFeatureFlagsModalButton
} from '@cf/components/SelectFeatureFlagsModalButton/SelectFeatureFlagsModalButton'
import { ItemContainer, ItemContainerProps } from '@cf/components/ItemContainer/ItemContainer'
import { NoDataFoundRow } from '@cf/components/NoDataFoundRow/NoDataFoundRow'
import useActiveEnvironment from '@cf/hooks/useActiveEnvironment'
 
import SaveFlagToGitModal from '@cf/components/SaveFlagToGitModal/SaveFlagToGitModal'
import { AUTO_COMMIT_MESSAGES } from '@cf/constants/GitSyncConstants'
import { GitSyncFormValues, GIT_SYNC_ERROR_CODE, UseGitSync } from '@cf/hooks/useGitSync'
import usePlanEnforcement from '@cf/hooks/usePlanEnforcement'
import { DetailHeading } from '../DetailHeading'
import FlagItemOptionsMenuButton from './flag-item-options-menu-button/FlagItemOptionsMenuButton'
 
interface FlagsUseSegmentProps {
  gitSync: UseGitSync
}
 
export const FlagsUseSegment = ({ gitSync }: FlagsUseSegmentProps): ReactElement => {
  const { getString } = useStrings()
  const { showError } = useToaster()
  const {
    accountId: accountIdentifier,
    orgIdentifier,
    projectIdentifier,
    segmentIdentifier
  } = useParams<Record<string, string>>()
  const { activeEnvironment: environmentIdentifier } = useActiveEnvironment()
  const { handleError: handleGovernanceError, isGovernanceError } = useGovernance()
 
  const queryParams = {
    accountIdentifier,
    orgIdentifier,
    projectIdentifier,
    environmentIdentifier
  }
  const {
    loading,
    error,
    data: flags,
    refetch: refetchFlags
  } = useGetSegmentFlags({
    identifier: segmentIdentifier,
    queryParams
  })
  const { mutate: patchFeature } = usePatchFeature({
    identifier: '',
    queryParams
  })
 
  const addSegmentToFlags = async (
    selectedFeatureFlags: SelectedFeatureFlag[],
    gitFormValues?: GitSyncFormValues
  ): Promise<void> => {
    // Note: Due to https://harness.atlassian.net/browse/FFM-713 not done, we make
    // multiple patch APIs instead of single one
 
    return await Promise.all(
      selectedFeatureFlags.map(({ feature, variationIdentifier }) => {
        const patchInstruction = {
          instructions: [
            {
              kind: 'addSegmentToVariationTargetMap',
              parameters: { variation: variationIdentifier, targetSegments: [segmentIdentifier] }
            }
          ]
        }
 
        return patchFeature(
          gitFormValues ? { ...patchInstruction, gitDetails: gitFormValues.gitDetails } : patchInstruction,
          {
            pathParams: { identifier: feature.identifier }
          }
        )
      })
    )
      .then(async () => {
        if (gitFormValues?.autoCommit) {
          await gitSync.handleAutoCommit(gitFormValues?.autoCommit)
        }
        refetchFlags()
      })
      .catch(e => {
        if (e.status === GIT_SYNC_ERROR_CODE) {
          gitSync.handleError(e.data as GitSyncErrorResponse)
        } else {
          if (isGovernanceError(e)) {
            handleGovernanceError(e.data)
          } else {
            showError(getErrorMessage(e), undefined, 'cf.path.feature.error')
          }
        }
      })
  }
 
  const removeRules = async (
    featureFlagIdentifier: string,
    instruction: string,
    variationIdentifier?: string,
    ruleId?: string,
    gitSyncFormValues?: GitSyncFormValues
  ): Promise<void> => {
    let gitDetails
    if (gitSync) {
      const { gitSyncInitialValues } = gitSync.getGitSyncFormMeta(AUTO_COMMIT_MESSAGES.DELETED_FLAG_FROM_SEGMENT)
 
      if (gitSync.isGitSyncEnabled) {
        if (gitSync.isAutoCommitEnabled) {
          gitDetails = gitSyncInitialValues.gitDetails
        } else {
          gitDetails = gitSyncFormValues?.gitDetails
        }
      }
    }
 
    const instructions = {
      instructions: [
        {
          kind: instruction,
          parameters: {
            variation: variationIdentifier,
            targetSegments: [segmentIdentifier],
            ruleID: ruleId
          }
        }
      ]
    }
 
    await patchFeature(gitSync?.isGitSyncEnabled ? { ...instructions, gitDetails } : instructions, {
      pathParams: { identifier: featureFlagIdentifier }
    })
      .then(async () => {
        await refetchFlags()
      })
      .catch(e => {
        if (e.status === GIT_SYNC_ERROR_CODE) {
          gitSync.handleError(e.data as GitSyncErrorResponse)
        } else {
          showError(getErrorMessage(e), undefined, 'cf.path.feature.error')
        }
      })
  }
 
  return (
    <Container width={480} height="100%" style={{ background: '#F8FAFB', overflow: 'auto', minWidth: '480px' }}>
      <Layout.Horizontal width="100%" height={55} style={{ alignItems: 'baseline' }} padding={{ right: 'small' }}>
        <DetailHeading style={{ paddingBottom: 0 }}>
          {getString(flags?.length ? 'cf.segments.usingSegmentWithCount' : 'cf.segments.usingSegment', {
            count: flags?.length || 0
          })}
        </DetailHeading>
        <FlexExpander />
        <SelectFeatureFlagsModalButton
          text={getString('cf.segmentDetail.addToFlag')}
          minimal
          gitSync={gitSync}
          intent="primary"
          accountIdentifier={accountIdentifier}
          orgIdentifier={orgIdentifier}
          projectIdentifier={projectIdentifier}
          environmentIdentifier={environmentIdentifier}
          modalTitle={getString('cf.segmentDetail.addSegmentToFlag')}
          submitButtonTitle={getString('add')}
          onSubmit={async (checkedFeatureFlags, gitSyncFormValues) =>
            await addSegmentToFlags(checkedFeatureFlags, gitSyncFormValues)
          }
          shouldDisableItem={feature =>
            (flags?.filter(flag => flag.type === EntityAddingMode.DIRECT) || [])
              .map(flag => flag.identifier)
              .includes(feature.identifier)
          }
        />
      </Layout.Horizontal>
      <Container
        width="100%"
        height="calc(100% - 55px)"
        style={{ overflow: 'auto', padding: '0 var(--spacing-xxlarge) var(--spacing-xxlarge)' }}
      >
        {error && <PageError message={getErrorMessage(error)} onClick={() => refetchFlags()} />}
        {!error && !loading && <FlagsList flags={flags} gitSync={gitSync} onRemoveRule={removeRules} />}
        {loading && <ContainerSpinner />}
      </Container>
    </Container>
  )
}
 
const SectionHeader: React.FC<HeadingProps> = ({ children, ...props }) => {
  const { getString } = useStrings()
  return (
    <>
      <Heading
        level={3}
        style={{
          color: '#4F5162',
          position: 'sticky',
          top: 0,
          padding: 'var(--spacing-xxlarge) 0 var(--spacing-large)',
          background: 'rgb(248, 250, 251)',
          fontSize: '14px',
          fontWeight: 500
        }}
        {...props}
      >
        {children}
      </Heading>
      <Layout.Horizontal style={{ alignItems: 'center' }} margin={{ bottom: 'small' }} padding={{ left: 'small' }}>
        <Text
          padding={{ left: 'small' }}
          style={{ color: '#4F5162', fontSize: '10px', fontWeight: 'bold', flexGrow: 1 }}
        >
          {getString('cf.shared.flags').toLocaleUpperCase()}
        </Text>
        <Text
          width={150}
          style={{ color: '#4F5162', fontSize: '10px', fontWeight: 'bold' }}
          padding={{ left: 'small' }}
        >
          {getString('cf.shared.variation').toLocaleUpperCase()}
        </Text>
      </Layout.Horizontal>
    </>
  )
}
 
const FlagsList: React.FC<{
  gitSync: UseGitSync
  flags: SegmentFlagsResponseResponse | null
  onRemoveRule: (
    featureFlagIdentifier: string,
    instruction: string,
    variationIdentifier?: string,
    ruleId?: string,
    gitSyncFormValues?: GitSyncFormValues
  ) => Promise<void>
}> = ({ flags, onRemoveRule, gitSync }) => {
  const { isPlanEnforcementEnabled } = usePlanEnforcement()
  const { getString } = useStrings()
  const directAddedFlags = flags?.filter(flag => flag.type === EntityAddingMode.DIRECT) || []
  const conditionalAddedFlags = flags?.filter(flag => flag.type === EntityAddingMode.CONDITION) || []
 
  if (!flags?.length) {
    return <NoDataFoundRow message={getString('cf.segmentDetail.noFlagsUseThisSegment')} margin={{ top: 'xxlarge' }} />
  }
 
  return (
    <>
      {!!directAddedFlags?.length && (
        <>
          <SectionHeader>{getString('cf.segmentDetail.directlyAdded')}</SectionHeader>
          <Layout.Vertical spacing="small" style={{ padding: '1px' }}>
            {directAddedFlags.map(_flag => (
              <FlagItem
                key={_flag.identifier}
                flag={_flag}
                gitSync={gitSync}
                onRemoveRule={onRemoveRule}
                instruction="removeSegmentToVariationTargetMap"
                isPlanEnforcementEnabled={isPlanEnforcementEnabled}
              />
            ))}
          </Layout.Vertical>
        </>
      )}
      {!!conditionalAddedFlags?.length && (
        <>
          <SectionHeader>{getString('cf.segmentDetail.autoAdded')}</SectionHeader>
          <Layout.Vertical spacing="small" style={{ padding: '1px' }}>
            {conditionalAddedFlags.map(_flag => (
              <FlagItem
                key={_flag.identifier}
                flag={_flag}
                gitSync={gitSync}
                onRemoveRule={onRemoveRule}
                instruction="removeRule"
                isPlanEnforcementEnabled={isPlanEnforcementEnabled}
              />
            ))}
          </Layout.Vertical>
        </>
      )}
    </>
  )
}
 
interface FlagItemProps extends ItemContainerProps {
  gitSync: UseGitSync
  flag: SegmentFlag
  isPlanEnforcementEnabled: boolean
  instruction: string
  onRemoveRule?: (
    featureFlagIdentifier: string,
    instruction: string,
    variationIdentifier?: string,
    ruleId?: string,
    gitSyncFormValues?: GitSyncFormValues
  ) => Promise<void>
}
 
const FlagItem: React.FC<FlagItemProps> = ({
  gitSync,
  flag,
  onRemoveRule,
  instruction,
  isPlanEnforcementEnabled,
  ...props
}) => {
  const { name, description, variation } = flag
 
  const variationTextWidth = onRemoveRule ? '88px' : '135px'
  const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false)
 
  return (
    <ItemContainer style={{ paddingRight: 'var(--spacing-xsmall)' }} {...props}>
      <Layout.Horizontal spacing="xsmall" style={{ alignItems: 'center' }}>
        <Container style={{ flexGrow: 1 }} padding={{ left: 'xsmall', right: 'small' }}>
          <Text
            margin={{ bottom: description?.length ? 'xsmall' : undefined }}
            style={{ color: '#22222A', fontSize: '12px', fontWeight: 600, lineHeight: '16px' }}
          >
            {name}
          </Text>
          {description && <Text style={{ color: '#22222ac7' }}>{description}</Text>}
        </Container>
 
        {/* NOTE: flag does not have enough info to render variation icon.
            Only identifier text for now. See: https://harness.atlassian.net/browse/FFM-699 */}
        <Text style={{ minWidth: variationTextWidth, maxWidth: variationTextWidth }}>{variation}</Text>
 
        {onRemoveRule && (
          <FlagItemOptionsMenuButton
            onClick={() => {
              if (gitSync?.isGitSyncEnabled && !gitSync?.isAutoCommitEnabled) {
                setIsDeleteModalOpen(true)
              } else {
                onRemoveRule(flag.identifier, instruction, variation, flag.ruleId)
              }
            }}
          />
        )}
      </Layout.Horizontal>
      {isDeleteModalOpen && (
        <SaveFlagToGitModal
          flagName={flag.name}
          flagIdentifier={flag.identifier}
          onSubmit={formValues => onRemoveRule?.(flag.identifier, instruction, variation, flag.ruleId, formValues)}
          onClose={() => {
            setIsDeleteModalOpen(false)
          }}
        />
      )}
    </ItemContainer>
  )
}